forked from GetStream/stream-react-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch.js
More file actions
222 lines (203 loc) · 4.19 KB
/
Copy pathSearch.js
File metadata and controls
222 lines (203 loc) · 4.19 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import * as axios from 'axios';
import algoliasearch from 'algoliasearch';
import config from 'config';
const algolia = algoliasearch(
config.algolia.appId,
config.algolia.searchOnlyKey,
);
/**
* SEARCH
* @type {string}
*/
export const SEARCH = 'SEARCH_SEARCH';
/**
* _searchRequest
* @param term
* @private
*/
const _searchRequest = term => ({ type: SEARCH, term });
/**
* _searchResponse
* @param term
* @param response
* @private
*/
const _searchResponse = (term, response) => ({ type: SEARCH, term, response });
/**
* search
* Make a search request to algolia based on term (string) and type
* Redux Action
* Reference: http://redux.js.org/docs/basics/Actions.html
* @param term {string} search term
* @param type {string} type of search ('all', 'hashtags', 'location')
* @returns {Function}
*/
export function search(term, type = 'all') {
return dispatch => {
// Initialize the 'cabin' index:
let index = algolia.initIndex('cabin'),
attrs = [];
switch (type) {
case 'all':
attrs.push('first_name', 'last_name', 'location', 'hashtags');
break;
case 'hashtags':
attrs.push('hashtags');
break;
case 'user':
attrs.push('first_name', 'last_name');
break;
case 'location':
attrs.push('location');
break;
}
// Perform Algolia search:
index.search(
term,
{
attributesToHighlight: attrs,
hitsPerPage: 100,
},
(err, content) => {
dispatch(_searchResponse(term, content));
},
);
};
}
/**
* TRIGGER
* @type {string}
*/
export const TRIGGER = 'SEARCH_TRIGGER';
/**
* _triggerRequest
* @param search
* @private
*/
const _triggerRequest = search => ({ type: TRIGGER, search });
/**
* _triggerResponse
* @param search
* @param response
* @private
*/
const _triggerResponse = (search, response) => ({
type: TRIGGER,
search,
response,
});
/**
* trigger
* Triggers search, and posts search data to API
* Redux Action
* Reference: http://redux.js.org/docs/basics/Actions.html
* @param search
* @returns {Function}
*/
export function trigger(search) {
return (dispatch, getState) => {
dispatch(_triggerRequest(search));
/**
* data
* @type {{user_id: *, search: *}}
*/
const data = {
user_id: getState().User.id,
search: search.word,
};
axios
.post(`${config.api.baseUrl}/searches`, data, {
headers: {
Authorization: `Bearer ${localStorage.getItem('jwt')}`,
},
})
.then(res => {
dispatch(_triggerResponse(search, res.data));
});
};
}
/**
* RECENT
* @type {string}
*/
export const RECENT = 'SEARCH_RECENT';
/**
* _recentRequest
* @private
*/
const _recentRequest = () => ({ type: RECENT });
/**
* _recentResponse
* @param response
* @private
*/
const _recentResponse = response => ({ type: RECENT, response });
/**
* recent
* Gets recent searches from API for a given user
* Redux Action
* Reference: http://redux.js.org/docs/basics/Actions.html
* @returns {Function}
*/
export function recent() {
return (dispatch, getState) => {
dispatch(_recentRequest());
axios
.get(
`${config.api.baseUrl}/searches?user_id=${getState().User.id}`,
{
headers: {
Authorization: `Bearer ${localStorage.getItem('jwt')}`,
},
},
)
.then(res => {
dispatch(_recentResponse(res.data));
});
};
}
/**
* RESULTS
* @type {string}
*/
export const RESULTS = 'SEARCH_RESULTS';
/**
* _searchResultsRequest
* @private
*/
const _searchResultsRequest = () => ({ type: RESULTS });
/**
* _searchResultsResponse
* @param response
* @private
*/
const _searchResultsResponse = response => ({ type: RESULTS, response });
/**
* results
* Gets uploads from API based on Algolia Search response
* Redux Action
* Reference: http://redux.js.org/docs/basics/Actions.html
* @param type {string}
* @param query {string}
* @returns {Function}
*/
export function results(type, query) {
return dispatch => {
dispatch(_searchResultsRequest());
axios
.get(
`${config.api
.baseUrl}/uploads?type=${type}&query=${encodeURIComponent(
query,
)}`,
{
headers: {
Authorization: `Bearer ${localStorage.getItem('jwt')}`,
},
},
)
.then(res => {
dispatch(_searchResultsResponse(res.data));
});
};
}