-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
49 lines (43 loc) · 1.45 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
import { useEffect } from "react";
import { useDispatch } from "react-redux";
import { useSelector } from "react-redux";
import Cart from "./components/Cart/Cart";
import Layout from "./components/Layout/Layout";
import Products from "./components/Shop/Products";
import Notification from "./components/UI/Notification";
import { fetchCartData, sendCartData } from "./store/cart-action";
let isInitial = true;
function App() {
const isCartVisible = useSelector((state) => state.ui.cartIsVisible);
const dispatch = useDispatch();
const cart = useSelector((state) => state.cart);
const notification = useSelector((state) => state.ui.notification);
// fetch data from server
useEffect(() => {
dispatch(fetchCartData());
}, [dispatch]);
// update data to server
useEffect(() => {
if (!isInitial) {
dispatch(sendCartData(cart));
}
// prevent sending data again after fetching data
if (!(cart.items.length === 0 && isInitial)) isInitial = false;
}, [cart, dispatch]);
return (
<>
{notification && (
<Notification
status={notification.status}
title={notification.title}
message={notification.message}
/>
)}
<Layout>
{isCartVisible && <Cart />}
<Products />
</Layout>
</>
);
}
export default App;