import React, { useState, useEffect, useCallback, useRef } from "react"; import { Link } from "react-router-dom"; import { useNavigate } from "react-router-dom"; import axios from "axios"; import Button from "@material-ui/core/Button"; import { isAutheticated } from "src/auth"; import swal from "sweetalert"; import debounce from "lodash.debounce"; const RetailDistributor = () => { const token = isAutheticated(); const Navigate = useNavigate(); const [loading, setLoading] = useState(false); const [allRetailDistributorsData, setAllRetailDistributorsData] = useState( [] ); const nameRef = useRef(); const principalDistributorRef = useRef(); const [totalPages, setTotalPages] = useState(1); const [currentPage, setCurrentPage] = useState(1); const [itemPerPage, setItemPerPage] = useState(10); const [totalData, setTotalData] = useState(0); const getRetailDistributorsData = async () => { setLoading(true); try { const res = await axios.get(`/api/kyc/getAllapproved/`, { headers: { Authorization: `Bearer ${token}`, }, params: { page: currentPage, show: itemPerPage, name: nameRef.current.value, principaldistributor: principalDistributorRef.current.value, }, }); setAllRetailDistributorsData(res.data?.kycs); setTotalData(res.data?.total_data); setTotalPages(res.data?.total_pages); } catch (err) { const msg = err?.response?.data?.message || "Something went wrong!"; swal({ title: "Error", text: msg, icon: "error", button: "Retry", dangerMode: true, }); } finally { setLoading(false); } }; useEffect(() => { getRetailDistributorsData(); }, [ itemPerPage, currentPage]); const debouncedSearch = useCallback(debounce(() => { setCurrentPage(1); getRetailDistributorsData(); }, 500), []); const handleSearchChange = () => { debouncedSearch(); }; return (
Retail Distributors
{loading ? ( ) : allRetailDistributorsData.length > 0 ? ( allRetailDistributorsData.map((retailDistributor) => ( )) ) : ( )}
ID Trade Name Created On Principal Distributor Territory Manager Sales Coordinator Action
Loading...
{retailDistributor._id} {retailDistributor.trade_name} {new Date( retailDistributor.updatedAt ).toLocaleString("en-IN", { weekday: "short", month: "short", day: "numeric", year: "numeric", hour: "numeric", minute: "numeric", hour12: true, })} {retailDistributor.principal_distributer ?.name || "N/A"} {retailDistributor.userType === "TerritoryManager" ? retailDistributor.addedBy?.name || "N/A" : "N/A"} {retailDistributor.userType === "SalesCoOrdinator" ? retailDistributor.addedBy?.name || "N/A" : "N/A"}
No Retail Distributor found!
); }; export default RetailDistributor;