-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
74 lines (68 loc) · 1.82 KB
/
App.tsx
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
import { StatusBar } from "expo-status-bar";
import React, { useCallback, useState } from "react";
import { SafeAreaProvider } from "react-native-safe-area-context";
import Navigation from "./navigation";
import { AppContext } from "./utilities/context";
export default function App() {
const [products, setProducts] = useState<
{ ean: string; quantity: number; score?: number }[]
>([]);
const addProduct = useCallback(
(ean: string, quantity: number, score?: number) => {
if (products.find((p) => p.ean === ean)) {
setProducts(
products.map((p) =>
p.ean === ean
? { ean: p.ean, quantity: p.quantity + quantity, score: p.score }
: p
)
);
} else {
setProducts([{ ean, quantity, score: undefined }, ...products]);
}
},
[products]
);
const updateProduct = useCallback(
(ean: string, quantity: number, score?: number) => {
if (quantity <= 0) {
removeProduct(ean);
return;
}
if (products.find((p) => p.ean === ean)) {
setProducts(
products.map((p) =>
p.ean === ean
? { ean: p.ean, quantity, score: score ? score : p.score }
: p
)
);
} else {
setProducts([...products, { ean, quantity, score }]);
}
},
[products]
);
const removeProduct = useCallback(
(ean: string) => {
setProducts(products.filter((p) => p.ean === ean));
},
[products]
);
return (
<SafeAreaProvider>
<AppContext.Provider
value={{
products,
setProducts,
addProduct,
updateProduct,
removeProduct,
}}
>
<Navigation />
<StatusBar translucent />
</AppContext.Provider>
</SafeAreaProvider>
);
}