summaryrefslogtreecommitdiffstats
path: root/web/react/components/search_results.jsx
blob: b1efd7685465504a921ca8398d4facd0b1f2b770 (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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.


var PostStore = require('../stores/post_store.jsx');
var ChannelStore = require('../stores/channel_store.jsx');
var UserStore = require('../stores/user_store.jsx');
var UserProfile = require( './user_profile.jsx' );
var SearchBox =require('./search_bar.jsx');
var utils = require('../utils/utils.jsx');
var client = require('../utils/client.jsx');
var AsyncClient = require('../utils/async_client.jsx');
var AppDispatcher = require('../dispatcher/app_dispatcher.jsx');
var Constants = require('../utils/constants.jsx');
var ActionTypes = Constants.ActionTypes;

var RhsHeaderSearch = React.createClass({
    handleClose: function(e) {
        e.preventDefault();

        AppDispatcher.handleServerAction({
            type: ActionTypes.RECIEVED_SEARCH,
            results: null
        });

        AppDispatcher.handleServerAction({
            type: ActionTypes.RECIEVED_SEARCH_TERM,
            term: null,
            do_search: false,
            is_mention_search: false
        });

        AppDispatcher.handleServerAction({
            type: ActionTypes.RECIEVED_POST_SELECTED,
            results: null
        });
    },
    render: function() {
        var title = this.props.isMentionSearch ? "Recent Mentions" : "Search Results";
        return (
            <div className="sidebar--right__header">
                <span className="sidebar--right__title">{title}</span>
                <button type="button" className="sidebar--right__close" aria-label="Close" title="Close" onClick={this.handleClose}></button>
            </div>
        );
    }
});

var SearchItem = React.createClass({
    handleClick: function(e) {
        e.preventDefault();

        var self = this;

        client.getPost(
            this.props.post.channel_id,
            this.props.post.id,
            function(data) {

                AppDispatcher.handleServerAction({
                    type: ActionTypes.RECIEVED_POST_SELECTED,
                    post_list: data,
                    from_search: PostStore.getSearchTerm()
                });

                AppDispatcher.handleServerAction({
                    type: ActionTypes.RECIEVED_SEARCH,
                    results: null,
                    is_mention_search: self.props.isMentionSearch
                });
            },
            function(err) {
                AsyncClient.dispatchError(err, "getPost");
            }
        );

        var postChannel = ChannelStore.get(this.props.post.channel_id);
        var teammate = postChannel.type === 'D' ? utils.getDirectTeammate(this.props.post.channel_id).username : "";

        utils.switchChannel(postChannel, teammate);
    },

    render: function() {

        var message = utils.textToJsx(this.props.post.message, {searchTerm: this.props.term, noMentionHighlight: !this.props.isMentionSearch});
        var channelName = "";
        var channel = ChannelStore.get(this.props.post.channel_id);
        var timestamp = UserStore.getCurrentUser().update_at;

        if (channel) {
            channelName = (channel.type === 'D') ? "Private Message" : channel.display_name;
        }

        return (
            <div className="search-item-container post" onClick={this.handleClick}>
                <div className="search-channel__name">{ channelName }</div>
                <div className="post-profile-img__container">
                    <img className="post-profile-img" src={"/api/v1/users/" + this.props.post.user_id + "/image?time=" + timestamp} height="36" width="36" />
                </div>
                <div className="post__content">
                    <ul className="post-header">
                        <li className="post-header-col"><strong><UserProfile userId={this.props.post.user_id} /></strong></li>
                        <li className="post-header-col">
                            <time className="search-item-time">
                                { utils.displayDate(this.props.post.create_at) + ' ' + utils.displayTime(this.props.post.create_at) }
                            </time>
                        </li>
                    </ul>
                    <div className="search-item-snippet"><span>{message}</span></div>
                </div>
            </div>
        );
    }
});


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

module.exports = React.createClass({
    displayName: 'SearchResults',
    componentDidMount: function() {
        PostStore.addSearchChangeListener(this._onChange);
        this.resize();
        var self = this;
        $(window).resize(function(){
            self.resize();
        });
    },
    componentDidUpdate: function() {
        this.resize();
    },
    componentWillUnmount: function() {
        PostStore.removeSearchChangeListener(this._onChange);
    },
    _onChange: function() {
        if (this.isMounted()) {
            var newState = getStateFromStores();
            if (!utils.areStatesEqual(newState, this.state)) {
                this.setState(newState);
            }
        }
    },
    getInitialState: function() {
        return getStateFromStores();
    },
    resize: function() {
        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: function() {

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

        return (
            <div className="sidebar--right__content">
                <div className="search-bar__container sidebar--right__search-header">{searchForm}</div>
                <div className="sidebar-right__body">
                    <RhsHeaderSearch isMentionSearch={this.props.isMentionSearch} />
                    <div id="search-items-container" className="search-items-container">

                        { noResults ? <div className="sidebar--right__subheader">No results</div>
                                    : results.order.map(function(id) {
                                          var post = results.posts[id];
                                          return <SearchItem key={post.id} post={post} term={searchTerm} isMentionSearch={this.props.isMentionSearch} />
                                    }, this)
                        }

                    </div>
                </div>
            </div>
        );
    }
});