aboutsummaryrefslogtreecommitdiff
path: root/src/client/react
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2017-12-10 14:23:35 +0100
committerNoah Loomans <noahloomans@gmail.com>2017-12-10 14:23:35 +0100
commit36dc0fb88258af24069f61935656334c35ef13b3 (patch)
tree8a6b7e761513c0c7ac1cef96eb9f030deb83ca25 /src/client/react
parent29338e66b28daee52f7fe5a5cdab49140b3e5a60 (diff)
Make search function
Diffstat (limited to 'src/client/react')
-rw-r--r--src/client/react/reducers/search.js29
1 files changed, 22 insertions, 7 deletions
diff --git a/src/client/react/reducers/search.js b/src/client/react/reducers/search.js
index 4e2032d..72fa469 100644
--- a/src/client/react/reducers/search.js
+++ b/src/client/react/reducers/search.js
@@ -1,21 +1,36 @@
+/* global USERS */
+import fuzzy from 'fuzzy';
+
const DEFAULT_STATE = {
searchInput: '',
- searchResults: [],
+ searchResults: [
+ { type: 's', value: '18561' },
+ ],
hasFocus: false,
};
+function getSearchResults(query) {
+ if (query.trim() === '') {
+ return [];
+ }
+
+ const allResults = fuzzy.filter(query, USERS, {
+ extract: user => user.value,
+ });
+
+ const firstResults = allResults.splice(0, 4);
+ const users = firstResults.map(result => result.original);
+
+ return users;
+}
+
const search = (state = DEFAULT_STATE, action) => {
switch (action.type) {
case 'SEARCH/INPUT_CHANGE':
return {
...state,
searchInput: action.typedValue,
- searchResults: [
- { type: 's', value: '18561' },
- { type: 'c', value: '5H2' },
- { type: 't', value: 'akh' },
- { type: 'r', value: '008-mk' },
- ],
+ searchResults: getSearchResults(action.typedValue),
};
case 'SEARCH/FOCUS_CHANGE':
return {