import React, { useState, useEffect, useRef, useCallback } from "react"; import axios from "axios"; import { Box, Typography, Grid, Paper, Dialog, DialogContent, DialogTitle, Button, TextField, DialogActions, } from "@mui/material"; import { isAutheticated } from "../../auth"; import swal from "sweetalert"; import debounce from "lodash.debounce"; const PDmodal = ({ openPDModal, handleClosePDModal, refreshData, rdid }) => { const token = isAutheticated(); const [modalPrincipalDistributors, setmodalPrincipalDistributors] = useState( [] ); const [modalTotalData, setModalTotalData] = useState(0); const [modalcurrentPage, setmodalCurrentPage] = useState(1); const [modalitemPerPage, setmodalItemPerPage] = useState(10); const [loading, setLoading] = useState(false); const pdnameRef = useRef(); const pdmobileRef = useRef(); const getprincipaldistributorData = async () => { setLoading(true); try { const res = await axios.get(`/api/v1/admin/users`, { headers: { Authorization: `Bearer ${token}`, }, params: { page: modalcurrentPage, show: modalitemPerPage, name: pdnameRef.current?.value, mobileNumber: pdmobileRef.current?.value, }, }); setmodalPrincipalDistributors(res.data?.users); setModalTotalData(res.data?.totalUsers); } 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); } }; const handlePDMap = (id) => { const data = { principal_distributor: id, }; axios .put(`/api/mapped/${rdid}`, data, { headers: { Authorization: `Bearer ${token}`, }, }) .then((res) => { swal({ title: "Success", text: "Principal Distributor mapped successfully!", icon: "success", button: "Ok", }); refreshData(); // Call the refresh function handleClosePDModal(); }) .catch((err) => { const msg = err?.response?.data?.message || "Something went wrong!"; swal({ title: "Error", text: msg, icon: "error", button: "Retry", dangerMode: true, }); }); }; useEffect(() => { getprincipaldistributorData(); }, [modalcurrentPage]); const handleInputChange = useCallback( debounce(() => { getprincipaldistributorData(); }, 1000), [] ); const handleNextPage = () => { setmodalCurrentPage((prev) => prev + 1); }; const handlePreviousPage = () => { setmodalCurrentPage((prev) => prev - 1); }; return ( Map Principal Distributor Search and Add Principal Distributor
{modalPrincipalDistributors.length > 0 ? ( modalPrincipalDistributors.map((PD) => ( )) ) : ( )}
Id SBU Name Mobile Email SC TM Action
{PD.uniqueId} {PD.SBU} {PD.name} {PD.phone} {PD.email} {PD.mappedbySC?.name || "N/A"} {PD.mappedby?.name || "N/A"}
No Principal Distributor found!
Showing {modalPrincipalDistributors.length} of {modalTotalData}{" "} entries
); }; export default PDmodal;