-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
61 lines (53 loc) · 1.56 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
import React, { useEffect } from "react";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { AuthStatus, AuthProvider } from "./Services/AuthContext";
import AppNavigator from "./AppNavigator";
import useAuth from "./Services/auth.services";
import { Provider } from "react-redux";
import { store } from "./Services/Redux/store";
import { useDispatch, useSelector } from "react-redux";
const App = () => {
return (
<Provider store={store}>
<AuthProvider>
<MainApp />
</AuthProvider>
</Provider>
);
};
const MainApp = () => {
const { InfoAuth, getTotalCart } = useAuth();
const { dispatch } = AuthStatus();
const fetchData = async () => {
try {
const data = await InfoAuth();
const result = await getTotalCart();
//
if (result) {
dispatch({ type: "INFOCART", payload: result });
}
if (data) {
dispatch({ type: "USERINFO", payload: data });
}
} catch (error) {
console.error("Error fetching data1:", error);
}
};
useEffect(() => {
const checkLoginStatus = async () => {
try {
const isLoggedIn = await AsyncStorage.getItem("isLoggedIn");
if (isLoggedIn === "true") {
fetchData();
const user = await AsyncStorage.getItem("user_id");
dispatch({ type: "LOGIN", payload: JSON.parse(user) });
}
} catch (error) {
console.error("Error reading data from AsyncStorage:", error);
}
};
checkLoginStatus();
}, []);
return <AppNavigator />;
};
export default App;