1 | /*
|
---|
2 | import axios from "axios";
|
---|
3 | import React, { useEffect, useReducer, useState } from "react";
|
---|
4 | import { useNavigate, useLocation } from "react-router-dom";
|
---|
5 | import ListGroup from "react-bootstrap/ListGroup";
|
---|
6 | import Button from "react-bootstrap/Button";
|
---|
7 | import Form from "react-bootstrap/Form";
|
---|
8 | import { getError } from "./utils";
|
---|
9 | import { toast } from "react-toastify";
|
---|
10 | import LoadingBox from "./LoadingBox";
|
---|
11 | import MessageBox from "./MessageBox";
|
---|
12 | import LinkContainer from "react-router-bootstrap/LinkContainer";
|
---|
13 |
|
---|
14 | const reducer = (state, action) => {
|
---|
15 | switch (action.type) {
|
---|
16 | case "FETCH_REQUEST":
|
---|
17 | return { ...state, loading: true };
|
---|
18 | case "FETCH_SUCCESS":
|
---|
19 | return {
|
---|
20 | ...state,
|
---|
21 | products: action.payload.products,
|
---|
22 | page: action.payload.page,
|
---|
23 | pages: action.payload.pages,
|
---|
24 | countProducts: action.payload.countProducts,
|
---|
25 | loading: false,
|
---|
26 | };
|
---|
27 | case "FETCH_FAIL":
|
---|
28 | return { ...state, loading: false, error: action.payload };
|
---|
29 | default:
|
---|
30 | return state;
|
---|
31 | }
|
---|
32 | };
|
---|
33 |
|
---|
34 | function ListProducts() {
|
---|
35 | const navigate = useNavigate();
|
---|
36 | const { search } = useLocation();
|
---|
37 | const sp = new URLSearchParams(search);
|
---|
38 | const category = sp.get("category") || "all";
|
---|
39 | const query = sp.get("query") || "all";
|
---|
40 | const subCategory = sp.get("subCategory") || "all";
|
---|
41 | const name = sp.get("name") || "all";
|
---|
42 | const order = sp.get("order") || "newest";
|
---|
43 | const page = sp.get("page") || 1;
|
---|
44 |
|
---|
45 | const [{ loading, error, products, pages, countProducts }, dispatch] =
|
---|
46 | useReducer(reducer, { loading: true, error: "" });
|
---|
47 |
|
---|
48 | useEffect(() => {
|
---|
49 | const fetchData = async () => {
|
---|
50 | try {
|
---|
51 | const { data } = await axios.get(
|
---|
52 | `/api/products/search?page=${page}&query=${query}&category=${category}&subCategory=${subCategory}&order=${order}`
|
---|
53 | );
|
---|
54 | dispatch({ type: "FETCH_SUCCSS", payload: data });
|
---|
55 | } catch (err) {
|
---|
56 | dispatch({ type: "FETCH_FAIL", payload: getError(error) });
|
---|
57 | }
|
---|
58 | };
|
---|
59 | fetchData();
|
---|
60 | }, [category, order, page, query, subCategory, error]);
|
---|
61 | /*
|
---|
62 | const [categories, setCategories] = useState([]);
|
---|
63 | useEffect(() => {
|
---|
64 | const fetchCategories = async () => {
|
---|
65 | try {
|
---|
66 | const { data } = await axios.get(`api/products/categories`);
|
---|
67 | setCategories(data);
|
---|
68 | } catch (err) {
|
---|
69 | toast.error(getError(err));
|
---|
70 | }
|
---|
71 | };
|
---|
72 | fetchCategories();
|
---|
73 | }, [dispatch]);
|
---|
74 |
|
---|
75 | const getFilterUrl = (filter) => {
|
---|
76 | const filterPage = filter.page || page;
|
---|
77 | const filterCategorry = filter.category || category;
|
---|
78 | const filterQuery = filter.query || query;
|
---|
79 | const filterSubCategory = filter.subCategory || subCategory;
|
---|
80 | const sortOrder = filter.order || order;
|
---|
81 | return `/search?category=${filterCategorry}&query=${filterQuery}&subCategory=${filterSubCategory}&order=${sortOrder}&page=${filterPage}`;
|
---|
82 | };
|
---|
83 |
|
---|
84 | return (
|
---|
85 | <div
|
---|
86 | style={{
|
---|
87 | display: "flex",
|
---|
88 | flexDirection: "column",
|
---|
89 | alignItems: "center",
|
---|
90 | justifyContent: "center",
|
---|
91 | }}
|
---|
92 | >
|
---|
93 | {loading ? (
|
---|
94 | <LoadingBox></LoadingBox>
|
---|
95 | ) : error ? (
|
---|
96 | <MessageBox varriant="danger">{error}</MessageBox>
|
---|
97 | ) : (
|
---|
98 | <div className="taskContainer">
|
---|
99 | <h1>Производи</h1>
|
---|
100 | <div className="filterContainer" style={{ width: "100%" }}>
|
---|
101 | <Form
|
---|
102 | id="fff"
|
---|
103 | style={{
|
---|
104 | display: "flex",
|
---|
105 | alignItems: "center",
|
---|
106 | justifyContent: "space-around",
|
---|
107 | }}
|
---|
108 | >
|
---|
109 | <Form.Group>
|
---|
110 | <Form.Control
|
---|
111 | id="filterName"
|
---|
112 | placeholder="Име"
|
---|
113 | value={name}
|
---|
114 | required
|
---|
115 | ></Form.Control>
|
---|
116 | </Form.Group>
|
---|
117 | <Form.Group>
|
---|
118 | <Form.Select
|
---|
119 | value={category}
|
---|
120 | onChange={(e) => {
|
---|
121 | navigate(getFilterUrl({ category: e.target.value }));
|
---|
122 | }}
|
---|
123 | >
|
---|
124 | <option value="default">Категорија</option>
|
---|
125 | <option value={"dnevna"}>Дневна</option>
|
---|
126 | <option value={"spalna"}>Спална</option>
|
---|
127 | <option value={"kancelarija"}>Канцеларија</option>
|
---|
128 | <option value={"hodnik"}>Ходник</option>
|
---|
129 | <option value={"gradina"}>Градина</option>
|
---|
130 | <option value={"trpezarija"}>Трпезарија</option>
|
---|
131 | <option value={"kujna"}>Кујна</option>
|
---|
132 | <option value={"detska"}>Детска</option>
|
---|
133 | </Form.Select>
|
---|
134 | </Form.Group>
|
---|
135 | <Form.Group>
|
---|
136 | <Form.Select
|
---|
137 | value={subCategory}
|
---|
138 | onChange={(e) => {
|
---|
139 | navigate(getFilterUrl({ subCategory: e.target.value }));
|
---|
140 | }}
|
---|
141 | >
|
---|
142 | <option value="default">Подкатегорија</option>
|
---|
143 |
|
---|
144 | {category === "dnevna" && (
|
---|
145 | <option value="agolni-garnituri">Аголни Гарнитури</option>
|
---|
146 | )}
|
---|
147 | {category === "dnevna" && <option value="sofi">Софи</option>}
|
---|
148 | {category === "dnevna" && (
|
---|
149 | <option value="fotelji">Фотелји</option>
|
---|
150 | )}
|
---|
151 | {category === "dnevna" && (
|
---|
152 | <option value="taburetki">Табуретки</option>
|
---|
153 | )}
|
---|
154 | {category === "dnevna" && (
|
---|
155 | <option value="klub-masi">Клуб маси</option>
|
---|
156 | )}
|
---|
157 | {category === "dnevna" && (
|
---|
158 | <option value="tv-komodi">ТВ комоди</option>
|
---|
159 | )}
|
---|
160 | {category === "dnevna" && (
|
---|
161 | <option value="komodi">Комоди</option>
|
---|
162 | )}
|
---|
163 | {category === "spalna" && (
|
---|
164 | <option value="spalni-kompleti">Спални Комплети</option>
|
---|
165 | )}
|
---|
166 | {category === "spalna" && (
|
---|
167 | <option value="lezai">Лежаи</option>
|
---|
168 | )}
|
---|
169 | {category === "spalna" && (
|
---|
170 | <option value="kreveti">Кревети</option>
|
---|
171 | )}
|
---|
172 | {category === "spalna" && (
|
---|
173 | <option value="plakari">Плакари</option>
|
---|
174 | )}
|
---|
175 | {category === "spalna" && (
|
---|
176 | <option value="nokni-skafcinja">Ноќни шкафчиња</option>
|
---|
177 | )}
|
---|
178 | {category === "spalna" && (
|
---|
179 | <option value="toaletni-masi">Тоалетни маси</option>
|
---|
180 | )}
|
---|
181 | {category === "kancelarija" && (
|
---|
182 | <option value="biroa">Бироа</option>
|
---|
183 | )}
|
---|
184 | {category === "kancelarija" && (
|
---|
185 | <option value="kancelariski-stolovi">
|
---|
186 | Канцелариски столови
|
---|
187 | </option>
|
---|
188 | )}
|
---|
189 | {category === "kancelarija" && (
|
---|
190 | <option value="gejmerski-stolovi">Гејмерски столови</option>
|
---|
191 | )}
|
---|
192 | {category === "kancelarija" && (
|
---|
193 | <option value="kancelariski-skafovi">
|
---|
194 | Канцелариски шкафови
|
---|
195 | </option>
|
---|
196 | )}
|
---|
197 | {category === "hodnik" && (
|
---|
198 | <option value="skafovi-za-cevli">Шкафови за чевли</option>
|
---|
199 | )}
|
---|
200 | {category === "hodnik" && (
|
---|
201 | <option value="zakacalki-i-ogledala">
|
---|
202 | Закачалки и огледала
|
---|
203 | </option>
|
---|
204 | )}
|
---|
205 | {category === "hodnik" && (
|
---|
206 | <option value="kolekcii-za-hodnik">
|
---|
207 | Колекции за ходник
|
---|
208 | </option>
|
---|
209 | )}
|
---|
210 | {category === "gradina" && (
|
---|
211 | <option value="gradinarski-kompleti">
|
---|
212 | Градинарски комплети
|
---|
213 | </option>
|
---|
214 | )}
|
---|
215 | {category === "gradina" && (
|
---|
216 | <option value="gradinarski-lulki">Градинарски лулки</option>
|
---|
217 | )}
|
---|
218 | {category === "gradina" && (
|
---|
219 | <option value="gradinarski-cadori">
|
---|
220 | Градинарски чадори
|
---|
221 | </option>
|
---|
222 | )}
|
---|
223 | {category === "gradina" && (
|
---|
224 | <option value="gradinarski-masi">Градинарски маси</option>
|
---|
225 | )}
|
---|
226 | {category === "gradina" && (
|
---|
227 | <option value="gradinarski-stolovi">
|
---|
228 | Градинарски столови
|
---|
229 | </option>
|
---|
230 | )}
|
---|
231 | {category === "gradina" && (
|
---|
232 | <option value="gradinarsko-osvetluvanje">
|
---|
233 | Градинарско осветлување
|
---|
234 | </option>
|
---|
235 | )}
|
---|
236 | {category === "trpezarija" && (
|
---|
237 | <option value="trpezariski-masi">Трпезариски маси</option>
|
---|
238 | )}
|
---|
239 | {category === "trpezarija" && (
|
---|
240 | <option value="trpezariski-stolovi">
|
---|
241 | Трпезариски столови
|
---|
242 | </option>
|
---|
243 | )}
|
---|
244 | {category === "trpezarija" && (
|
---|
245 | <option value="kujnski-garnituri">Кујнски гарнитури</option>
|
---|
246 | )}
|
---|
247 | {category === "trpezarija" && (
|
---|
248 | <option value="bar-stolovi-i-masi">
|
---|
249 | Бар столови и маси
|
---|
250 | </option>
|
---|
251 | )}
|
---|
252 | {category === "kujna" && (
|
---|
253 | <option value="kujnski-agolni-garnituri">
|
---|
254 | Кујнски аголни гарнитури
|
---|
255 | </option>
|
---|
256 | )}
|
---|
257 | {category === "kujna" && (
|
---|
258 | <option value="standardni-kujni">Стандардни кујни</option>
|
---|
259 | )}
|
---|
260 | {category === "detska" && (
|
---|
261 | <option value="kolekcii-za-detska-soba">
|
---|
262 | Колекции за детска соба
|
---|
263 | </option>
|
---|
264 | )}
|
---|
265 | {category === "detska" && (
|
---|
266 | <option value="detski-biroa">Детски бироа</option>
|
---|
267 | )}
|
---|
268 | {category === "detska" && (
|
---|
269 | <option value="detski-lezai">Детски лежаи</option>
|
---|
270 | )}
|
---|
271 | </Form.Select>
|
---|
272 | </Form.Group>
|
---|
273 | <Form.Group>
|
---|
274 | <Form.Select
|
---|
275 | id="sortOrder"
|
---|
276 | value={order}
|
---|
277 | onChange={(e) => {
|
---|
278 | navigate(getFilterUrl({ order: e.target.value }));
|
---|
279 | }}
|
---|
280 | >
|
---|
281 | <option value={"default"}>Сортирај по цена</option>
|
---|
282 | <option value={"lowFirst"}>Од ниска кон висока</option>
|
---|
283 | <option value={"highFirst"}>Од висока кон ниска</option>
|
---|
284 | </Form.Select>
|
---|
285 | </Form.Group>
|
---|
286 | <Button variant="danger">Филтрирај</Button>
|
---|
287 | </Form>
|
---|
288 | </div>
|
---|
289 | <ListGroup
|
---|
290 | id="filteredProductsContainer"
|
---|
291 | variant="success"
|
---|
292 | style={{ width: "95%", margin: "auto", marginTop: "20px" }}
|
---|
293 | >
|
---|
294 | {products.map((item) => (
|
---|
295 | <ListGroup.Item key={item._id}>
|
---|
296 | <div
|
---|
297 | style={{
|
---|
298 | display: "flex",
|
---|
299 | alignItems: "center",
|
---|
300 | justifyContent: "space-between",
|
---|
301 | }}
|
---|
302 | >
|
---|
303 | <div style={{ display: "flex", alignItems: "center" }}>
|
---|
304 | <img
|
---|
305 | src={item.image}
|
---|
306 | alt={item.name}
|
---|
307 | className="img-fluid rounded img-thumbnail"
|
---|
308 | style={{ margin: "0px" }}
|
---|
309 | ></img>
|
---|
310 | <span style={{ marginLeft: "20px" }}>{item.name}</span>
|
---|
311 | </div>
|
---|
312 | <span>{item.category}</span>
|
---|
313 | <span>{item.subCategory}</span>
|
---|
314 | <span>{item.countInStock}</span>
|
---|
315 |
|
---|
316 | <span>{item.price} ден</span>
|
---|
317 | <Button
|
---|
318 | type="button"
|
---|
319 | variant="primary"
|
---|
320 | style={{ height: "50px" }}
|
---|
321 | >
|
---|
322 | Измени
|
---|
323 | </Button>
|
---|
324 | </div>
|
---|
325 | </ListGroup.Item>
|
---|
326 | ))}
|
---|
327 | </ListGroup>
|
---|
328 | <div>
|
---|
329 | {[...Array(pages).keys()].map((x) => (
|
---|
330 | <LinkContainer
|
---|
331 | key={x + 1}
|
---|
332 | className="mx-1"
|
---|
333 | to={getFilterUrl({ page: x + 1 })}
|
---|
334 | >
|
---|
335 | <Button
|
---|
336 | className={Number(page) === x + 1 ? "text-bold" : ""}
|
---|
337 | variant="light"
|
---|
338 | >
|
---|
339 | {x + 1}
|
---|
340 | </Button>
|
---|
341 | </LinkContainer>
|
---|
342 | ))}
|
---|
343 | </div>
|
---|
344 | {/*products.map((product) => (
|
---|
345 | <Product key={product.slug} product={product} />
|
---|
346 | ))}
|
---|
347 | </div>
|
---|
348 | )}
|
---|
349 | </div>
|
---|
350 | );
|
---|
351 | }
|
---|
352 |
|
---|
353 | export default ListProducts;
|
---|
354 | */
|
---|