source: frontend/src/screens/AdminEditProductScreen.js@ a2e5735

Last change on this file since a2e5735 was a2e5735, checked in by Nace Gjorgjievski <nace.gorgievski123@…>, 23 months ago

Final Version

  • Property mode set to 100644
File size: 13.1 KB
Line 
1import axios from "axios";
2import React, { useEffect, useReducer, useState } from "react";
3import { Link, useParams } from "react-router-dom";
4import { toast } from "react-toastify";
5import Button from "react-bootstrap/Button";
6import Form from "react-bootstrap/Form";
7
8const reducer = (state, action) => {
9 switch (action.type) {
10 case "FETCH_REQUEST":
11 return { ...state, loading: true };
12 case "FETCH_SUCCESS":
13 return { ...state, product: action.payload, loading: false };
14 case "FETCH_FAIL":
15 return { ...state, loading: false, error: action.payload };
16 default:
17 return state;
18 }
19};
20
21function AdminEditProductScreen() {
22 const params = useParams();
23 const { slug } = params;
24
25 const [{ loading, error, product }, dispatch] = useReducer(reducer, {
26 product: [],
27 loading: true,
28 error: "",
29 });
30
31 useEffect(() => {
32 const fetchData = async () => {
33 dispatch({ type: "FETCH_REQUEST" });
34 try {
35 const result = await axios.get(`/api/products/slug/${slug}`);
36 dispatch({ type: "FETCH_SUCCESS", payload: result.data });
37 } catch (err) {
38 dispatch({ type: "FETCH_FAIL", payload: err.message });
39 }
40 };
41 fetchData();
42 }, [slug]);
43
44 const [category, setCategory] = useState("all");
45 const [subCategory, setSubCategory] = useState("all");
46
47 const submitHandler = async (e) => {
48 e.preventDefault();
49 let name = document.getElementById("name").value;
50 let slug = document.getElementById("slug").value;
51 let price = document.getElementById("price").value;
52 let priceMontaza = document.getElementById("priceMontaza").value;
53 let countInStock = document.getElementById("stock").value;
54 let description = document.getElementById("textArea").value;
55
56 try {
57 const { data } = await axios.put("/api/products/edit", {
58 name,
59 slug,
60 category,
61 subCategory,
62 description,
63 price,
64 priceMontaza,
65 countInStock,
66 });
67
68 toast.success("Успешно ажурирање");
69 } catch (err) {
70 dispatch({ type: "FETCH_FAIL" });
71 toast.error("Грешка");
72 }
73 };
74
75 return (
76 <div id="pgContainer">
77 <div id="sidebarMenu">
78 <Link
79 to={"/admin/addProduct"}
80 style={{ textDecoration: "none", width: "100%" }}
81 >
82 <div className="dashboard-btn" to="/admin/addProduct">
83 Додади нов производ
84 </div>
85 </Link>
86 <Link
87 to={"/admin/addCategory"}
88 style={{ textDecoration: "none", width: "100%" }}
89 >
90 <div className="dashboard-btn">Додади категорија</div>
91 </Link>
92 <Link
93 to={"/admin/products"}
94 style={{ textDecoration: "none", width: "100%" }}
95 >
96 <div className="dashboard-btn">Производи</div>
97 </Link>
98 <Link
99 to={"/admin/orders"}
100 style={{ textDecoration: "none", width: "100%" }}
101 >
102 <div className="dashboard-btn">Нарачки</div>
103 </Link>
104 </div>
105 <div id="mainScreen">
106 <div className="taskContainer">
107 <h1>Ажурирај продукт</h1>
108 <Form
109 onSubmit={submitHandler}
110 encType="multipart/form-data"
111 className="newProductFormCointainer"
112 >
113 <div className="firstRow" style={{ display: "flex" }}>
114 <Form.Group>
115 <Form.Label>Име</Form.Label>
116 <Form.Control
117 id="name"
118 defaultValue={product.name}
119 required
120 ></Form.Control>
121 </Form.Group>
122 <Form.Group>
123 <Form.Label>Слуг</Form.Label>
124 <Form.Control
125 id="slug"
126 defaultValue={slug}
127 required
128 ></Form.Control>
129 </Form.Group>
130 <Form.Group>
131 <Form.Label>Категорија</Form.Label>
132 <Form.Select onChange={(e) => setCategory(e.target.value)}>
133 <option value={"all"}>Категорија</option>
134 <option value={"dnevna"}>Дневна</option>
135 <option value={"spalna"}>Спална</option>
136 <option value={"kancelarija"}>Канцеларија</option>
137 <option value={"hodnik"}>Ходник</option>
138 <option value={"gradina"}>Градина</option>
139 <option value={"trpezarija"}>Трпезарија</option>
140 <option value={"kujna"}>Кујна</option>
141 <option value={"detska"}>Детска</option>
142 </Form.Select>
143 </Form.Group>
144 <Form.Group>
145 <Form.Label>Подкатегорија</Form.Label>
146 <Form.Select
147 onChange={(e) => {
148 setSubCategory(e.target.value);
149 }}
150 >
151 <option value="all">Подкатегорија</option>
152
153 {category === "dnevna" && (
154 <option value="agolni-garnituri">Аголни Гарнитури</option>
155 )}
156 {category === "dnevna" && <option value="sofi">Софи</option>}
157 {category === "dnevna" && (
158 <option value="fotelji">Фотелји</option>
159 )}
160 {category === "dnevna" && (
161 <option value="taburetki">Табуретки</option>
162 )}
163 {category === "dnevna" && (
164 <option value="klub-masi">Клуб маси</option>
165 )}
166 {category === "dnevna" && (
167 <option value="tv-komodi">ТВ комоди</option>
168 )}
169 {category === "dnevna" && (
170 <option value="komodi">Комоди</option>
171 )}
172 {category === "spalna" && (
173 <option value="spalni-kompleti">Спални Комплети</option>
174 )}
175 {category === "spalna" && (
176 <option value="lezai">Лежаи</option>
177 )}
178 {category === "spalna" && (
179 <option value="kreveti">Кревети</option>
180 )}
181 {category === "spalna" && (
182 <option value="plakari">Плакари</option>
183 )}
184 {category === "spalna" && (
185 <option value="nokni-skafcinja">Ноќни шкафчиња</option>
186 )}
187 {category === "spalna" && (
188 <option value="toaletni-masi">Тоалетни маси</option>
189 )}
190 {category === "kancelarija" && (
191 <option value="biroa">Бироа</option>
192 )}
193 {category === "kancelarija" && (
194 <option value="kancelariski-stolovi">
195 Канцелариски столови
196 </option>
197 )}
198 {category === "kancelarija" && (
199 <option value="gejmerski-stolovi">Гејмерски столови</option>
200 )}
201 {category === "kancelarija" && (
202 <option value="kancelariski-skafovi">
203 Канцелариски шкафови
204 </option>
205 )}
206 {category === "hodnik" && (
207 <option value="skafovi-za-cevli">Шкафови за чевли</option>
208 )}
209 {category === "hodnik" && (
210 <option value="zakacalki-i-ogledala">
211 Закачалки и огледала
212 </option>
213 )}
214 {category === "hodnik" && (
215 <option value="kolekcii-za-hodnik">
216 Колекции за ходник
217 </option>
218 )}
219 {category === "gradina" && (
220 <option value="gradinarski-kompleti">
221 Градинарски комплети
222 </option>
223 )}
224 {category === "gradina" && (
225 <option value="gradinarski-lulki">Градинарски лулки</option>
226 )}
227 {category === "gradina" && (
228 <option value="gradinarski-cadori">
229 Градинарски чадори
230 </option>
231 )}
232 {category === "gradina" && (
233 <option value="gradinarski-masi">Градинарски маси</option>
234 )}
235 {category === "gradina" && (
236 <option value="gradinarski-stolovi">
237 Градинарски столови
238 </option>
239 )}
240 {category === "gradina" && (
241 <option value="gradinarsko-osvetluvanje">
242 Градинарско осветлување
243 </option>
244 )}
245 {category === "trpezarija" && (
246 <option value="trpezariski-masi">Трпезариски маси</option>
247 )}
248 {category === "trpezarija" && (
249 <option value="trpezariski-stolovi">
250 Трпезариски столови
251 </option>
252 )}
253 {category === "trpezarija" && (
254 <option value="kujnski-garnituri">Кујнски гарнитури</option>
255 )}
256 {category === "trpezarija" && (
257 <option value="bar-stolovi-i-masi">
258 Бар столови и маси
259 </option>
260 )}
261 {category === "kujna" && (
262 <option value="kujnski-agolni-garnituri">
263 Кујнски аголни гарнитури
264 </option>
265 )}
266 {category === "kujna" && (
267 <option value="standardni-kujni">Стандардни кујни</option>
268 )}
269 {category === "detska" && (
270 <option value="kolekcii-za-detska-soba">
271 Колекции за детска соба
272 </option>
273 )}
274 {category === "detska" && (
275 <option value="detski-biroa">Детски бироа</option>
276 )}
277 {category === "detska" && (
278 <option value="detski-lezai">Детски лежаи</option>
279 )}
280 </Form.Select>
281 </Form.Group>
282 </div>
283 <div className="secondRow">
284 <Form.Group style={{ width: "60%", marginTop: "20px" }}>
285 <Form.Label>Опис</Form.Label>
286 <Form.Control
287 id="textArea"
288 defaultValue={product.description}
289 as="textarea"
290 required
291 ></Form.Control>
292 </Form.Group>
293 <div
294 style={{
295 display: "flex",
296 flexDirection: "column",
297 alignItems: "center",
298 justifyContent: "center",
299 width: "40%",
300 }}
301 >
302 <Form.Group style={{ marginTop: "20px" }}>
303 <Form.Label>Цена</Form.Label>
304 <Form.Control
305 id="price"
306 defaultValue={product.price}
307 type="number"
308 style={{ color: "black" }}
309 required
310 ></Form.Control>
311 </Form.Group>
312 <Form.Group style={{ marginTop: "10px" }}>
313 <Form.Label>Цена Монтажа</Form.Label>
314 <Form.Control
315 id="priceMontaza"
316 defaultValue={product.priceMontaza}
317 type="number"
318 required
319 ></Form.Control>
320 </Form.Group>
321 <Form.Group style={{ marginTop: "10px" }}>
322 <Form.Label>Залиха</Form.Label>
323 <Form.Control
324 id="stock"
325 defaultValue={product.countInStock}
326 type="number"
327 required
328 ></Form.Control>
329 </Form.Group>
330 </div>
331 </div>
332
333 <div className="submitBtnContainer">
334 <Button variant="danger" size="lg" type="submit">
335 Ажурирај
336 </Button>
337 </div>
338 </Form>
339 </div>
340 </div>
341 </div>
342 );
343}
344
345export default AdminEditProductScreen;
Note: See TracBrowser for help on using the repository browser.