aboutsummaryrefslogtreecommitdiff
path: root/src/client/react/reducers
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2017-12-21 12:06:41 +0100
committerNoah Loomans <noahloomans@gmail.com>2017-12-21 12:06:41 +0100
commitf0c8cf0e79f003514fd65a70def5820205955a77 (patch)
treecb66d325fb5d16d8b7fa0f14c91ad17dd4ff7c6c /src/client/react/reducers
parent569b2969d530f08e55798c5cb3079948c7c037cd (diff)
Move to typescript
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);