-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
173 lines (160 loc) · 4.68 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import React, { useEffect } from "react";
import { AppearanceProvider } from "react-native-appearance";
import Snackbar from 'react-native-snackbar';
import LoginScreen from "./src/screen/employee/account/LoginScreen";
import RegisterScreen from "./src/screen/employee/account/RegisterScreen";
import CustomerPath from "./src/screen/paths/CustomerPath";
import EmployeePath from "./src/screen/paths/EmployeePath";
import SplashScreen from "./src/screen/SplashScreen";
import * as api from "./src/service/BarApiService.js";
import * as storage from "./src/service/BarStorageService";
import { AuthContext } from "./src/service/Context.js";
import { ThemeProvider } from "./src/theme/ThemeManager";
const SignInNavigator = createStackNavigator();
export function SignInStack({ state }) {
return (
<NavigationContainer>
<SignInNavigator.Navigator headerMode="none">
<SignInNavigator.Screen
name="Login"
component={LoginScreen}
initialParams={{ context: AuthContext }}
/>
<SignInNavigator.Screen
name="Register"
component={RegisterScreen}
initialParams={{ context: AuthContext }}
/>
<SignInNavigator.Screen
name="App"
component={EmployeePath}
initialParams={{ context: AuthContext }}
/>
</SignInNavigator.Navigator>
</NavigationContainer>
);
}
export default function App() {
const [bars, setBars] = React.useState([]);
const [state, dispatch] = React.useReducer(
(prevState, action) => {
switch (action.type) {
case "RESTORE_TOKEN":
return {
...prevState,
userToken: action.token,
isLoading: false,
};
case "SIGN_IN":
return {
...prevState,
isSignout: false,
userToken: action.token,
};
case "SIGN_OUT":
return {
...prevState,
isSignout: true,
userToken: null,
};
}
},
{
isLoading: true,
isSignout: false,
userToken: null,
},
);
useEffect(() => {
const bootstrapAsync = async () => {
let userToken = null;
try {
userToken = await storage.getAccessToken();
} catch (e) {
userToken = null;
}
dispatch({ type: "RESTORE_TOKEN", token: userToken });
};
getBars();
bootstrapAsync();
}, []);
const authContext = React.useMemo(
() => ({
signIn: async (data) => {
let accessToken;
try {
accessToken = await api.login(data.email, data.password);
const bars = await api.getBars();
if (bars[0] !== undefined) {
await storage.storeActiveBar(bars[0].id.toString());
}
dispatch({ type: "SIGN_IN", token: accessToken });
} catch (error) {
Snackbar.show({text: error, duration: Snackbar.LENGTH_SHORT})
}
},
signOut: async () => {
await api.logout();
dispatch({ type: "SIGN_OUT" });
},
signUp: async (data) => {
api
.signUp(
data.email,
data.userName,
data.password,
data.firstName,
data.lastName,
)
.then(() => {
let accessToken = api
.login(data.email, data.password)
.catch((error) => Snackbar.show({text: error.message, duration: Snackbar.LENGTH_SHORT}));
dispatch({ type: "SIGN_IN", token: accessToken });
})
.catch((error) => {
Snackbar.show({text: error.message, duration: Snackbar.LENGTH_SHORT});
});
},
}),
[],
);
const getBars = () => {
api
.getBars()
.then((json) => {
setBars(json);
})
.catch("No bars found");
};
if (state.isLoading) {
// We haven't finished checking for the token yet
return (
<ThemeProvider>
<SplashScreen />
</ThemeProvider>
);
}
return (
<AppearanceProvider>
<ThemeProvider>
<AuthContext.Provider value={authContext}>
{/* {state.userToken !== null && bars.length > 0 ? (
<EmployeePath state={state} />
) : state.userToken !== null && bars.length === 0 ? (
<CustomerPath />
) : (
<SignInStack />
)} */}
{state.userToken !== null ? (
<EmployeePath state={state} />
) : (
<SignInStack />
)}
</AuthContext.Provider>
</ThemeProvider>
</AppearanceProvider>
);
}