forked from noudadrichem/keycapsets.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.tsx
More file actions
37 lines (33 loc) · 1.08 KB
/
context.tsx
File metadata and controls
37 lines (33 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import React, { createContext, useReducer } from 'react';
import { InititalState, Action, Context, Keycapset, Filters } from 'typings';
import moment from 'moment';
export const INITITAL_STATE: InititalState = {
filters: {
availabilityFilter: 'none',
brandFilter: [],
profileFilter: [],
materialFilter: [],
},
keycapsets: [],
fetchedKeycapsetsLength: 0,
searchQuery: '',
allKeycapsetsCount: 0,
};
const context = createContext<any>(INITITAL_STATE);
const StateProvider = ({ children }) => {
let [state, dispatch]: any[] = useReducer((state: InititalState, action: Action) => {
switch (action.type) {
case 'set':
const newState: InititalState = {
...state,
...action.payload,
};
return newState;
default:
return state;
}
}, INITITAL_STATE);
return <context.Provider value={{ state, dispatch }}>{children}</context.Provider>;
};
export { context, StateProvider };
export default context;