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 "@mui/material"; import { isAutheticated } from "src/auth"; import swal from "sweetalert"; import debounce from "lodash.debounce"; import { toast } from "react-hot-toast"; 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/getAllRDandorder`, { headers: { Authorization: `Bearer ${token}`, }, params: { page: currentPage, show: itemPerPage, tradename: nameRef.current.value, principaldistributor: principalDistributorRef.current.value, }, }); // console.log(res.data); setAllRetailDistributorsData(res.data?.Retaildistributor); 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(); }; const handleDownloadReport = async () => { try { const response = await axios.get( `/api/retaildistributor/download-report`, { headers: { Authorization: `Bearer ${token}`, }, responseType: "arraybuffer", } ); // console.log(response); // Check if the request was successful if (response.status === 200) { // Convert response data into a Blob for download const url = window.URL.createObjectURL( new Blob([response.data], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", }) ); // Create a temporary download link and trigger the download const link = document.createElement("a"); link.href = url; link.download = "RetailerReport.xlsx"; document.body.appendChild(link); link.click(); // Cleanup document.body.removeChild(link); window.URL.revokeObjectURL(url); // Release Blob URL memory // Show success message toast.success("Report downloaded successfully!"); } else { // If the response status is not 200, display an error message throw new Error("Failed to download report"); } } catch (err) { // Display a user-friendly error message const msg = err?.response?.data?.message || "Something went wrong!"; swal({ title: "Error", text: msg, icon: "error", button: "Retry", dangerMode: true, }); } }; const handleReset = async (id) => { try { const response = await axios.put( `/api/rd/reset-password/${id}`, {}, // No body content required for this request { headers: { Authorization: `Bearer ${token}`, }, } ); if (response.data.success) { toast.success( "Password reset successfully! Email sent to Retailer." ); } } catch (error) { toast.error( error.response?.data?.message || "Failed to reset the password. Please try again." ); } }; return (
Retailers
{loading ? ( ) : allRetailDistributorsData.length > 0 ? ( allRetailDistributorsData.map((retailDistributor) => ( )) ) : ( )}
ID Trade Name Created On Principal Distributor Territory Manager Sales Coordinator Orders Mapping Action
Loading...
{retailDistributor.uniqueId} {retailDistributor.kycDetails.trade_name} {new Date( retailDistributor.createdAt ).toLocaleString("en-IN", { // weekday: "short", month: "short", day: "numeric", year: "numeric", // hour: "numeric", // minute: "numeric", // hour12: true, })} {retailDistributor?.principalDetails?.name || "N/A"} {retailDistributor?.mappedTMDetails?.name || "N/A"} {retailDistributor?.mappedSCDetails?.name || "N/A"} {/* On large screens: View and Stock buttons in one row */}
{/* On large screens: OI and Edit buttons in one row */}
No Retailer found!
); }; export default RetailDistributor;