diff --git a/src/routes.js b/src/routes.js index ceab322..e42e5ba 100644 --- a/src/routes.js +++ b/src/routes.js @@ -154,6 +154,7 @@ import ViewRetailDistributorPD from "./views/PrincipalDistributors/ViewRetailDis import MapRD from "./views/RetailDistributors/MapRD"; import PendingOrders from "./views/orders/pendingOrders"; import ViewInvoices from "./views/orders/viewInoices"; +import Stocks from "./views/PrincipalDistributors/Stock"; const routes = [ //dashboard @@ -425,6 +426,12 @@ const routes = [ element: ViewRetailDistributorPD, navName: "PrincipalDistributor", }, + { + path: "/pd-stock/:id", + name: "view Stock", + element: Stocks, + navName: "PrincipalDistributor", + }, //Inventory { path: "/inventory", diff --git a/src/views/PrincipalDistributors/Stock.js b/src/views/PrincipalDistributors/Stock.js new file mode 100644 index 0000000..cc743da --- /dev/null +++ b/src/views/PrincipalDistributors/Stock.js @@ -0,0 +1,513 @@ +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 } from "@mui/material"; +const Stocks = () => { + const token = isAutheticated(); + const navigate = useNavigate(); + const { id } = 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(`/api/v1/admin/user/${id}`, { + headers: { + Authorization: `Bearer ${token}`, + }, + }); + setUser(response.data.user); + } 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(`/api/pd/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(); + }; + 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...+ |
+