-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.js
111 lines (105 loc) · 2.92 KB
/
App.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import * as React from "react";
import { View, AsyncStorage } from "react-native";
import Routes from "./routes";
import { AuthContext } from "./hooks";
import { WebService } from "./services";
import { sortNotaries } from "./utils";
import { assignConfig } from "./constants";
function App() {
const [state, dispatch] = React.useReducer(
(prevState, action) => {
switch (action.type) {
case "RESTORE_TOKEN":
return {
...prevState,
userMobileNumber: action.token,
isLoading: false,
};
case "SIGN_IN":
return {
...prevState,
isSignout: false,
userMobileNumber: action.token,
};
case "SIGN_OUT":
return {
...prevState,
isSignout: true,
userMobileNumber: null,
};
case "SET_ALL_NOTARIES":
return {
...prevState,
allNotaries: action.allNotaries,
backupNotaries: action.allNotaries,
};
case "SET_ONLY_ALL_NOTARIES":
return {
...prevState,
allNotaries: action.allNotaries,
};
case "TOGGLE_FETCH_LOADING":
return {
...prevState,
isFetching: action.isFetching,
};
}
},
{
isLoading: true,
isSignout: false,
userMobileNumber: null,
allNotaries: [],
backupNotaries: [],
isFetching: false,
}
);
React.useEffect(() => {
setTimeout(() => {
_retrieveData();
}, 2000);
}, []);
const _retrieveData = async () => {
try {
const value = await AsyncStorage.getItem("mobileNumber");
dispatch({ type: "RESTORE_TOKEN", token: value });
} catch (error) {
// Error retrieving data
}
};
const authContext = React.useMemo(
() => ({
signIn: (data) => {
dispatch({ type: "SIGN_IN", token: data });
},
signOut: () => {
dispatch({ type: "SET_ALL_NOTARIES", allNotaries: [] });
dispatch({ type: "SIGN_OUT" });
},
fetchNotaryItem: async (userMobileNumber) => {
dispatch({ type: "TOGGLE_FETCH_LOADING", isFetching: true });
const allNotaries = await WebService.getNotaryItemsByNumber(
userMobileNumber
);
console.log(allNotaries);
const finalSort = allNotaries.notaries
? sortNotaries(allNotaries.notaries)
: [];
dispatch({ type: "SET_ALL_NOTARIES", allNotaries: finalSort });
dispatch({ type: "TOGGLE_FETCH_LOADING", isFetching: false });
},
setAllNotaries: (allNotaries) => {
dispatch({ type: "SET_ONLY_ALL_NOTARIES", allNotaries });
},
}),
[]
);
return (
<View style={{ flex: 1 }}>
<AuthContext.Provider value={{ authContext, state }}>
<Routes state={state} />
</AuthContext.Provider>
</View>
);
}
export default App;