search-result.js 5.92 KB
Newer Older
zhanghaozhe committed
1 2 3 4 5 6 7
import React, { PureComponent } from "react"
import SearchHeader from "./searchHead"
import VList from "src/common/VList"
import { http, getParam } from "src/utils"
import "./search-result.scss"
import Recommendation from "./recommendation"
import throttle from "lodash/throttle"
zhanghaozhe committed
8 9

const ForwardRefSearchHead = React.forwardRef((props, ref) => {
zhanghaozhe committed
10
  return <SearchHeader {...props} forwardedRef={ref} />
zhanghaozhe committed
11
})
zhanghaozhe committed
12

zhanghaozhe committed
13 14 15 16 17 18 19
const Bottom = ({ item }) => {
  return (
    <div className="bottom">
      <span className="price">¥{item.price1}</span>
      <span className="stale-price">¥{item.price0}</span>
    </div>
  )
zhanghaozhe committed
20
}
zhanghaozhe committed
21 22

class SearchResult extends PureComponent {
zhanghaozhe committed
23 24
  prevScrollY = 0
  searchHead = React.createRef()
zhanghaozhe committed
25 26
  swipeUp = "up"
  swipeDown = "down"
zhanghaozhe committed
27 28 29

  state = {
    courseList: [],
zhanghaozhe committed
30 31
    value: decodeURIComponent(getParam("word")) || "",
    searchHistory: JSON.parse(localStorage.getItem("searchHistory")) || [],
zhanghaozhe committed
32 33 34 35 36 37 38 39 40
    fixedHeader: false,
    searchHeadStyle: { top: 0 },
    swipeDirection: this.swipeUp,
    isHide: false,
    basicTop: 0,
    resultCount: 0,
  }

  componentDidMount() {
zhanghaozhe committed
41 42
    this.getCourses(getParam("word"))
    document.addEventListener("scroll", this.handleScroll)
zhanghaozhe committed
43 44 45
  }

  componentWillUnmount() {
zhanghaozhe committed
46
    document.removeEventListener("scroll", this.handleScroll)
zhanghaozhe committed
47 48
  }

zhanghaozhe committed
49 50 51 52 53 54 55 56 57 58 59 60 61
  getCourses = (word) => {
    http
      .get(`${API["search-api"]}/search/${word}?type=v2-course&page=1`)
      .then((res) => {
        const data = res.data

        if (data.errno === 0) {
          this.setState({
            courseList: data.data.info["search_data"].course,
            resultCount: data.data.info["search_data"].num,
          })
        }
      })
zhanghaozhe committed
62 63
  }

zhanghaozhe committed
64
  handleClick = (id) => {
zhanghaozhe committed
65 66 67 68 69 70 71
    this.props.history.push(`/detail?id=${id}`)
  }

  handleSearch = () => {
    this.state.value && this.getCourses(this.state.value)
  }

zhanghaozhe committed
72
  handleChange = (value) => {
zhanghaozhe committed
73 74 75
    this.setState({ value })
  }

zhanghaozhe committed
76
  toCourseDetail = (id) => {
zhanghaozhe committed
77 78 79 80 81 82 83 84 85 86 87 88 89
    const { history } = this.props
    history.push(`/detail?id=${id}`)
  }

