import React, { useState, useEffect, useRef, useCallback } from "react"; import { Link, useParams } from "react-router-dom"; import axios from "axios"; import { Button } from "@mui/material"; import { useNavigate } from "react-router-dom"; import { isAutheticated } from "src/auth"; import swal from "sweetalert"; import debounce from "lodash.debounce"; import Dialog from "@material-ui/core/Dialog"; import DialogActions from "@material-ui/core/DialogActions"; import DialogContent from "@material-ui/core/DialogContent"; import DialogTitle from "@material-ui/core/DialogTitle"; import TextField from "@material-ui/core/TextField"; const ViewPrincipalDistributorTM = () => { const token = isAutheticated(); const { id } = useParams(); const navigate = useNavigate(); const [loading, setLoading] = useState(false); const [success, setSuccess] = useState(true); const [principaldistributorData, setprincipaldistributorData] = useState([]); const [data, setData] = useState({}); const nameRef = useRef(); const mobileRef = useRef(); const pdnameRef = useRef(); const pdmobileRef = useRef(); const [currentPage, setCurrentPage] = useState(1); const [itemPerPage, setItemPerPage] = useState(10); const [modalcurrentPage, setmodalCurrentPage] = useState(1); const modalitemPerPage = 10; const [totalData, setTotalData] = useState(0); const [openModal, setOpenModal] = useState(false); const [modalPrincipalDistributors, setmodalPrincipalDistributors] = useState( [] ); const [modalTotalData, setModalTotalData] = useState(0); // Fetch territory manager data useEffect(() => { axios .get(`/api/territorymanager/getOne/${id}`, { headers: { Authorization: `Bearer ${token}` }, }) .then((response) => setData(response.data.data)) .catch((error) => console.error("Error fetching TM data:", error)); }, [id, token]); // Fetch Principal Distributors data const getTMsprincipaldistributorData = async () => { setLoading(true); axios .get(`/api/v1/getbyTmId/${id}`, { headers: { Authorization: `Bearer ${token}`, }, params: { page: currentPage, show: itemPerPage, name: nameRef.current?.value, mobileNumber: mobileRef.current?.value, }, }) .then((res) => { setprincipaldistributorData(res.data?.principaldistributor); setTotalData(res.data?.total_data); }) .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(() => { getTMsprincipaldistributorData(); }, [success, itemPerPage, currentPage]); // Debounced search for Principal Distributors const debouncedSearch = useCallback( debounce(() => { setCurrentPage(1); getTMsprincipaldistributorData(); }, 500), [currentPage, itemPerPage] ); const handleSearchChange = useCallback(() => { debouncedSearch(); }, [debouncedSearch]); // Fetch Principal Distributors data for modal 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); } }; useEffect(() => { if (openModal) { getprincipaldistributorData(); } }, [openModal, modalcurrentPage]); // Debounced search for Principal Distributors in modal const debouncedmodalSearch = useCallback( debounce(() => { setmodalCurrentPage(1); getprincipaldistributorData(); }, 500), [modalcurrentPage] ); const handlemodalSearchChange = useCallback(() => { debouncedmodalSearch(); }, [debouncedmodalSearch]); const handleOpenModal = () => { setOpenModal(true); }; const handleCloseModal = () => { setOpenModal(false); }; const handlePreviousPage = () => { if (modalcurrentPage > 1) { setmodalCurrentPage(modalcurrentPage - 1); } }; const handleNextPage = () => { if (modalPrincipalDistributors.length === modalitemPerPage) { setmodalCurrentPage(modalcurrentPage + 1); } }; const handleDelete = (id) => { swal({ title: "Are you sure?", icon: "warning", buttons: { Yes: { text: "Yes", value: true }, Cancel: { text: "Cancel", value: "cancel" }, }, }).then((value) => { if (value === true) { axios .patch(`/api/v1/unmap/${id}`, {}, { // Changed to PATCH and sent an empty body headers: { "Access-Control-Allow-Origin": "*", Authorization: `Bearer ${token}`, }, }) .then((res) => { swal({ title: "Deleted", text: "Principal Distributor Unmapped successfully!", icon: "success", button: "ok", }); setSuccess((prev) => !prev); }) .catch((err) => { let msg = err?.response?.data?.message ? err?.response?.data?.message : "Something went wrong!"; swal({ title: "Warning", text: msg, icon: "error", button: "Retry", dangerMode: true, }); }); } }); }; const handleAddPrincipalDistributor = async (pdid) => { try { await axios.put( `/api/v1/mappedtm/${pdid}`, { mappedby: id }, { headers: { Authorization: `Bearer ${token}`, }, } ); swal({ title: "Success", text: "Principal Distributor added successfully!", icon: "success", button: "Ok", }); setSuccess((prev) => !prev); handleCloseModal(); // Close modal after adding } catch (err) { const msg = err?.response?.data?.message || "Something went wrong!"; swal({ title: "Error", text: msg, icon: "error", button: "Retry", dangerMode: true, }); } }; return (
Unique Id | SBU | Name | Mobile No. | Action | |
---|---|---|---|---|---|
Loading... | |||||
{PD?.uniqueId} | {PD?.SBU} | {PD?.name} | {PD?.phone} | {PD?.email ? ( PD?.email ) : ( No Email Added! )} | |
No Principal Distributor found! |