-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppContext.js
64 lines (52 loc) · 1.56 KB
/
AppContext.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
import React, { createContext, useState, useEffect } from 'react';
import { auth } from './firebase';
const AppContext = createContext();
const AppProvider = ({ children }) => {
const [cart, setCart] = useState([]);
const [totalBill, setTotalBill] = useState(0);
const [favorites, setFavorites] = useState([]);
const [ordersCount, setOrdersCount] = useState(0);
const [confirmedOrders, setConfirmedOrders] = useState([]);
const deliveryCharge = 20;
const calculateTotalBill = () => {
let total = 0;
cart.forEach((item) => {
total += item.price * item.quantity;
});
const deliveryCharges = ordersCount < 2 ? 0 : deliveryCharge;
total += deliveryCharges;
return total;
};
const addToFavorites = (item) => {
setFavorites((prevFavorites) => [...prevFavorites, item]);
};
const removeFromFavorites = (itemId) => {
setFavorites((prevFavorites) => prevFavorites.filter((item) => item.id !== itemId));
};
const addOrder = (order) => {
setConfirmedOrders((prev) => [...prev, order]);
setOrdersCount((prevCount) => prevCount + 1); // Increment order count
};
return (
<AppContext.Provider
value={{
cart,
setCart,
totalBill: calculateTotalBill(),
setTotalBill,
favorites,
setFavorites,
ordersCount,
setOrdersCount,
addOrder,
confirmedOrders,
removeFromFavorites,
addToFavorites,
setConfirmedOrders,
}}
>
{children}
</AppContext.Provider>
);
};
export { AppContext, AppProvider };