-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.js
198 lines (187 loc) · 5.54 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//import Image from "next/image";
//import styles from "./page.module.css";
'use client'
import { useState, useEffect } from 'react'
import { Box, Stack, Typography, Button, Modal, TextField } from '@mui/material'
import { firestore } from '@/firebase'
import {
collection,
doc,
getDocs,
query,
setDoc,
deleteDoc,
getDoc,
} from 'firebase/firestore'
const style = {
position: 'absolute',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
width: 400,
bgcolor: 'white',
border: '2px solid #b2ac88',
boxShadow: 24,
p: 4,
display: 'flex',
flexDirection: 'column',
gap: 3,
}
export default function Home() {
// State hooks
const [inventory, setInventory] = useState([])
const [open, setOpen] = useState(false)
const [itemName, setItemName] = useState('')
// Function to update the inventory state from Firestore
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)
}
// Fetch inventory on component mount
useEffect(() => {
updateInventory()
}, [])
// Function to add an item to the inventory
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()
}
// Function to increment the quantity of an item
const incrementQuantity = 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 })
await updateInventory()
}
}
// Function to decrement the quantity of an item
const decrementQuantity = 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 setDoc(docRef, { quantity: quantity - 1 })
} else {
await deleteDoc(docRef)
}
await updateInventory()
}
}
// Function to open the modal for adding a new item
const handleOpen = () => setOpen(true)
// Function to close the modal
const handleClose = () => setOpen(false)
return (
<Box
width="100vw"
height="100vh"
display={'flex'}
justifyContent={'center'}
flexDirection={'column'}
alignItems={'center'}
gap={2}
>
<Modal
open={open}
onClose={handleClose}
aria-labelledby="modal-modal-title"
aria-describedby="modal-modal-description"
>
<Box sx={style}>
<Typography id="modal-modal-title" variant="h6" component="h2">
Add Item
</Typography>
<Stack width="100%" direction={'row'} spacing={2}>
<TextField
id="outlined-basic"
label="Item"
variant="outlined"
fullWidth
value={itemName}
onChange={(e) => setItemName(e.target.value)}
/>
<Button
variant="outlined"
onClick={() => {
addItem(itemName)
setItemName('')
handleClose()
}}
>
Add
</Button>
</Stack>
</Box>
</Modal>
<Button variant="contained" onClick={handleOpen}>
Add New Item
</Button>
<Box border={'1px solid #333'}>
<Box
width="800px"
height="100px"
bgcolor={'#9370DB'}
display={'flex'}
justifyContent={'center'}
alignItems={'center'}
>
<Typography variant={'h2'} color={'#FFFFFF'} textAlign={'center'} fontStyle={'Arial'}>
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'}
justifyContent={'space-between'}
alignItems={'center'}
bgcolor={'#E6E6FA'}
paddingX={5}
>
<Typography variant={'h3'} color={'#333'} textAlign={'center'}>
{name.charAt(0).toUpperCase() + name.slice(1)}
</Typography>
<Typography variant={'h3'} color={'#333'} textAlign={'center'}>
Quantity: {quantity}
</Typography>
<Box>
<Button
variant="contained"
onClick={() => incrementQuantity(name)}
sx={{ backgroundColor: '#4caf50', color: 'white' }} // Custom green color
>
+
</Button>
<Button
variant="contained"
onClick={() => decrementQuantity(name)}
sx={{ backgroundColor: '#f44336', color: 'white' }} // Custom red color
>
-
</Button>
</Box>
</Box>
))}
</Stack>
</Box>
</Box>
)
}