-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.js
162 lines (151 loc) · 4.22 KB
/
page.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
'use client';
import Image from "next/image";
import { useState, useEffect } from 'react';
import { firestore } from '@/firebase';
import { Stack, Box, Modal, Typography, TextField, Button } from '@mui/material';
import { collection, deleteDoc, doc, getDocs, query, getDoc, setDoc } from "firebase/firestore";
export default function Home() {
const [inventory, setInventory] = useState([]);
const [open, setOpen] = useState(true);
const [itemName, setItemName] = useState('');
const updateInventory = async () => {
const snapshot = query(collection(firestore, 'inventory'));
const docs = await getDocs(snapshot);
const inventoryList = [];
docs.forEach((doc) => {
inventoryList.push({
name: doc.id,
...doc.data(),
});
});
setInventory(inventoryList);
};
const addItem = async (item) => {
const docRef = doc(collection(firestore, 'inventory'), item);
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
const { quantity } = docSnap.data();
await setDoc(docRef, { quantity: quantity + 1 });
} else {
await setDoc(docRef, { quantity: 1 });
}
await updateInventory();
};
const removeItem = async (item) => {
const docRef = doc(collection(firestore, 'inventory'), item);
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
const { quantity } = docSnap.data();
if (quantity === 1) {
await deleteDoc(docRef);
} else {
await setDoc(docRef, { quantity: quantity - 1 });
}
}
await updateInventory();
};
useEffect(() => {
updateInventory();
}, []);
const handleOpen = () => setOpen(true);
const handleClose = () => setOpen(false);
return (
<Box
width="100vw"
height="100vh"
display="flex"
flexDirection="column"
justifyContent="center"
alignItems="center"
gap={2}
>
<Modal open={open} onClose={handleClose}>
<Box
position="absolute"
top="50%"
left="50%"
width={400}
bgcolor="white"
border="2px solid #000000"
boxShadow={24}
p={4}
display="flex"
flexDirection="column"
gap={3}
sx={{ transform: "translate(-50%, -50%)" }}
>
<Typography variant="h6">Add Item</Typography>
<TextField
variant="outlined"
fullWidth
value={itemName}
onChange={(e) => {
setItemName(e.target.value);
}}
/>
<Button
variant="outlined"
onClick={() => {
addItem(itemName);
setItemName('');
handleClose();
}}
>
Add
</Button>
</Box>
</Modal>
<Button
variant="contained"
onClick={() => {
handleOpen();
}}
>
Add New Item
</Button>
<Box border="1px solid #333">
<Box
width="800px"
height="100px"
bgcolor="ADD8E6"
display="flex"
alignItems="center"
justifyContent="center"
>
<Typography variant="h2" color="#333">
Inventory Items
</Typography>
</Box>
<Stack width="800px" height="300px" spacing={2} overflow="auto">
{inventory.map(({ name, quantity }) => (
<Box
key={name}
width="100%"
minHeight="150px"
display="flex"
alignItems="center"
justifyContent="space-between"
bgcolor="#f0f0f0"
padding={5}
>
<Typography variant="h3" color="#333" textAlign="center">
{name.charAt(0).toUpperCase() + name.slice(1)}
</Typography>
<Typography variant="h3" color="#333" textAlign="center">
{quantity}
</Typography>
<Button
variant="contained"
onClick={() => {
removeItem(name);
}}
>
Remove
</Button>
</Box>
))}
</Stack>
</Box>
</Box>
);
}