-
Notifications
You must be signed in to change notification settings - Fork 0
/
Store.tsx
71 lines (59 loc) · 1.77 KB
/
Store.tsx
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import {useState, useCallback} from 'react';
import {createContainer} from 'react-tracked';
import {produce, Draft} from 'immer'; // eslint-disable-line no-unused-vars
import {AsyncStorage} from 'react-native';
/*
The store contains all data types and functions related to the global state of the React app,
it defines what the global state contains and what the initial value is
*/
type State = {
registered: boolean;
loggedIn: boolean;
ID: string;
attester: boolean;
serverURL: string;
darkMode: boolean;
jwt: string;
};
type Certificate = {
creatorID: string;
holderID: string;
type: string;
hash?: string; // only available once the data has been double attested
};
type OutstandingRequest = {
creatorID: string;
type: string;
};
let darkmode = false;
AsyncStorage.getItem('darkmode_enabled', (error, result) => {
darkmode = result === 'true';
});
let registered = false;
AsyncStorage.getItem('registered', (error, result) => {
registered = result === 'true';
});
const defaultState: State = {
registered: registered,
loggedIn: false,
attester: false,
ID: '0',
serverURL: 'http://localhost:8085',
darkMode: darkmode,
jwt: '',
};
// attributeTypeMap relates the numerical value used in the backend to the text used in the frontend
const attributeTypeMap = [{value: 'COVID-19 Immunity Certificate'}];
const useValue = () => useState(defaultState);
const {Provider, useTrackedState, useUpdate} = createContainer(useValue);
const useSetDraft = () => {
const setState = useUpdate();
return useCallback(
(draftUpdater: (draft: Draft<State>) => void) => {
setState(produce(draftUpdater));
},
[setState],
);
};
export {Certificate, State, OutstandingRequest, attributeTypeMap};
export {Provider, useTrackedState, useSetDraft};