summaryrefslogtreecommitdiffstats
path: root/web/react/components/search_results.jsx
blob: 6ee8171714cc4210aa844ae3b585fbd46060780c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

var PostStore = require('../stores/post_store.jsx');
var UserStore = require('../stores/user_store.jsx');
var SearchBox = require('./search_bar.jsx');
var utils = require('../utils/utils.jsx');
var SearchResultsHeader = require('./search_results_header.jsx');
var SearchResultsItem = require('./search_results_item.jsx');

function getStateFromStores() {
    return {results: PostStore.getSearchResults()};
}

export default class SearchResults extends React.Component {
    constructor(props) {
        super(props);

        this.mounted = false;

        this.onChange = this.onChange.bind(this);
        this.resize = this.resize.bind(this);

        this.state = getStateFromStores();
    }

    componentDidMount() {
        this.mounted = true;
        PostStore.addSearchChangeListener(this.onChange);
        this.resize();
        var self = this;
        $(window).resize(function resize() {
            self.resize();
        });
    }

    componentDidUpdate() {
        this.resize();
    }

    componentWillUnmount() {
        PostStore.removeSearchChangeListener(this.onChange);
        this.mounted = false;
    }

    onChange() {
        if (this.mounted) {
            var newState = getStateFromStores();
            if (!utils.areStatesEqual(newState, this.state)) {
                this.setState(newState);
            }
        }
    }

    resize() {
        var height = $(window).height() - $('#error_bar').outerHeight() - 100;
        $('#search-items-container').css('height', height + 'px');
        $('#search-items-container').scrollTop(0);
        $('#search-items-container').perfectScrollbar();
    }

    render() {
        var results = this.state.results;
        var currentId = UserStore.getCurrentId();
        var searchForm = null;
        if (currentId) {
            searchForm = <SearchBox />;
        }
        var noResults = (!results || !results.order || !results.order.length);
        var searchTerm = PostStore.getSearchTerm();

        var ctls = null;

        if (noResults) {
            ctls = <div className='sidebar--right__subheader'>No results</div>;
        } else {
            ctls = results.order.map(function mymap(id) {
                var post = results.posts[id];
                return (
                    <SearchResultsItem
                        key={post.id}
                        post={post}
                        term={searchTerm}
                        isMentionSearch={this.props.isMentionSearch}
                    />
                );
            }, this);
        }

        return (
            <div className='sidebar--right__content'>
                <div className='search-bar__container sidebar--right__search-header'>{searchForm}</div>
                <div className='sidebar-right__body'>
                    <SearchResultsHeader isMentionSearch={this.props.isMentionSearch} />
                    <div
                        id='search-items-container'
                        className='search-items-container'
                    >
                        {ctls}
                    </div>
                </div>
            </div>
        );
    }
}

SearchResults.propTypes = {
    isMentionSearch: React.PropTypes.bool
};