aboutsummaryrefslogtreecommitdiff
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
parent29338e66b28daee52f7fe5a5cdab49140b3e5a60 (diff)
Make search function
-rw-r--r--src/client/react/reducers/search.js29
-rw-r--r--src/client/views/index.jade2
2 files changed, 24 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 {
diff --git a/src/client/views/index.jade b/src/client/views/index.jade
index 0a057db..9e9d713 100644
--- a/src/client/views/index.jade
+++ b/src/client/views/index.jade
@@ -7,4 +7,6 @@ block content
#root
block scripts
+ script.
+ !{usersStr}
script(src='/bundle.js')