diff --git a/src/views/Brands/Brands.js b/src/views/Brands/Brands.js index bed3291..0a0dd7d 100644 --- a/src/views/Brands/Brands.js +++ b/src/views/Brands/Brands.js @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from "react"; +import React, { useState, useEffect, useRef, useCallback } from "react"; import axios from "axios"; import { isAutheticated } from "src/auth"; import { @@ -13,7 +13,8 @@ import { import CloseIcon from "@mui/icons-material/Close"; import { ClipLoader } from "react-spinners"; import swal from "sweetalert"; - +import { toast } from "react-hot-toast"; +import debounce from "lodash.debounce"; const style = { position: "absolute", top: "50%", @@ -28,54 +29,65 @@ const style = { const Brands = () => { const token = isAutheticated(); + const nameRef = useRef(); const [loading, setLoading] = useState(true); const [updating, setUpdating] = useState(true); - const [saveLoding, setSaveLoading] = useState(true); + const [saveLoading, setSaveLoading] = useState(true); const [edit, setEdit] = useState(false); - const [brandName, setbrandName] = useState(""); - const [brandId, setbrandId] = useState(""); - const [brand, setbrand] = useState([]); + const [brandName, setBrandName] = useState(""); + const [brandId, setBrandId] = useState(""); + const [brands, setBrands] = useState([]); const [itemPerPage, setItemPerPage] = useState(10); const [page, setPage] = useState(1); const [open, setOpen] = useState(false); - const [olderbrandName, setOlderBrandName] = useState(""); - + const [olderBrandName, setOlderBrandName] = useState(""); const handleOpen = () => setOpen(true); const handleClose = () => { setOpen(false); setEdit(false); - setbrandName(""); - setbrandId(""); + setBrandName(""); + setBrandId(""); }; const getBrands = async () => { try { - const response = await axios.get("/api/brand/getBrands"); + setLoading(true); // Set loading to true before fetching + const response = await axios.get("/api/brand/getBrands", { + params: { + page, + show: itemPerPage, + brandName: nameRef.current?.value || "", + }, // Include pagination and search + }); if (response.status === 200) { - setbrand(response.data.brands); - setLoading(false); + setBrands(response.data.brands); } } catch (error) { console.error("Failed to fetch brands:", error); + } finally { + setLoading(false); // Set loading to false after fetching } }; useEffect(() => { getBrands(); - }, []); + }, [page, itemPerPage]); // Trigger fetch when these values change const handleEditClick = (_id, brandName) => { setOpen(true); - setbrandName(brandName); - setbrandId(_id); + setBrandName(brandName); + setBrandId(_id); setOlderBrandName(brandName); setEdit(true); }; const handleUpdate = async () => { - const filteredBrandNames = brand - .filter((brand) => brand.brandName.toLowerCase() !== olderbrandName.toLowerCase()) + const filteredBrandNames = brands + .filter( + (brand) => + brand.brandName.toLowerCase() !== olderBrandName.toLowerCase() + ) .map((brand) => brand.brandName.toLowerCase()); if (filteredBrandNames.includes(brandName.toLowerCase())) { @@ -97,7 +109,7 @@ const Brands = () => { headers: { Authorization: `Bearer ${token}` }, }); handleClose(); - swal("Success", "The brand was updated successfully!", "success"); + toast.success("Brand updated successfully"); getBrands(); } catch (err) { swal("Error", "Failed to update brand", "error"); @@ -120,7 +132,7 @@ const Brands = () => { await axios.delete(`/api/brand/delete/${_id}`, { headers: { Authorization: `Bearer ${token}` }, }); - swal("Success", "The brand was deleted successfully!", "success"); + toast.success("Brand deleted successfully"); getBrands(); } catch (err) { swal("Error", "Failed to delete brand", "error"); @@ -129,8 +141,12 @@ const Brands = () => { }); }; - const handleSavebrand = async () => { - if (brand.some((brand) => brand.brandName.toLowerCase() === brandName.toLowerCase())) { + const handleSaveBrand = async () => { + if ( + brands.some( + (brand) => brand.brandName.toLowerCase() === brandName.toLowerCase() + ) + ) { swal("Warning", "Brand already exists.", "error"); return; } @@ -148,7 +164,7 @@ const Brands = () => { await axios.post("/api/brand/add", formData, { headers: { Authorization: `Bearer ${token}`, - "Content-Type": "multipart/formdata", + "Content-Type": "multipart/form-data", }, }); handleClose(); @@ -161,8 +177,19 @@ const Brands = () => { } }; - const getPageCount = () => Math.max(1, Math.ceil(brand.length / itemPerPage)); + const getPageCount = () => + Math.max(1, Math.ceil(brands.length / itemPerPage)); + const debouncedSearch = useCallback( + debounce(() => { + setPage(1); + getBrands(); + }, 500), + [] + ); + const handleSearchChange = () => { + debouncedSearch(); + }; return (