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


const ForwardRefSearchHead = React.forwardRef((props, ref) => {
    return <SearchHeader {...props} forwardedRef={ref}/>
})
zhanghaozhe committed
13 14 15 16 17 18 19 20 21

const Bottom = ({item}) => {
    return (
        <div className='bottom'>
            <span className='price'>¥{item.price1}</span>
            <span className='stale-price'>¥{item.price0}</span>
        </div>
    )
}
zhanghaozhe committed
22 23

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

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

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

    componentWillUnmount() {
        document.removeEventListener('scroll', this.handleScroll)
zhanghaozhe committed
47 48 49 50
    }


    getCourses = (word) => {
zhanghaozhe committed
51
        http.get(`${API['search-api']}/search/${word}?type=course&page=1`)
zhanghaozhe committed
52 53
            .then(res => {
                const data = res.data
zhanghaozhe committed
54

zhanghaozhe committed
55 56
                if (data.errno === 0) {
                    this.setState({
zhanghaozhe committed
57
                        courseList: data.data.info['search_data'].course
zhanghaozhe committed
58 59 60
                    });
                }
            })
zhanghaozhe committed
61 62
    }

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

    handleSearch = () => {
zhanghaozhe committed
68
        this.state.value && this.getCourses(this.state.value)
zhanghaozhe committed
69 70 71 72 73 74
    }

    handleChange = value => {
        this.setState({value})
    }

75
    toCourseDetail = (id) => {
zhanghaozhe committed
76 77
        const {history} = this.props;
        history.push(`/detail?id=${id}`)
78
    }
zhanghaozhe committed
79

zhanghaozhe committed
80
    handleScroll = throttle(() => {
FE committed
81 82 83 84
        let y = window.scrollY < 0? 0 : window.scrollY, 
            headY = this.searchHead.current.offsetTop,
            h = this.searchHead.current.offsetHeight;
        if(y > this.prevScrollY) {
zhanghaozhe committed
85
            this.setState({
zhanghaozhe committed
86
                searchHeadStyle: {
FE committed
87
                    top : `${-h}px`
zhanghaozhe committed
88
                }
FE committed
89
            });
zhanghaozhe committed
90
        }
FE committed
91 92 93 94 95 96 97 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
        if(y < this.prevScrollY) {
            this.setState({
                searchHeadStyle: {
                    top: 0
                }
            });
        }
        // 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`
        //         }
        //     })
        // }
FE committed
135
        this.prevScrollY = y;
zhanghaozhe committed
136
    }, 0)
zhanghaozhe committed
137

zhanghaozhe committed
138
    render() {
FE committed
139
        const { courseList, isHide } = this.state;
zhanghaozhe committed
140

zhanghaozhe committed
141
        return (
zhanghaozhe committed
142
            <div
zhanghaozhe committed
143
                className={'search-result'}>
zhanghaozhe committed
144
                <ForwardRefSearchHead
zhanghaozhe committed
145 146 147 148
                    handleSearch={this.handleSearch}
                    value={this.state.value}
                    handleChange={this.handleChange}
                    searchHistory={this.state.searchHistory}
zhanghaozhe committed
149 150
                    style={this.state.searchHeadStyle}
                    ref={this.searchHead}
FE committed
151
                    isHide={isHide}
zhanghaozhe committed
152
                />
zhanghaozhe committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
                {

                    courseList && courseList.length > 0 ?
                        <ul>
                            {
                                courseList.map(item => {
                                    const Info = (
                                        <div className="info">
                                            <p className='title'>{item.course_title}</p>
                                            <p className='des'>{item.simpledescription}</p>
                                            <Bottom
                                                item={item}
                                            />
                                        </div>
                                    )
                                    const status = (
                                        (item['bargain_num'] || item['groupon_num']) ?
                                            <div
zhanghaozhe committed
171
                                                className='status'>
zhanghaozhe committed
172 173 174 175 176 177 178
                                                {
                                                    item['bargain_num'] === 0 ? `砍价减${item['groupon_num']}元` : `拼团减${item['bargain_num']}元`
                                                }
                                            </div>
                                            : null
                                    )
                                    return (
179 180 181 182 183 184 185
                                        <VList
                                            img={item.image_name}
                                            toDetail={this.toCourseDetail}
                                            key={item.course_id}
                                            info={Info}
                                            id={item['course_id']}
                                            status={status}
zhanghaozhe committed
186
                                        />
zhanghaozhe committed
187 188 189 190 191 192 193 194
                                    )
                                })
                            }
                        </ul>
                        : <div className="empty">
                            抱歉!没有搜到相关内容
                        </div>
                }
zhanghaozhe committed
195 196
                <Recommendation/>

zhanghaozhe committed
197 198 199 200 201
            </div>
        );
    }
}

zhanghaozhe committed
202

zhanghaozhe committed
203
export default SearchResult;