-
Notifications
You must be signed in to change notification settings - Fork 1
/
useComponentStore.ts
54 lines (45 loc) · 1.29 KB
/
useComponentStore.ts
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { useRef } from 'react';
import {
ComponentStoreGet,
ComponentStoreSet,
ComponentStoreMethods,
ComponentStore
} from '../interfaces/FlowRouterInterfaces';
export default useComponentStore;
function useComponentStore(step: string): ComponentStoreMethods {
const componentStore = useRef<ComponentStore>(initComponentStore());
const get: ComponentStoreGet = (
dataKey: string,
dfault?: any,
uniqueComponentKey?: string
) => {
if (!uniqueComponentKey) {
uniqueComponentKey = step;
}
return (
(componentStore.current[uniqueComponentKey] &&
componentStore.current[uniqueComponentKey][dataKey]) ??
dfault
);
};
const set: ComponentStoreSet = (dataKey: string, value: any) => {
if (!componentStore.current[step]) {
componentStore.current[step] = {};
}
componentStore.current[step] = {
...componentStore.current[step],
[dataKey]: value
};
};
const reset = () => {
componentStore.current = initComponentStore();
};
return { get, set, reset };
}
function initComponentStore(): ComponentStore {
return {
menu: {},
confirmation: {},
details: {}
};
}