[a2e5735] | 1 | import React, { useEffect, useReducer, useState } from "react";
|
---|
[b612ab1] | 2 | import "../styles/Home.css";
|
---|
| 3 | // import data from "./data";
|
---|
| 4 | import Product from "../components/Product";
|
---|
| 5 | import axios from "axios";
|
---|
| 6 | import { Helmet } from "react-helmet-async";
|
---|
[113029b] | 7 | import { getError } from "../components/utils";
|
---|
| 8 | import { useLocation, useNavigate, useParams } from "react-router-dom";
|
---|
| 9 | import Form from "react-bootstrap/Form";
|
---|
| 10 | import Button from "react-bootstrap/Button";
|
---|
| 11 | import LinkContainer from "react-router-bootstrap/LinkContainer";
|
---|
[b612ab1] | 12 |
|
---|
| 13 | const reducer = (state, action) => {
|
---|
| 14 | switch (action.type) {
|
---|
| 15 | case "FETCH_REQUEST":
|
---|
| 16 | return { ...state, loading: true };
|
---|
| 17 | case "FETCH_SUCCESS":
|
---|
[113029b] | 18 | return {
|
---|
| 19 | ...state,
|
---|
| 20 | products: action.payload.products,
|
---|
| 21 | page: action.payload.page,
|
---|
| 22 | pages: action.payload.pages,
|
---|
| 23 | countProducts: action.payload.countProducts,
|
---|
| 24 | loading: false,
|
---|
| 25 | };
|
---|
[b612ab1] | 26 | case "FETCH_FAIL":
|
---|
| 27 | return { ...state, loading: false, error: action.payload };
|
---|
| 28 | default:
|
---|
| 29 | return state;
|
---|
| 30 | }
|
---|
| 31 | };
|
---|
| 32 |
|
---|
| 33 | function Home() {
|
---|
[113029b] | 34 | const params = useParams();
|
---|
| 35 | const { category, subCategory } = params;
|
---|
[a2e5735] | 36 | var HF = 0;
|
---|
| 37 | var HT = 1000;
|
---|
| 38 | var WF = 0;
|
---|
| 39 | var WT = 1000;
|
---|
| 40 | var LF = 0;
|
---|
| 41 | var LT = 1000;
|
---|
| 42 | //const [HT, setHT] = useState(1000);
|
---|
| 43 | //const [WF, setWF] = useState(0);
|
---|
| 44 | //const [WT, setWT] = useState(1000);
|
---|
| 45 | //const [LF, setLF] = useState(0);
|
---|
| 46 | //const [LT, setLT] = useState(1000);
|
---|
[113029b] | 47 | const navigate = useNavigate();
|
---|
| 48 | const { search } = useLocation();
|
---|
| 49 | const sp = new URLSearchParams(search);
|
---|
| 50 | //const category = sp.get("category") || "all";
|
---|
| 51 | const query = sp.get("query") || "all";
|
---|
| 52 | //const subCategory = sp.get("subCategory") || "all";
|
---|
| 53 | //const name = sp.get("name") || "all";
|
---|
| 54 | const order = sp.get("order") || "newest";
|
---|
| 55 | const page = sp.get("page") || 1;
|
---|
| 56 | const [{ loading, error, products, pages, countProducts }, dispatch] =
|
---|
| 57 | useReducer(reducer, { loading: true, error: "" });
|
---|
| 58 |
|
---|
[b612ab1] | 59 | useEffect(() => {
|
---|
| 60 | const fetchData = async () => {
|
---|
| 61 | try {
|
---|
[113029b] | 62 | dispatch({ type: "FETCH_REQUEST" });
|
---|
| 63 | /*
|
---|
| 64 | const { data } = await axios.get(
|
---|
| 65 | `/api/products/search?page=${page}&query=${query}&category=${category}&subCategory=${subCategory}&order=${order}`
|
---|
| 66 | );*/
|
---|
| 67 | const { data } = await axios.get(
|
---|
[a2e5735] | 68 | `/api/products?page=${page}&query=${query}&category=${category}&subCategory=${subCategory}&order=${order}&HF=${HF}&HT=${HT}&WF=${WF}&WT=${WT}&LF=${LF}<=${LT}`
|
---|
[113029b] | 69 | );
|
---|
| 70 | dispatch({ type: "FETCH_SUCCESS", payload: data });
|
---|
[b612ab1] | 71 | } catch (err) {
|
---|
[113029b] | 72 | dispatch({ type: "FETCH_FAIL", payload: getError(err) });
|
---|
[b612ab1] | 73 | }
|
---|
| 74 | };
|
---|
| 75 | fetchData();
|
---|
[a2e5735] | 76 | }, [
|
---|
| 77 | category,
|
---|
| 78 | page,
|
---|
| 79 | query,
|
---|
| 80 | order,
|
---|
| 81 | subCategory,
|
---|
| 82 | error,
|
---|
| 83 | HF,
|
---|
| 84 | HT,
|
---|
| 85 | WF,
|
---|
| 86 | WT,
|
---|
| 87 | LF,
|
---|
| 88 | LT,
|
---|
| 89 | ]);
|
---|
[113029b] | 90 |
|
---|
| 91 | const getFilterUrl = (filter) => {
|
---|
| 92 | const filterPage = filter.page || page;
|
---|
| 93 | const filterCategorry = filter.category || category;
|
---|
| 94 | const filterQuery = filter.query || query;
|
---|
| 95 | const filterSubCategory = filter.subCategory || subCategory;
|
---|
| 96 | const sortOrder = filter.order || order;
|
---|
[a2e5735] | 97 | const filterHF = filter.HF || HF;
|
---|
| 98 | const filterHT = filter.HT || HT;
|
---|
| 99 | const filterWF = filter.WF || WF;
|
---|
| 100 | const filterWT = filter.WT || WT;
|
---|
| 101 | const filterLF = filter.LF || LF;
|
---|
| 102 | const filterLT = filter.LT || LT;
|
---|
| 103 | return `?category=${filterCategorry}&query=${filterQuery}&subCategory=${filterSubCategory}&page=${filterPage}&order=${sortOrder}&HF=${filterHF}&HT=${filterHT}&WF=${filterWF}&WT=${filterWT}&LF=${filterLF}<=${filterLT}`;
|
---|
| 104 | };
|
---|
| 105 |
|
---|
| 106 | const filterHandler = (e) => {
|
---|
| 107 | e.preventDefault();
|
---|
| 108 | HF = document.getElementById("HF").value;
|
---|
| 109 | HT = document.getElementById("HT").value;
|
---|
| 110 | WF = document.getElementById("WF").value;
|
---|
| 111 | WT = document.getElementById("WT").value;
|
---|
| 112 | LF = document.getElementById("LF").value;
|
---|
| 113 | LT = document.getElementById("LT").value;
|
---|
| 114 | console.log(HT);
|
---|
| 115 | const fetchData = async () => {
|
---|
| 116 | try {
|
---|
| 117 | dispatch({ type: "FETCH_REQUEST" });
|
---|
| 118 | /*
|
---|
| 119 | const { data } = await axios.get(
|
---|
| 120 | `/api/products/search?page=${page}&query=${query}&category=${category}&subCategory=${subCategory}&order=${order}`
|
---|
| 121 | );*/
|
---|
| 122 | const { data } = await axios.get(
|
---|
| 123 | `/api/products?page=${page}&query=${query}&category=${category}&subCategory=${subCategory}&order=${order}&HF=${HF}&HT=${HT}&WF=${WF}&WT=${WT}&LF=${LF}<=${LT}`
|
---|
| 124 | );
|
---|
| 125 | dispatch({ type: "FETCH_SUCCESS", payload: data });
|
---|
| 126 | } catch (err) {
|
---|
| 127 | dispatch({ type: "FETCH_FAIL", payload: getError(err) });
|
---|
| 128 | }
|
---|
| 129 | };
|
---|
| 130 | fetchData();
|
---|
[113029b] | 131 | };
|
---|
[b612ab1] | 132 |
|
---|
| 133 | return (
|
---|
[113029b] | 134 | <div style={{ width: "100%" }}>
|
---|
[b612ab1] | 135 | <Helmet>
|
---|
| 136 | <title>MebelCity</title>
|
---|
| 137 | </Helmet>
|
---|
| 138 |
|
---|
| 139 | <div className="most-popular-products">
|
---|
| 140 | {loading ? (
|
---|
| 141 | <div>Loading...</div>
|
---|
| 142 | ) : error ? (
|
---|
| 143 | <div>{error}</div>
|
---|
| 144 | ) : (
|
---|
[113029b] | 145 | <div style={{ width: "100%" }}>
|
---|
| 146 | {category === "kancelarija" ? (
|
---|
| 147 | <h1 style={{ textAlign: "center", marginTop: "10px" }}>
|
---|
| 148 | Канцеларија
|
---|
| 149 | </h1>
|
---|
| 150 | ) : category === "dnevna" ? (
|
---|
| 151 | <h1 style={{ textAlign: "center", marginTop: "10px" }}>Дневна</h1>
|
---|
| 152 | ) : category === "spalna" ? (
|
---|
| 153 | <h1 style={{ textAlign: "center", marginTop: "10px" }}>Спална</h1>
|
---|
| 154 | ) : category === "hodnik" ? (
|
---|
| 155 | <h1 style={{ textAlign: "center", marginTop: "10px" }}>Ходник</h1>
|
---|
| 156 | ) : category === "kujna" ? (
|
---|
| 157 | <h1 style={{ textAlign: "center", marginTop: "10px" }}>Кујна</h1>
|
---|
| 158 | ) : category === "gradina" ? (
|
---|
| 159 | <h1 style={{ textAlign: "center", marginTop: "10px" }}>
|
---|
| 160 | Градина
|
---|
| 161 | </h1>
|
---|
| 162 | ) : category === "trpezarija" ? (
|
---|
| 163 | <h1 style={{ textAlign: "center", marginTop: "10px" }}>
|
---|
| 164 | Трпезарија
|
---|
| 165 | </h1>
|
---|
| 166 | ) : (
|
---|
| 167 | <h1 style={{ textAlign: "center", marginTop: "10px" }}>
|
---|
| 168 | Детска соба
|
---|
| 169 | </h1>
|
---|
| 170 | )}
|
---|
| 171 |
|
---|
| 172 | <div
|
---|
| 173 | style={{
|
---|
| 174 | width: "100%",
|
---|
| 175 | display: "flex",
|
---|
| 176 | justifyContent: "space-evenly",
|
---|
| 177 | alignItems: "center",
|
---|
| 178 | marginTop: "20px",
|
---|
| 179 | }}
|
---|
| 180 | >
|
---|
| 181 | <Form
|
---|
[a2e5735] | 182 | onSubmit={filterHandler}
|
---|
[113029b] | 183 | style={{
|
---|
| 184 | width: "100%",
|
---|
| 185 | display: "flex",
|
---|
[a2e5735] | 186 | justifyContent: "space-around",
|
---|
[113029b] | 187 | marginRight: "40px",
|
---|
| 188 | alignItems: "center",
|
---|
| 189 | }}
|
---|
| 190 | >
|
---|
[a2e5735] | 191 | <Form.Group style={{ display: "flex", alignItems: "center" }}>
|
---|
| 192 | <Form.Label style={{ margin: "0px" }}>H:</Form.Label>
|
---|
| 193 | <Form.Control
|
---|
| 194 | style={{ width: "60px" }}
|
---|
| 195 | id="HF"
|
---|
| 196 | //onChange={(e) => setHF(e.target.value)}
|
---|
| 197 | />
|
---|
| 198 | <Form.Label style={{ margin: "0px" }}>-</Form.Label>
|
---|
| 199 | <Form.Control
|
---|
| 200 | style={{ width: "60px" }}
|
---|
| 201 | id="HT"
|
---|
| 202 | //onChange={(e) => setHT(e.target.value)}
|
---|
| 203 | />
|
---|
| 204 | </Form.Group>
|
---|
| 205 | <Form.Group style={{ display: "flex", alignItems: "center" }}>
|
---|
| 206 | <Form.Label style={{ margin: "0px" }}>W:</Form.Label>
|
---|
| 207 | <Form.Control
|
---|
| 208 | style={{ width: "60px" }}
|
---|
| 209 | id="WF"
|
---|
| 210 | //onChange={(e) => setWF(e.target.value)}
|
---|
| 211 | />
|
---|
| 212 | <Form.Label style={{ margin: "0px" }}>-</Form.Label>
|
---|
| 213 | <Form.Control
|
---|
| 214 | style={{ width: "60px" }}
|
---|
| 215 | id="WT"
|
---|
| 216 | //onChange={(e) => setWT(e.target.value)}
|
---|
| 217 | />
|
---|
| 218 | </Form.Group>
|
---|
| 219 | <Form.Group style={{ display: "flex", alignItems: "center" }}>
|
---|
| 220 | <Form.Label style={{ margin: "0px" }}>L:</Form.Label>
|
---|
| 221 | <Form.Control
|
---|
| 222 | id="LF"
|
---|
| 223 | style={{ width: "60px" }}
|
---|
| 224 | //onChange={(e) => setLF(e.target.value)}
|
---|
| 225 | />
|
---|
| 226 | <Form.Label style={{ margin: "0px" }}>-</Form.Label>
|
---|
| 227 | <Form.Control
|
---|
| 228 | id="LT"
|
---|
| 229 | style={{ width: "60px" }}
|
---|
| 230 | //onChange={(e) => setLT(e.target.value)}
|
---|
| 231 | />
|
---|
| 232 | </Form.Group>
|
---|
[113029b] | 233 | {/*}
|
---|
| 234 | <Form.Group>
|
---|
| 235 | <Form.Select
|
---|
| 236 | value={subCategory}
|
---|
| 237 | onChange={(e) => {
|
---|
| 238 | navigate(
|
---|
| 239 | getFilterUrl({ subCategory: e.target.value, page: 1 })
|
---|
| 240 | );
|
---|
| 241 | }}
|
---|
| 242 | >
|
---|
| 243 | <option value="all">Подкатегорија</option>
|
---|
| 244 |
|
---|
| 245 | {category === "dnevna" && (
|
---|
| 246 | <option value="agolni-garnituri">Аголни Гарнитури</option>
|
---|
| 247 | )}
|
---|
| 248 | {category === "dnevna" && (
|
---|
| 249 | <option value="sofi">Софи</option>
|
---|
| 250 | )}
|
---|
| 251 | {category === "dnevna" && (
|
---|
| 252 | <option value="fotelji">Фотелји</option>
|
---|
| 253 | )}
|
---|
| 254 | {category === "dnevna" && (
|
---|
| 255 | <option value="taburetki">Табуретки</option>
|
---|
| 256 | )}
|
---|
| 257 | {category === "dnevna" && (
|
---|
| 258 | <option value="klub-masi">Клуб маси</option>
|
---|
| 259 | )}
|
---|
| 260 | {category === "dnevna" && (
|
---|
| 261 | <option value="tv-komodi">ТВ комоди</option>
|
---|
| 262 | )}
|
---|
| 263 | {category === "dnevna" && (
|
---|
| 264 | <option value="komodi">Комоди</option>
|
---|
| 265 | )}
|
---|
| 266 | {category === "spalna" && (
|
---|
| 267 | <option value="spalni-kompleti">Спални Комплети</option>
|
---|
| 268 | )}
|
---|
| 269 | {category === "spalna" && (
|
---|
| 270 | <option value="lezai">Лежаи</option>
|
---|
| 271 | )}
|
---|
| 272 | {category === "spalna" && (
|
---|
| 273 | <option value="kreveti">Кревети</option>
|
---|
| 274 | )}
|
---|
| 275 | {category === "spalna" && (
|
---|
| 276 | <option value="plakari">Плакари</option>
|
---|
| 277 | )}
|
---|
| 278 | {category === "spalna" && (
|
---|
| 279 | <option value="nokni-skafcinja">Ноќни шкафчиња</option>
|
---|
| 280 | )}
|
---|
| 281 | {category === "spalna" && (
|
---|
| 282 | <option value="toaletni-masi">Тоалетни маси</option>
|
---|
| 283 | )}
|
---|
| 284 | {category === "kancelarija" && (
|
---|
| 285 | <option value="biroa">Бироа</option>
|
---|
| 286 | )}
|
---|
| 287 | {category === "kancelarija" && (
|
---|
| 288 | <option value="kancelariski-stolovi">
|
---|
| 289 | Канцелариски столови
|
---|
| 290 | </option>
|
---|
| 291 | )}
|
---|
| 292 | {category === "kancelarija" && (
|
---|
| 293 | <option value="gejmerski-stolovi">
|
---|
| 294 | Гејмерски столови
|
---|
| 295 | </option>
|
---|
| 296 | )}
|
---|
| 297 | {category === "kancelarija" && (
|
---|
| 298 | <option value="kancelariski-skafovi">
|
---|
| 299 | Канцелариски шкафови
|
---|
| 300 | </option>
|
---|
| 301 | )}
|
---|
| 302 | {category === "hodnik" && (
|
---|
| 303 | <option value="skafovi-za-cevli">Шкафови за чевли</option>
|
---|
| 304 | )}
|
---|
| 305 | {category === "hodnik" && (
|
---|
| 306 | <option value="zakacalki-i-ogledala">
|
---|
| 307 | Закачалки и огледала
|
---|
| 308 | </option>
|
---|
| 309 | )}
|
---|
| 310 | {category === "hodnik" && (
|
---|
| 311 | <option value="kolekcii-za-hodnik">
|
---|
| 312 | Колекции за ходник
|
---|
| 313 | </option>
|
---|
| 314 | )}
|
---|
| 315 | {category === "gradina" && (
|
---|
| 316 | <option value="gradinarski-kompleti">
|
---|
| 317 | Градинарски комплети
|
---|
| 318 | </option>
|
---|
| 319 | )}
|
---|
| 320 | {category === "gradina" && (
|
---|
| 321 | <option value="gradinarski-lulki">
|
---|
| 322 | Градинарски лулки
|
---|
| 323 | </option>
|
---|
| 324 | )}
|
---|
| 325 | {category === "gradina" && (
|
---|
| 326 | <option value="gradinarski-cadori">
|
---|
| 327 | Градинарски чадори
|
---|
| 328 | </option>
|
---|
| 329 | )}
|
---|
| 330 | {category === "gradina" && (
|
---|
| 331 | <option value="gradinarski-masi">Градинарски маси</option>
|
---|
| 332 | )}
|
---|
| 333 | {category === "gradina" && (
|
---|
| 334 | <option value="gradinarski-stolovi">
|
---|
| 335 | Градинарски столови
|
---|
| 336 | </option>
|
---|
| 337 | )}
|
---|
| 338 | {category === "gradina" && (
|
---|
| 339 | <option value="gradinarsko-osvetluvanje">
|
---|
| 340 | Градинарско осветлување
|
---|
| 341 | </option>
|
---|
| 342 | )}
|
---|
| 343 | {category === "trpezarija" && (
|
---|
| 344 | <option value="trpezariski-masi">Трпезариски маси</option>
|
---|
| 345 | )}
|
---|
| 346 | {category === "trpezarija" && (
|
---|
| 347 | <option value="trpezariski-stolovi">
|
---|
| 348 | Трпезариски столови
|
---|
| 349 | </option>
|
---|
| 350 | )}
|
---|
| 351 | {category === "trpezarija" && (
|
---|
| 352 | <option value="kujnski-garnituri">
|
---|
| 353 | Кујнски гарнитури
|
---|
| 354 | </option>
|
---|
| 355 | )}
|
---|
| 356 | {category === "trpezarija" && (
|
---|
| 357 | <option value="bar-stolovi-i-masi">
|
---|
| 358 | Бар столови и маси
|
---|
| 359 | </option>
|
---|
| 360 | )}
|
---|
| 361 | {category === "kujna" && (
|
---|
| 362 | <option value="kujnski-agolni-garnituri">
|
---|
| 363 | Кујнски аголни гарнитури
|
---|
| 364 | </option>
|
---|
| 365 | )}
|
---|
| 366 | {category === "kujna" && (
|
---|
| 367 | <option value="standardni-kujni">Стандардни кујни</option>
|
---|
| 368 | )}
|
---|
| 369 | {category === "detska" && (
|
---|
| 370 | <option value="kolekcii-za-detska-soba">
|
---|
| 371 | Колекции за детска соба
|
---|
| 372 | </option>
|
---|
| 373 | )}
|
---|
| 374 | {category === "detska" && (
|
---|
| 375 | <option value="detski-biroa">Детски бироа</option>
|
---|
| 376 | )}
|
---|
| 377 | {category === "detska" && (
|
---|
| 378 | <option value="detski-lezai">Детски лежаи</option>
|
---|
| 379 | )}
|
---|
| 380 | </Form.Select>
|
---|
| 381 | </Form.Group>*/}
|
---|
| 382 | <Form.Group>
|
---|
| 383 | <Form.Select
|
---|
| 384 | id="sortOrder"
|
---|
| 385 | onChange={(e) => {
|
---|
| 386 | navigate(getFilterUrl({ order: e.target.value }));
|
---|
| 387 | }}
|
---|
| 388 | >
|
---|
| 389 | <option value={"newest"}>Сортирај по цена</option>
|
---|
| 390 | <option value={"lowFirst"}>Од ниска кон висока</option>
|
---|
| 391 | <option value={"highFirst"}>Од висока кон ниска</option>
|
---|
| 392 | </Form.Select>
|
---|
| 393 | </Form.Group>
|
---|
[a2e5735] | 394 | <Form.Group>
|
---|
| 395 | <Button variant="danger" size="lg" type="submit">
|
---|
| 396 | Филтрирај
|
---|
| 397 | </Button>
|
---|
| 398 | </Form.Group>
|
---|
[113029b] | 399 | </Form>
|
---|
| 400 | </div>
|
---|
| 401 | <div
|
---|
| 402 | style={{
|
---|
| 403 | display: "flex",
|
---|
| 404 | flexWrap: "wrap",
|
---|
| 405 | justifyContent: "space-evenly",
|
---|
| 406 | alignItems: "center",
|
---|
| 407 | marginTop: "10px",
|
---|
| 408 | }}
|
---|
| 409 | >
|
---|
| 410 | {products.map((product) => (
|
---|
| 411 | <Product key={product.slug} product={product} />
|
---|
| 412 | ))}
|
---|
| 413 | </div>
|
---|
| 414 | <div
|
---|
| 415 | style={{
|
---|
| 416 | marginTop: "20px",
|
---|
| 417 | display: "flex",
|
---|
| 418 | justifyContent: "center",
|
---|
| 419 | alignItems: "center",
|
---|
| 420 | }}
|
---|
| 421 | >
|
---|
| 422 | {[...Array(pages).keys()].map((x) => (
|
---|
| 423 | <LinkContainer
|
---|
| 424 | key={x + 1}
|
---|
| 425 | className="mx-1"
|
---|
| 426 | to={getFilterUrl({ page: x + 1 })}
|
---|
| 427 | >
|
---|
| 428 | <Button
|
---|
| 429 | className={Number(page) === x + 1 ? "text-bold" : ""}
|
---|
| 430 | variant={Number(page) === x + 1 ? "danger" : "light"}
|
---|
| 431 | >
|
---|
| 432 | {x + 1}
|
---|
| 433 | </Button>
|
---|
| 434 | </LinkContainer>
|
---|
| 435 | ))}
|
---|
| 436 | </div>
|
---|
| 437 | </div>
|
---|
[b612ab1] | 438 | )}
|
---|
| 439 | </div>
|
---|
| 440 | </div>
|
---|
| 441 | );
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | export default Home;
|
---|