  handleScroll = throttle(() => {
    let y = window.scrollY < 0 ? 0 : window.scrollY,
      h = this.searchHead.current.offsetHeight
    if (y > this.prevScrollY) {
      this.setState({
        searchHeadStyle: {
          top: `${-h}px`,
        },
      })
zhanghaozhe committed
90
    }
zhanghaozhe committed
91 92 93 94 95 96
    if (y < this.prevScrollY) {
      this.setState({
        searchHeadStyle: {
          top: 0,
        },
      })
zhanghaozhe committed
97
    }
zhanghaozhe committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    // if (y < this.prevScrollY) {
    //     if (this.state.swipeDirection === this.swipeDown) {
    //         y <= headY && this.state.searchHeadStyle.position !== 'fixed' &&
    //         this.setState({
    //             searchHeadStyle: {
    //                 top: `0`,
    //                 position: 'fixed'
    //             }
    //         })
    //     } else {
    //         this.setState({
    //             swipeDirection: this.swipeDown
    //         }, () => {
    //             if (this.state.swipeDirection === this.swipeDown) {
    //                 let h = y > document.querySelector('body').offsetHeight? document.querySelector('body').offsetHeight: y;
    //                 let h1 = this.searchHead.current.offsetHeight
    //                     this.setState({
    //                         searchHeadStyle: {
    //                             // top: `${h > headY ? h - h1 : h}px`
    //                             top: `${y}px`
    //                         }
    //                     })

    //             }
    //         })
    //     }
    // } else {
    //     this.state.swipeDirection !== this.swipeUp &&
    //     this.setState({
    //         swipeDirection: this.swipeUp,
    //         searchHeadStyle: {
    //             position: 'absolute',
    //             top: `${y}px`
    //         }
    //     })
    // }
    this.prevScrollY = y
  }, 0)

  render() {
    const { courseList, isHide, resultCount } = this.state
zhanghaozhe committed
139

zhanghaozhe committed
140
    return (
zhanghaozhe committed
141
      <div className={"search-result"}>
zhanghaozhe committed
142 143 144 145 146 147 148 149 150
        <ForwardRefSearchHead
          handleSearch={this.handleSearch}
          value={this.state.value}
          handleChange={this.handleChange}
          searchHistory={this.state.searchHistory}
          style={this.state.searchHeadStyle}
          ref={this.searchHead}
          isHide={isHide}
        />
zhanghaozhe committed
151 152 153
        {resultCount > 0 && (
          <div className="result-count">{resultCount}个结果</div>
        )}
zhanghaozhe committed
154 155 156 157 158 159 160 161 162
        {courseList && courseList.length > 0 ? (
          <ul>
            {courseList.map((item, index) => {
              const Info = (
                <div className="info">
                  <p
                    className="title text-overflow-2"
                    dangerouslySetInnerHTML={{
                      __html:
zhanghaozhe committed
163 164 165
                        item.highlight?.course_title?.length > 0
                          ? item.highlight.course_title[0]
                          : item.course_title,
zhanghaozhe committed
166 167
                    }}
                  ></p>
zhanghaozhe committed
168 169 170
                  <p className="des text-overflow-1">
                    {item.simpledescription}
                  </p>
zhanghaozhe committed
171 172 173 174
                  <Bottom item={item} />
                </div>
              )
              const status =
zhanghaozhe committed
175
                item["bargain_num"] || item["groupon_num"] ? (
zhanghaozhe committed
176
                  <div className="status">
zhanghaozhe committed
177 178 179
                    {item["bargain_num"] === 0
                      ? `砍价减${item["groupon_num"]}元`
                      : `拼团减${item["bargain_num"]}元`}
zhanghaozhe committed
180 181 182 183 184 185 186 187
                  </div>
                ) : null
              return (
                <VList
                  img={item.image_name}
                  toDetail={this.toCourseDetail}
                  key={index}
                  info={Info}
zhanghaozhe committed
188
                  id={item["course_id"]}
zhanghaozhe committed
189
                  status={status}
zhanghaozhe committed
190
                />
zhanghaozhe committed
191 192 193 194 195
              )
            })}
          </ul>
        ) : (
          <div className="empty">
zhanghaozhe committed
196
            <img src={require("./image/ss_empty.png")} alt="" />
zhanghaozhe committed
197 198 199 200 201 202 203
            抱歉,没有搜到相关内容!
          </div>
        )}
        <Recommendation />
      </div>
    )
  }
zhanghaozhe committed
204 205
}

zhanghaozhe committed
206
export default SearchResult