319 lines
13 KiB
JavaScript
319 lines
13 KiB
JavaScript
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/getAllRD`, {
|
|
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();
|
|
};
|
|
|
|
return (
|
|
<div className="main-content">
|
|
<div className="page-content">
|
|
<div className="container-fluid">
|
|
<div className="row">
|
|
<div className="col-12">
|
|
<div className="page-title-box d-flex align-items-center justify-content-between">
|
|
<div style={{ fontSize: "22px" }} className="fw-bold">
|
|
Retail Distributors
|
|
</div>
|
|
<div className="page-title-right">
|
|
<Button
|
|
variant="contained"
|
|
color="primary"
|
|
className="font-bold mb-2 capitalize mr-2"
|
|
onClick={() => {
|
|
Navigate("/retaildistributor/add");
|
|
}}
|
|
>
|
|
Add Retail Distributor
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="row">
|
|
<div className="col-lg-12">
|
|
<div className="card">
|
|
<div className="card-body">
|
|
<div className="row ml-0 mr-0 mb-10">
|
|
<div className="col-lg-1">
|
|
<div className="dataTables_length">
|
|
<label className="w-100">
|
|
Show
|
|
<select
|
|
onChange={(e) => {
|
|
setItemPerPage(Number(e.target.value));
|
|
setCurrentPage(1); // Reset to first page when changing items per page
|
|
}}
|
|
className="form-control"
|
|
disabled={loading}
|
|
>
|
|
<option value="10">10</option>
|
|
<option value="25">25</option>
|
|
<option value="50">50</option>
|
|
<option value="100">100</option>
|
|
</select>
|
|
entries
|
|
</label>
|
|
</div>
|
|
</div>
|
|
<div className="col-lg-3">
|
|
<label>Trade Name:</label>
|
|
<input
|
|
type="text"
|
|
name="searchTerm"
|
|
placeholder="Trade name"
|
|
className="form-control"
|
|
ref={nameRef}
|
|
onChange={handleSearchChange}
|
|
disabled={loading}
|
|
/>
|
|
</div>
|
|
<div className="col-lg-3">
|
|
<label>Principal Distributor Name:</label>
|
|
<input
|
|
type="text"
|
|
name="searchPrincipalDistributor"
|
|
placeholder="Principal Distributor name"
|
|
className="form-control"
|
|
onChange={handleSearchChange}
|
|
disabled={loading}
|
|
ref={principalDistributorRef}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="table-responsive table-shoot mt-3">
|
|
<table className="table table-centered table-nowrap">
|
|
<thead
|
|
className="thead-light"
|
|
style={{ background: "#ecdddd" }}
|
|
>
|
|
<tr>
|
|
<th>ID</th>
|
|
<th className="text-start">Trade Name</th>
|
|
<th className="text-start">Created On</th>
|
|
<th className="text-start">Principal Distributor</th>
|
|
<th className="text-start">Territory Manager</th>
|
|
<th className="text-start">Sales Coordinator</th>
|
|
<th className="text-start">Mapping</th>
|
|
<th className="text-start">Action</th>
|
|
</tr>
|
|
</thead>
|
|
|
|
<tbody>
|
|
{loading ? (
|
|
<tr>
|
|
<td className="text-center" colSpan="7">
|
|
Loading...
|
|
</td>
|
|
</tr>
|
|
) : allRetailDistributorsData.length > 0 ? (
|
|
allRetailDistributorsData.map((retailDistributor) => (
|
|
<tr key={retailDistributor._id}>
|
|
<td className="text-start">
|
|
{retailDistributor.uniqueId}
|
|
</td>
|
|
<td className="text-start">
|
|
{retailDistributor.kycDetails.trade_name}
|
|
</td>
|
|
<td className="text-start">
|
|
{new Date(
|
|
retailDistributor.createdAt
|
|
).toLocaleString("en-IN", {
|
|
weekday: "short",
|
|
month: "short",
|
|
day: "numeric",
|
|
year: "numeric",
|
|
hour: "numeric",
|
|
minute: "numeric",
|
|
hour12: true,
|
|
})}
|
|
</td>
|
|
<td className="text-start">
|
|
{retailDistributor?.principalDetails
|
|
?.name || "N/A"}
|
|
</td>
|
|
<td className="text-start">
|
|
{retailDistributor?.mappedTMDetails?.name || "N/A"}
|
|
</td>
|
|
<td className="text-start">
|
|
{retailDistributor?.mappedSCDetails?.name || "N/A"}
|
|
</td>
|
|
<td className="text-start">
|
|
<Link
|
|
to={`/retaildistributor/mapping/${retailDistributor._id}`}
|
|
>
|
|
<button
|
|
style={{
|
|
color: "white",
|
|
marginRight: "1rem",
|
|
}}
|
|
type="button"
|
|
className="btn btn-primary btn-sm waves-effect waves-light btn-table ml-2"
|
|
>
|
|
Map
|
|
</button>
|
|
</Link>
|
|
</td>
|
|
<td className="text-start">
|
|
<Link
|
|
to={`/retaildistributor/view/${retailDistributor._id}`}
|
|
>
|
|
<Button variant="contained" color="primary">
|
|
View
|
|
</Button>
|
|
</Link>
|
|
</td>
|
|
</tr>
|
|
))
|
|
) : (
|
|
<tr>
|
|
<td className="text-center" colSpan="7">
|
|
No Retail Distributor found!
|
|
</td>
|
|
</tr>
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div className="row mt-20">
|
|
<div className="col-sm-12 col-md-6 mb-20">
|
|
<div
|
|
className="dataTables_info"
|
|
id="datatable_info"
|
|
role="status"
|
|
aria-live="polite"
|
|
>
|
|
Showing {allRetailDistributorsData?.length} of {totalData} entries
|
|
</div>
|
|
</div>
|
|
|
|
<div className="col-sm-12 col-md-6">
|
|
<div className="dataTables_paginate paging_simple_numbers">
|
|
<ul className="pagination">
|
|
<li
|
|
className={`paginate_button page-item previous ${
|
|
currentPage === 1 ? "disabled" : ""
|
|
}`}
|
|
>
|
|
<a
|
|
className="page-link"
|
|
onClick={() =>
|
|
setCurrentPage((prev) => Math.max(prev - 1, 1))
|
|
}
|
|
aria-controls="datatable"
|
|
>
|
|
Previous
|
|
</a>
|
|
</li>
|
|
{Array.from({ length: totalPages }, (_, index) => (
|
|
<li
|
|
key={index}
|
|
className={`paginate_button page-item ${
|
|
currentPage === index + 1 ? "active" : ""
|
|
}`}
|
|
>
|
|
<a
|
|
className="page-link"
|
|
onClick={() => setCurrentPage(index + 1)}
|
|
aria-controls="datatable"
|
|
>
|
|
{index + 1}
|
|
</a>
|
|
</li>
|
|
))}
|
|
<li
|
|
className={`paginate_button page-item next ${
|
|
currentPage === totalPages ? "disabled" : ""
|
|
}`}
|
|
>
|
|
<a
|
|
className="page-link"
|
|
onClick={() =>
|
|
setCurrentPage((prev) =>
|
|
Math.min(prev + 1, totalPages)
|
|
)
|
|
}
|
|
aria-controls="datatable"
|
|
>
|
|
Next
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default RetailDistributor;
|