From 36dc0fb88258af24069f61935656334c35ef13b3 Mon Sep 17 00:00:00 2001
From: Noah Loomans <noahloomans@gmail.com>
Date: Sun, 10 Dec 2017 14:23:35 +0100
Subject: Make search function

---
 src/client/react/reducers/search.js | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)

(limited to 'src/client/react')

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 {
-- 
cgit v1.1