diff --git a/src/index.js b/src/index.js index 8a829f2..0ab534a 100644 --- a/src/index.js +++ b/src/index.js @@ -15,9 +15,9 @@ import { cibGmail } from "@coreui/icons"; import { createRoot } from "react-dom/client"; const setupAxios = () => { - axios.defaults.baseURL = "http://localhost:5000"; + // axios.defaults.baseURL = "http://localhost:5000"; // axios.defaults.baseURL = "https://cheminova-api-2.onrender.com"; - // axios.defaults.baseURL = "https://api.cnapp.co.in"; + axios.defaults.baseURL = "https://api.cnapp.co.in"; axios.defaults.headers = { "Cache-Control": "no-cache,no-store", diff --git a/src/routes.js b/src/routes.js index 88e1013..becc2c8 100644 --- a/src/routes.js +++ b/src/routes.js @@ -150,6 +150,7 @@ import ViewRetailDistributorTM from "./views/TerritoryManager/ViewRetailDistribu import AddRetailDistributor from "./views/RetailDistributors/addRetailDistributor"; import ViewPrincipalDistributorSC from "./views/SalesCoOrdinators/ViewPrincipalDistributorSC"; import ViewRetailDistributorSC from "./views/SalesCoOrdinators/ViewRetailDistributorSC"; +import ViewRetailDistributorPD from "./views/PrincipalDistributors/ViewRetailDistributorPD"; const routes = [ //dashboard @@ -409,6 +410,12 @@ const routes = [ element: AddMultiplePd, navName: "PrincipalDistributor", }, + { + path: "/view/mappedretaildistributor/:id", + name: "view retail distributor", + element: ViewRetailDistributorPD, + navName: "PrincipalDistributor", + }, //Inventory { path: "/inventory", diff --git a/src/views/PrincipalDistributors/ViewRetailDistributorPD.js b/src/views/PrincipalDistributors/ViewRetailDistributorPD.js new file mode 100644 index 0000000..1c6f785 --- /dev/null +++ b/src/views/PrincipalDistributors/ViewRetailDistributorPD.js @@ -0,0 +1,577 @@ +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 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 ViewRetailDistributorPD = () => { + const token = isAutheticated(); + const { id } = useParams(); + const navigate = useNavigate(); + const [loading, setLoading] = useState(false); + const [success, setSuccess] = useState(true); + const [retaildistributorData, setretaildistributorData] = useState([]); + const [data, setData] = useState({}); + const nameRef = useRef(); + const mobileRef = useRef(); + const rdnameRef = useRef(); + const rdmobileRef = 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 [modalRetailDistributors, setmodalRetailDistributors] = useState([]); + const [modalTotalData, setmodalTotalData] = useState(0); + + // Fetch territory manager data + useEffect(() => { + axios + .get(`/api/v1/admin/user/${id}`, { + headers: { Authorization: `Bearer ${token}` }, + }) + .then((response) => setData(response.data.user)) + .catch((error) => console.error("Error fetching sc data:", error)); + }, [id, token]); + + // Fetch Retail Distributors data + const getPDsretaildistributorData = async () => { + setLoading(true); + axios + .get(`/api/getAllRDbypdid/${id}`, { + headers: { + Authorization: `Bearer ${token}`, + }, + params: { + page: currentPage, + show: itemPerPage, + tradename: nameRef.current?.value, + mobile_number: mobileRef.current?.value, + }, + }) + .then((res) => { + // console.log(res.data); + setretaildistributorData(res.data?.Retaildistributor); + 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(() => { + getPDsretaildistributorData(); + }, [success, itemPerPage, currentPage]); + + // Debounced search for Retail Distributors + const debouncedSearch = useCallback( + debounce(() => { + setCurrentPage(1); + getPDsretaildistributorData(); + }, 500), + [currentPage, itemPerPage] + ); + + const handleSearchChange = useCallback(() => { + debouncedSearch(); + }, [debouncedSearch]); + // Fetch Retail Distributors data for modal + const getretaildistributorData = async () => { + setLoading(true); + try { + const res = await axios.get(`/api/getAllRD`, { + headers: { + Authorization: `Bearer ${token}`, + }, + params: { + page: modalcurrentPage, + show: modalitemPerPage, + tradename: rdnameRef.current?.value, + mobile_number: rdmobileRef.current?.value, + }, + }); + setmodalRetailDistributors(res.data?.Retaildistributor); + setmodalTotalData(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(() => { + if (openModal) { + getretaildistributorData(); + } + }, [openModal, modalcurrentPage]); + + // Debounced search for Retail Distributors in modal + const debouncedmodalSearch = useCallback( + debounce(() => { + setmodalCurrentPage(1); + getretaildistributorData(); + }, 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 (modalRetailDistributors.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/unmap/${id}`, + { principal_distributor: true }, + { + // Changed to PATCH and sent an empty body + headers: { + "Access-Control-Allow-Origin": "*", + Authorization: `Bearer ${token}`, + }, + } + ) + .then((res) => { + swal({ + title: "Deleted", + text: "Retail 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 handleAddRetailDistributor = async (rdid) => { + try { + await axios.put( + `/api/mapped/${rdid}`, + { principal_distributor: id }, + { + headers: { + Authorization: `Bearer ${token}`, + }, + } + ); + swal({ + title: "Success", + text: "Retail 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 ( +
UniqueID | +Trade Name | +Mobile | +Created On | +Action | +|
---|---|---|---|---|---|
+ Loading... + | +|||||
{RD?.uniqueId} | ++ {RD?.kycDetails?.trade_name} + | ++ {RD?.mobile_number ? ( + RD?.mobile_number + ) : ( + + No Phone Added! + + )} + | + +{RD?.email} | ++ {" "} + {new Date(RD.createdAt).toLocaleString( + "en-IN", + { + weekday: "short", + month: "short", + day: "numeric", + year: "numeric", + hour: "numeric", + minute: "numeric", + hour12: true, + } + )} + | ++ + | +
+ No Retail Distributor found! + | +