import axios from "axios"; import React, { useEffect, useReducer, useState } from "react"; import { Link, useNavigate, useLocation } from "react-router-dom"; import ListGroup from "react-bootstrap/ListGroup"; import Button from "react-bootstrap/Button"; import Form from "react-bootstrap/Form"; import { getError } from "../components/utils"; import { toast } from "react-toastify"; import LoadingBox from "../components/LoadingBox"; import MessageBox from "../components/MessageBox"; import LinkContainer from "react-router-bootstrap/LinkContainer"; const reducer = (state, action) => { switch (action.type) { case "FETCH_REQUEST": return { ...state, loading: true }; case "FETCH_SUCCESS": return { ...state, products: action.payload.products, page: action.payload.page, pages: action.payload.pages, countProducts: action.payload.countProducts, loading: false, }; case "FETCH_FAIL": return { ...state, loading: false, error: action.payload }; default: return state; } }; function AdminProductsScreen() { const navigate = useNavigate(); const { search } = useLocation(); const sp = new URLSearchParams(search); const category = sp.get("category") || "all"; const query = sp.get("query") || "all"; const subCategory = sp.get("subCategory") || "all"; //const name = sp.get("name") || "all"; const order = sp.get("order") || "newest"; const page = sp.get("page") || 1; const [{ loading, error, products, pages, countProducts }, dispatch] = useReducer(reducer, { loading: true, error: "" }); useEffect(() => { const fetchData = async () => { try { dispatch({ type: "FETCH_REQUEST" }); /* const { data } = await axios.get( `/api/products/search?page=${page}&query=${query}&category=${category}&subCategory=${subCategory}&order=${order}` );*/ const { data } = await axios.get( `/api/products?page=${page}&query=${query}&category=${category}&subCategory=${subCategory}&order=${order}` ); dispatch({ type: "FETCH_SUCCESS", payload: data }); } catch (err) { dispatch({ type: "FETCH_FAIL", payload: getError(err) }); } }; fetchData(); }, [category, page, query, order, subCategory, error]); const getFilterUrl = (filter) => { const filterPage = filter.page || page; const filterCategorry = filter.category || category; const filterQuery = filter.query || query; const filterSubCategory = filter.subCategory || subCategory; const sortOrder = filter.order || order; return `?category=${filterCategorry}&query=${filterQuery}&subCategory=${filterSubCategory}&page=${filterPage}&order=${sortOrder}`; }; return (
{loading ? ( ) : error ? ( {error} ) : (
Додади нов производ
Додади категорија
Производи
Нарачки

Производи

{/* */} { if (e.target.value === "all") { navigate( getFilterUrl({ page: 1, category: e.target.value, subCategory: "all", }) ); } else { navigate( getFilterUrl({ category: e.target.value, page: 1 }) ); } }} > { navigate( getFilterUrl({ subCategory: e.target.value, page: 1 }) ); }} > {category === "dnevna" && ( )} {category === "dnevna" && ( )} {category === "dnevna" && ( )} {category === "dnevna" && ( )} {category === "dnevna" && ( )} {category === "dnevna" && ( )} {category === "dnevna" && ( )} {category === "spalna" && ( )} {category === "spalna" && ( )} {category === "spalna" && ( )} {category === "spalna" && ( )} {category === "spalna" && ( )} {category === "spalna" && ( )} {category === "kancelarija" && ( )} {category === "kancelarija" && ( )} {category === "kancelarija" && ( )} {category === "kancelarija" && ( )} {category === "hodnik" && ( )} {category === "hodnik" && ( )} {category === "hodnik" && ( )} {category === "gradina" && ( )} {category === "gradina" && ( )} {category === "gradina" && ( )} {category === "gradina" && ( )} {category === "gradina" && ( )} {category === "gradina" && ( )} {category === "trpezarija" && ( )} {category === "trpezarija" && ( )} {category === "trpezarija" && ( )} {category === "trpezarija" && ( )} {category === "kujna" && ( )} {category === "kujna" && ( )} {category === "detska" && ( )} {category === "detska" && ( )} {category === "detska" && ( )} { navigate(getFilterUrl({ order: e.target.value })); }} >
{products.map((item) => (
{item.name} {item.name}
{item.category} {item.subCategory} {item.countInStock} {item.price} ден
))}
{[...Array(pages).keys()].map((x) => ( ))}
)}
); } export default AdminProductsScreen;