Ignore:
Timestamp:
12/13/22 22:38:11 (19 months ago)
Author:
Nace Gjorgjievski <nace.gorgievski123@…>
Branches:
master
Parents:
113029b
Message:

Final Version

File:
1 edited

Legend:

Unmodified
Added
Removed
  • frontend/src/screens/CategoryScreen.js

    r113029b ra2e5735  
    1 import React, { useEffect, useReducer } from "react";
     1import React, { useEffect, useReducer, useState } from "react";
    22import "../styles/Home.css";
    33// import data from "./data";
     
    3434  const params = useParams();
    3535  const { category, subCategory } = params;
     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);
    3647  const navigate = useNavigate();
    3748  const { search } = useLocation();
     
    4354  const order = sp.get("order") || "newest";
    4455  const page = sp.get("page") || 1;
    45 
    4656  const [{ loading, error, products, pages, countProducts }, dispatch] =
    4757    useReducer(reducer, { loading: true, error: "" });
     
    5666        );*/
    5767        const { data } = await axios.get(
    58           `/api/products?page=${page}&query=${query}&category=${category}&subCategory=${subCategory}&order=${order}`
     68          `/api/products?page=${page}&query=${query}&category=${category}&subCategory=${subCategory}&order=${order}&HF=${HF}&HT=${HT}&WF=${WF}&WT=${WT}&LF=${LF}&LT=${LT}`
    5969        );
    6070        dispatch({ type: "FETCH_SUCCESS", payload: data });
     
    6474    };
    6575    fetchData();
    66   }, [category, page, query, order, subCategory, error]);
     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  ]);
    6790
    6891  const getFilterUrl = (filter) => {
     
    7295    const filterSubCategory = filter.subCategory || subCategory;
    7396    const sortOrder = filter.order || order;
    74     return `?category=${filterCategorry}&query=${filterQuery}&subCategory=${filterSubCategory}&page=${filterPage}&order=${sortOrder}`;
     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}&LT=${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=${LT}`
     124        );
     125        dispatch({ type: "FETCH_SUCCESS", payload: data });
     126      } catch (err) {
     127        dispatch({ type: "FETCH_FAIL", payload: getError(err) });
     128      }
     129    };
     130    fetchData();
    75131  };
    76132
     
    124180            >
    125181              <Form
     182                onSubmit={filterHandler}
    126183                style={{
    127184                  width: "100%",
    128185                  display: "flex",
    129                   justifyContent: "end",
     186                  justifyContent: "space-around",
    130187                  marginRight: "40px",
    131188                  alignItems: "center",
    132189                }}
    133190              >
     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>
    134233                {/*}
    135234                <Form.Group>
     
    292391                    <option value={"highFirst"}>Од висока кон ниска</option>
    293392                  </Form.Select>
     393                </Form.Group>
     394                <Form.Group>
     395                  <Button variant="danger" size="lg" type="submit">
     396                    Филтрирај
     397                  </Button>
    294398                </Form.Group>
    295399              </Form>
Note: See TracChangeset for help on using the changeset viewer.