diff options
Diffstat (limited to 'src/client/react/reducers')
-rw-r--r-- | src/client/react/reducers/search.test.ts (renamed from src/client/react/reducers/search.test.js) | 2 | ||||
-rw-r--r-- | src/client/react/reducers/search.ts (renamed from src/client/react/reducers/search.js) | 23 |
2 files changed, 17 insertions, 8 deletions
diff --git a/src/client/react/reducers/search.test.js b/src/client/react/reducers/search.test.ts index e0ca18e..5869b81 100644 --- a/src/client/react/reducers/search.test.js +++ b/src/client/react/reducers/search.test.ts @@ -1,4 +1,4 @@ -window.USERS = [ +(<any>window).USERS = [ { type: 's', value: '18561' }, { type: 's', value: '18562' }, { type: 's', value: '18563' }, diff --git a/src/client/react/reducers/search.js b/src/client/react/reducers/search.ts index 2a7e7a5..658d3ca 100644 --- a/src/client/react/reducers/search.js +++ b/src/client/react/reducers/search.ts @@ -1,7 +1,16 @@ -import fuzzy from 'fuzzy'; -import users from '../users'; +import * as fuzzy from 'fuzzy'; +import users, { User } from '../users'; +import { InputChangeAction, ChangeSelectedResultAction } from '../actions/search'; + +export interface State { + results: string[], + selectedResult: string | null, + isExactMatch: boolean, +}; + +export type Action = InputChangeAction | ChangeSelectedResultAction; -const DEFAULT_STATE = { +const DEFAULT_STATE: State = { results: [ 's/18562', ], @@ -9,22 +18,22 @@ const DEFAULT_STATE = { isExactMatch: false, }; -function getSearchResults(allUsers, query) { +function getSearchResults(allUsers: User[], query: string) { if (query.trim() === '') { return []; } const allResults = fuzzy.filter(query, allUsers, { - extract: user => user.value, + extract: (user: User) => user.value, }); const firstResults = allResults.splice(0, 4); - const userIds = firstResults.map(result => result.original.id); + const userIds = firstResults.map((result: { original: User }) => result.original.id); return userIds; } -const search = (state = DEFAULT_STATE, action) => { +const search = (state = DEFAULT_STATE, action: Action): State => { switch (action.type) { case 'SEARCH/INPUT_CHANGE': { const results = getSearchResults(users.allUsers, action.typedValue); |