index.js 1.68 KB
Newer Older
zhanghaozhe committed
1 2 3 4 5 6 7
import React, { Component } from "react"
import { connect } from "react-redux"
import "./recommendation.scss"
import { Course } from "src/common"
import { http } from "src/utils"
import { Toast } from "antd-mobile"
import { withRouter } from "react-router-dom"
zhanghaozhe committed
8

9
@connect()
zhanghaozhe committed
10
class Recommendation extends Component {
zhanghaozhe committed
11 12 13
  state = {
    courses: [],
  }
zhanghaozhe committed
14

zhanghaozhe committed
15 16 17 18 19 20 21 22 23 24 25
  componentDidMount() {
    http.get(`${API["search-api"]}/search_hot_word`).then((res) => {
      if (res.data.errno === 0) {
        this.setState({
          courses: res.data.data.info.courses,
        })
      } else {
        Toast.info(res.data.msg)
      }
    })
  }
zhanghaozhe committed
26

zhanghaozhe committed
27
  toCourseDetail = (id) => {
zhanghaozhe committed
28
    const { history } = this.props
zhanghaozhe committed
29 30
    history.push(`/detail?id=${id}`)
  }
zhanghaozhe committed
31

zhanghaozhe committed
32 33 34 35 36 37 38 39 40 41 42 43
  render() {
    const { courses } = this.state
    return (
      <div className="recommendation">
        <div className="title">推荐课程</div>
        <div className="courses">
          {courses.length > 0 &&
            courses.map((item) => {
              const Bottom = (
                <div className="bottom">
                  <span className="price">{item["price1"]}</span>
                  <span className="old-price">{item["price0"]}</span>
zhanghaozhe committed
44
                </div>
zhanghaozhe committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
              )
              return (
                <Course
                  className={"text-overflow-2"}
                  key={item["course_id"]}
                  id={item["course_id"]}
                  img={item["image_name"]}
                  title={item["course_title"]}
                  bottom={Bottom}
                  toDetail={this.toCourseDetail}
                />
              )
            })}
        </div>
      </div>
    )
  }
zhanghaozhe committed
62 63 64
}

export default withRouter(Recommendation)