index.js 4.66 KB
Newer Older
zhanghaozhe committed
1
import React, { PureComponent } from 'react';
baiguangyao committed
2
import SearchHead from './searchHead'
3
import Tag from '@common/Tag/index.js'
zhanghaozhe committed
4
import { http } from '@/utils'
baiguangyao committed
5
import './index.scss';
zhanghaozhe committed
6
import { Link } from 'react-router-dom'
zhanghaozhe committed
7
import Loading from '@/common/Loading'
baiguangyao committed
8 9


zhanghaozhe committed
10
class Search extends PureComponent {
zhanghaozhe committed
11 12 13 14

    state = {
        searchHistory: JSON.parse(localStorage.getItem('searchHistory')) || [],
        hot_words: [],
FE committed
15
        defaultWord: '',
zhanghaozhe committed
16
        searchList: [],
zhanghaozhe committed
17 18
        value: '',
        isLoading: true
baiguangyao committed
19
    }
zhanghaozhe committed
20 21

    async componentDidMount() {
FE committed
22 23 24
        const res = await http.get(`${API['search-api']}/search_hot_word`);
        const { errno, data } = res.data; 
        if (errno === 0) {
zhanghaozhe committed
25
            this.setState({
FE committed
26
                hot_words: data.info.hot_words,
27
                value: data.info.recommend_word,
zhanghaozhe committed
28
                isLoading: false
zhanghaozhe committed
29 30 31 32
            })
        }
    }

zhanghaozhe committed
33 34 35 36 37 38 39 40 41 42 43 44
    clearHistory = () => {
        localStorage.setItem('searchHistory', null)
        this.setState({
            searchHistory: []
        })
    }

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

    handleSearch = () => {
FE committed
45 46 47
        const { defaultWord, value } = this.state;
        const val = value || defaultWord;
        val && this.props.history.push(`/search-result?word=${encodeURIComponent(val)}`)
zhanghaozhe committed
48 49 50
    }

    storeHistory = keyword => {
FE committed
51 52 53
        const { searchHistory } = this.state;
        const data = searchHistory.some(item => item === keyword)? searchHistory : searchHistory.concat([keyword]);
        localStorage.setItem('searchHistory', JSON.stringify(data));
zhanghaozhe committed
54 55 56
    }


baiguangyao committed
57
    render() {
zhanghaozhe committed
58
        const {searchHistory} = this.state
baiguangyao committed
59 60
        return (
            <div className="search-page">
zhanghaozhe committed
61 62
                <SearchHead
                    searchHistory={this.state.searchHistory}
63
                    value={this.state.value}
zhanghaozhe committed
64 65 66
                    handleChange={this.handleChange}
                    handleSearch={this.handleSearch}
                />
zhanghaozhe committed
67 68
                <Loading isLoading={this.state.isLoading}>
                    <div className="search-main">
zhanghaozhe committed
69 70 71 72 73 74 75 76 77 78
                        {
                            searchHistory.length > 0 &&
                            <div className="search-land">
                                <div className='search-history'>
                                    <span>最近搜索</span>
                                    <i className="iconfont iconiconfront-56" onClick={this.clearHistory}/>
                                </div>
                                <div className="search-tag">
                                    {

zhanghaozhe committed
79 80 81 82 83 84
                                        searchHistory.map((v, i) => {
                                            return (
                                                <Link
                                                    key={i}
                                                    to={`/search-result?word=${encodeURIComponent(v)}`}
                                                >
xuzhenghua committed
85
                                                    <Tag name={"tagLately"}>{v}</Tag>
zhanghaozhe committed
86 87 88
                                                </Link>
                                            )
                                        })
zhanghaozhe committed
89 90
                                    }
                                </div>
zhanghaozhe committed
91
                            </div>
zhanghaozhe committed
92
                        }
zhanghaozhe committed
93
                        <div className="search-land search-hot">
xuzhenghua committed
94
                            <label className={'search-history'}>
zhanghaozhe committed
95 96 97 98 99 100 101 102 103 104 105
                                <span>热门搜索</span>
                            </label>
                            <div className="search-tag">
                                {
                                    this.state['hot_words'].length > 0 ?
                                        this.state['hot_words'].map((v, i) => {
                                            return (
                                                <Link key={i}
                                                      to={`/search-result?word=${encodeURIComponent(v)}`}
                                                      onClick={this.storeHistory.bind(this, v)}
                                                >
xuzhenghua committed
106
                                                    <Tag name={"tagHot"}>{v}</Tag>
zhanghaozhe committed
107 108 109 110 111
                                                </Link>
                                            )
                                        })
                                        : <div style={{textAlign: 'center', padding: '20px'}}>暂无热门</div>
                                }
xuzhenghua committed
112
                                
zhanghaozhe committed
113
                            </div>
baiguangyao committed
114 115
                        </div>
                    </div>
zhanghaozhe committed
116
                </Loading>
zhanghaozhe committed
117

baiguangyao committed
118 119 120 121 122 123 124
            </div>
        )
    }

}

export default Search;