This repository has been archived by the owner on Sep 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Search.js
220 lines (203 loc) · 5.24 KB
/
Search.js
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
import { Component } from "react";
import { debounce } from "lodash";
import queryString from "query-string";
import * as SwiftypeAppSearch from "swiftype-app-search-javascript";
const client = SwiftypeAppSearch.createClient({
accountHostKey: process.env.REACT_APP_HOST_IDENTIFIER,
apiKey: process.env.REACT_APP_SEARCH_KEY,
engineName: "node-modules"
});
const QUERY_OPTIONS = {
search_fields: {
name: {},
description: {},
keywords: {},
owners: {},
dependencies: {}
},
result_fields: {
id: { raw: {} },
name: {
raw: {},
snippet: {
size: 200,
fallback: true
}
},
version: { raw: {} },
license: { raw: {} },
description: {
snippet: {
size: 200,
fallback: true
}
},
keywords: {
raw: {},
snippet: {
size: 200,
fallback: true
}
},
repository: { raw: {} },
owners: {
raw: {},
snippet: {
size: 200,
fallback: true
}
},
dependencies: {
raw: {},
snippet: {
size: 200,
fallback: true
}
},
homepage: { raw: {} }
},
facets: {
license: {
type: "value",
size: 10
},
keywords: {
type: "value",
size: 10
},
dependencies: {
type: "value",
size: 10
}
}
};
const FILTERS_WHITELIST = ["dependencies", "license", "keywords"];
function getFiltersFromQueryState(queryState) {
return FILTERS_WHITELIST.reduce((acc, filterName) => {
const filterValue = queryState[filterName];
if (!filterValue) return acc;
return acc.concat({ [filterName]: filterValue });
}, []);
}
/*
A simple abstraction to centralize all App Search logic in one place. This uses the
Render Props pattern (https://reactjs.org/docs/render-props.html) and hence makes no assumption
about your view, it simply passes down handlers and data to the app.
In your React app, you may choose to use something like Redux or Mobx to achieve this centralization,
but for simplicity in this Demo, we've chosen to simply keep this in a top-level component.
*/
export default class Search extends Component {
state = {
facets: {},
filters: {},
pageState: {
currentPage: 0,
pageSize: 0,
totalPages: 0,
totalResults: 0
},
query: "",
requestId: "",
results: []
};
updateQuery = query => {
const { history } = this.props;
history.push("?" + queryString.stringify({ q: query }));
};
updatePage = newPage => {
const { history } = this.props;
history.push(
"?" + queryString.stringify({ ...this.getQueryState(), page: newPage })
);
};
updateFromQueryState = () => {
let { q, page } = this.getQueryState();
const filters = getFiltersFromQueryState(this.getQueryState());
q = q || "";
page = parseInt(page, 10);
page = isNaN(page) ? 1 : page;
this.updateResults({ query: q, page: parseInt(page, 10), filters });
};
updateResults = debounce(({ query, page = 1, filters }) => {
client
.search(query, {
...QUERY_OPTIONS,
filters: {
all: filters
},
page: {
size: 10,
current: page
}
})
.then(
resultList => {
this.setState({
facets: resultList.info.facets,
filters: filters.reduce((p, n) => ({ ...p, ...n }), {}),
pageState: {
currentPage: resultList.info.meta.page.current,
pageSize: resultList.info.meta.page.size,
totalPages: resultList.info.meta.page.total_pages,
totalResults: resultList.info.meta.page.total_results
},
query: query,
requestId: resultList.info.meta.request_id,
results: resultList.results
});
},
error => {
console.log(`error: ${error}`);
}
);
}, 200);
trackClick = ({ query, documentId, requestId }) => {
client.click({ query, documentId, requestId });
// We'll return undefined here. We don't necessarily want calling code to wait for a response.
};
// This is so that we don't have to pass `requestId` and `query` deep down
// the tree.
curriedTrackClick = (query, requestId) => documentId => {
this.trackClick({ query, requestId, documentId });
};
getQueryState = () => queryString.parse(this.props.location.search);
componentDidMount() {
this.updateFromQueryState();
}
componentDidUpdate(prevProps) {
if (this.props.location.search !== prevProps.location.search) {
this.updateFromQueryState();
}
}
render() {
const { children } = this.props;
const {
facets,
filters,
pageState,
query,
requestId,
results
} = this.state;
const { q } = this.getQueryState();
const queryClass = query ? "active-search" : "";
return children({
query: q,
queryState: this.getQueryState(),
queryClass: queryClass,
searchActions: {
trackClick: this.curriedTrackClick(query, requestId),
updatePage: this.updatePage,
updateQuery: this.updateQuery
},
searchResults: {
facets,
filters,
pageState,
query,
requestId,
results
}
});
}
}