import React, { useState, useEffect, useRef, useCallback } from "react"; import { Link, useParams } from "react-router-dom"; import axios from "axios"; import Button from "@material-ui/core/Button"; import { useNavigate } from "react-router-dom"; import { isAutheticated } from "src/auth"; import swal from "sweetalert"; import debounce from "lodash.debounce"; import { Typography,Paper } from "@mui/material"; const DistributorStocks = () => { const token = isAutheticated(); const navigate = useNavigate(); const { id } = useParams(); const { distributortype } = useParams(); const [loading, setLoading] = useState(false); const [productsData, setProductsData] = useState([]); const [categories, setCategories] = useState([]); const [brands, setBrands] = useState([]); const [user, setUser] = useState(null); const nameRef = useRef(); const categoryRef = useRef(); const brandRef = useRef(); const [currentPage, setCurrentPage] = useState(1); const [itemPerPage, setItemPerPage] = useState(10); const [totalData, setTotalData] = useState(0); // Fetch User Details const getUserDetails = useCallback(async () => { try { const response = await axios.get( distributortype === "principaldistributor" ? `/api/v1/admin/user/${id}` : `/api/getRD/${id}`, { headers: { Authorization: `Bearer ${token}`, }, } ); distributortype === "principaldistributor" ? setUser(response.data.user) : setUser(response.data); } catch (error) { swal({ title: "Warning", text: error.message, icon: "error", button: "Close", dangerMode: true, }); } }, [id, token]); // Call getUserDetails on component mount useEffect(() => { getUserDetails(); }, [getUserDetails]); const getProductsData = async () => { setLoading(true); try { const response = await axios.get( distributortype === "principaldistributor" ? `/api/pd/stock/${id}` : `/api/rd/stock/${id}`, { headers: { Authorization: `Bearer ${token}`, }, params: { page: currentPage, show: itemPerPage, name: nameRef.current?.value || "", category: categoryRef.current?.value || "", brand: brandRef.current?.value || "", }, }); // console.log(response.data); setProductsData(response.data?.products || []); setTotalData(response.data?.totalProducts || 0); } catch (err) { const msg = err?.response?.data?.msg || "Something went wrong!"; swal({ title: "Error", text: msg, icon: "error", button: "Retry", dangerMode: true, }); } finally { setLoading(false); } }; const getCatagories = () => { axios .get(`/api/category/getCategories`, { headers: { "Access-Control-Allow-Origin": "*", Authorization: `Bearer ${token}`, }, }) .then((res) => { // console.log(res?.data?.categories); setCategories(res?.data?.categories); }); }; const getBrands = () => { axios .get(`/api/brand/getBrands`, { headers: { "Access-Control-Allow-Origin": "*", Authorization: `Bearer ${token}`, }, }) .then((res) => { // console.log(res?.data?.brands); setBrands(res?.data?.brands); }); }; const [currencyDetails, setCurrencyDetails] = useState(null); const getCurrency = async () => { try { const response = await axios.get("/api/currency/getall", { // headers: { // Authorization: `Bearer ${token}`, // }, }); if (response.status === 200) { setCurrencyDetails(response?.data?.currency[0]); } } catch (error) { console.log(error); } }; useEffect(() => { getCatagories(); getCurrency(); getBrands(); }, []); useEffect(() => { getProductsData(); }, [itemPerPage, currentPage]); const debouncedSearch = useCallback( debounce(() => { setCurrentPage(1); getProductsData(); }, 500), [] ); const handleSearchChange = () => { debouncedSearch(); }; const handleCancel = () => { navigate( distributortype === "principaldistributor" ? "/principal-distributor" : "/retail-distributor" ); }; return (
Image | SKU | Product | Category Name | Brand Name | Price | Added On | Stock |
---|---|---|---|---|---|---|---|
Loading... | |||||||
{product?.image &&
product?.image?.length !== 0 ? (
<>
No image uploaded! |
{product.SKU} | {product.name} | {product.category !== "" ? product.category : "Category Not selected "} | {product.brand !== "" ? product.brand : "Brand Not selected "} | {currencyDetails?.CurrencySymbol} {product?.price} | {new Date(product.createdAt).toLocaleString( "en-IN", { weekday: "short", month: "short", day: "numeric", year: "numeric", hour: "numeric", minute: "numeric", hour12: true, } )} | {product.stock} |
No Product Available... |