From 36dc0fb88258af24069f61935656334c35ef13b3 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Sun, 10 Dec 2017 14:23:35 +0100 Subject: Make search function --- src/client/react/reducers/search.js | 29 ++++++++++++++++++++++------- src/client/views/index.jade | 2 ++ 2 files changed, 24 insertions(+), 7 deletions(-) (limited to 'src/client') 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') -- cgit v1.1