352 lines
12 KiB
JavaScript
352 lines
12 KiB
JavaScript
import React, { useEffect, useMemo, useState } from "react";
|
|
import Breadcrumb from "../../components/common/Breadcrumb";
|
|
import IconButton from "../../components/common/IconButton";
|
|
import GlobalModel from "../../components/common/GlobalModel";
|
|
import ManageDirectory from "../../components/Directory/ManageDirectory";
|
|
import ListViewDirectory from "../../components/Directory/ListViewDirectory";
|
|
import { useDirectory } from "../../hooks/useDirectory";
|
|
import { DirectoryRepository } from "../../repositories/DirectoryRepository";
|
|
import { getCachedData } from "../../slices/apiDataManager";
|
|
import showToast from "../../services/toastService";
|
|
import UpdateContact from "../../components/Directory/UpdateContact";
|
|
import CardViewDirectory from "../../components/Directory/CardViewDirectory";
|
|
import { useContactCategory } from "../../hooks/masterHook/useMaster";
|
|
import usePagination from "../../hooks/usePagination";
|
|
|
|
const Directory = () => {
|
|
const [isOpenModal, setIsOpenModal] = useState(false);
|
|
const [selectedContact, setSelectedContact] = useState(null);
|
|
const [ContatList, setContactList] = useState([]);
|
|
const [contactCategories, setContactCategories] = useState([]);
|
|
const [ searchText, setSearchText ] = useState( "" );
|
|
const [listView, setListView] = useState(true);
|
|
|
|
const { contacts, loading } = useDirectory();
|
|
const { contactCategory, loading: contactCategoryLoading } =
|
|
useContactCategory();
|
|
const submitContact = async (data) => {
|
|
try {
|
|
let response;
|
|
let updatedContacts;
|
|
const contacts_cache = getCachedData("contacts") || [];
|
|
|
|
if (selectedContact) {
|
|
response = await DirectoryRepository.UpdateContact(data.id, data);
|
|
updatedContacts = contacts_cache.map((contact) =>
|
|
contact.id === data.id ? response.data : contact
|
|
);
|
|
showToast("Contact updated successfully", "success");
|
|
setIsOpenModal(false);
|
|
setSelectedContact(null);
|
|
} else {
|
|
response = await DirectoryRepository.CreateContact(data);
|
|
updatedContacts = [...contacts_cache, response.data];
|
|
showToast("Contact created successfully", "success");
|
|
setIsOpenModal(false);
|
|
}
|
|
|
|
setContactList(updatedContacts);
|
|
} catch (error) {
|
|
const msg =
|
|
error.response?.data?.message ||
|
|
error.message ||
|
|
"Error occurred during API call!";
|
|
showToast(msg, "error");
|
|
}
|
|
};
|
|
|
|
const closedModel = () => {
|
|
setIsOpenModal(false);
|
|
setSelectedContact(null);
|
|
};
|
|
useEffect(() => {
|
|
setContactList(contacts);
|
|
}, [contacts]);
|
|
|
|
const [selectedCategoryIds, setSelectedCategoryIds] = useState(
|
|
contactCategory.map((category) => category.id)
|
|
);
|
|
|
|
const usedCategoryIds = [
|
|
...new Set(contacts.map((c) => c.contactCategory?.id)),
|
|
];
|
|
const filteredCategories = contactCategory.filter((category) =>
|
|
usedCategoryIds.includes(category.id)
|
|
);
|
|
const handleCategoryChange = (id) => {
|
|
setSelectedCategoryIds((prev) =>
|
|
prev.includes(id) ? prev.filter((cid) => cid !== id) : [...prev, id]
|
|
);
|
|
};
|
|
const filteredContacts = useMemo(() => {
|
|
return ContatList
|
|
.filter((c) => {
|
|
const matchesSearch =
|
|
c.name.toLowerCase().includes(searchText.toLowerCase()) ||
|
|
c.organization.toLowerCase().includes(searchText.toLowerCase());
|
|
const matchesCategory =
|
|
selectedCategoryIds.length === 0 ||
|
|
selectedCategoryIds.includes(c.contactCategory?.id);
|
|
return matchesSearch && matchesCategory;
|
|
})
|
|
.sort((a, b) => a.name.localeCompare(b.name));
|
|
}, [ContatList, searchText, selectedCategoryIds]);
|
|
const { currentPage, totalPages, currentItems, paginate } = usePagination(
|
|
filteredContacts,
|
|
10
|
|
);
|
|
|
|
return (
|
|
<div className="container-xxl flex-grow-1 container-p-y">
|
|
<Breadcrumb
|
|
data={[
|
|
{ label: "Home", link: "/dashboard" },
|
|
{ label: "Directory (Comming Soon)", link: null },
|
|
]}
|
|
></Breadcrumb>
|
|
|
|
{isOpenModal && (
|
|
<GlobalModel
|
|
isOpen={isOpenModal}
|
|
closeModal={() => setIsOpenModal(false)}
|
|
size="lg"
|
|
>
|
|
{selectedContact ? (
|
|
<UpdateContact
|
|
existingContact={selectedContact}
|
|
submitContact={submitContact}
|
|
onCLosed={closedModel}
|
|
/>
|
|
) : (
|
|
<ManageDirectory
|
|
submitContact={submitContact}
|
|
onCLosed={closedModel}
|
|
/>
|
|
)}
|
|
</GlobalModel>
|
|
)}
|
|
<div className="card p-2">
|
|
<div className="row mx-0 px-0 align-items-center">
|
|
<div className="col-7 col-md-4 mb-2 px-1 d-flex align-items-center ">
|
|
<input
|
|
type="search"
|
|
className="form-control form-control-sm me-2"
|
|
placeholder="Search Contact..."
|
|
value={searchText}
|
|
onChange={(e) => setSearchText(e.target.value)}
|
|
/>
|
|
<div className="d-flex gap-2 ">
|
|
<button
|
|
type="button"
|
|
className={`btn btn-xs ${
|
|
!listView ? "btn-primary" : "btn-outline-primary"
|
|
}`}
|
|
onClick={() => setListView(false)}
|
|
data-bs-toggle="tooltip"
|
|
data-bs-offset="0,8"
|
|
data-bs-placement="top"
|
|
data-bs-custom-class="tooltip"
|
|
title="Card View"
|
|
>
|
|
<i className="bx bx-grid-alt bx-sm"></i>
|
|
</button>
|
|
<button
|
|
type="button"
|
|
className={`btn btn-xs ${
|
|
listView ? "btn-primary" : "btn-outline-primary"
|
|
}`}
|
|
onClick={() => setListView(true)}
|
|
data-bs-toggle="tooltip"
|
|
data-bs-offset="0,8"
|
|
data-bs-placement="top"
|
|
data-bs-custom-class="tooltip"
|
|
title="List View"
|
|
>
|
|
<i className="bx bx-list-ul bx-sm"></i>
|
|
</button>
|
|
</div>
|
|
<div className="dropdown">
|
|
<a
|
|
className="dropdown-toggle hide-arrow cursor-pointer d-flex align-items-center"
|
|
data-bs-toggle="dropdown"
|
|
aria-expanded="false"
|
|
>
|
|
<i className="bx bx-filter bx-lg ms-1"></i>
|
|
</a>
|
|
<ul className="dropdown-menu p-2 text-capitalize">
|
|
<p className="small-text">Apply Filter</p>
|
|
{filteredCategories.map(({ id, name }) => (
|
|
<li key={id}>
|
|
<div className="form-check">
|
|
<input
|
|
className="form-check-input"
|
|
type="checkbox"
|
|
id={`cat-${id}`}
|
|
checked={selectedCategoryIds.includes(id)}
|
|
onChange={() => handleCategoryChange(id)}
|
|
/>
|
|
<label className="form-check-label">{name}</label>
|
|
</div>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="col-5 col-md-8 mb-2 px-1 text-md-end text-end">
|
|
<button
|
|
type="button"
|
|
className="btn btn-sm btn-primary"
|
|
onClick={() => setIsOpenModal(true)}
|
|
>
|
|
<i className="bx bx-plus-circle me-2"></i>
|
|
<span className="d-sm-block d-none"> New Contact</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
{
|
|
listView ? (
|
|
<div className="table-responsive text-nowrap py-2 ">
|
|
<table className="table px-2">
|
|
<thead>
|
|
<tr>
|
|
<th colSpan={2}>
|
|
<div className="d-flex align-items-center gap-1">
|
|
<IconButton
|
|
size={12}
|
|
iconClass="bx bx-user"
|
|
color="secondary"
|
|
onClick={() => alert("User icon clicked")}
|
|
/>
|
|
<span>Name</span>
|
|
</div>
|
|
</th>
|
|
<th className="px-2 text-start">
|
|
<div className="d-flex text-center align-items-center gap-1 justify-content-start">
|
|
<IconButton
|
|
size={12}
|
|
iconClass="bx bx-envelope"
|
|
color="primary"
|
|
/>
|
|
<span>Email</span>
|
|
</div>
|
|
</th>
|
|
|
|
<th className="mx-2">
|
|
<div className="d-flex align-items-center m-0 p-0 gap-1">
|
|
<IconButton
|
|
size={12}
|
|
iconClass="bx bx-phone"
|
|
color="warning"
|
|
onClick={() => alert("User icon clicked")}
|
|
/>
|
|
<span>Phone</span>
|
|
</div>
|
|
</th>
|
|
<th className="mx-2">
|
|
<div className="d-flex align-items-center gap-1">
|
|
<IconButton
|
|
size={12}
|
|
iconClass="bx bxs-grid-alt"
|
|
color="info"
|
|
/>
|
|
<span>Organization</span>
|
|
</div>
|
|
</th>
|
|
<th className="mx-2">Category</th>
|
|
<th
|
|
// className={`mx-2 ${
|
|
// HasManageProject ? "d-sm-table-cell" : "d-none"
|
|
// }`}
|
|
>
|
|
Action
|
|
</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="table-border-bottom-0 overflow-auto ">
|
|
{loading && ContatList.length === 0 && (
|
|
<tr>
|
|
<td colSpan={10}>Loading...</td>
|
|
</tr>
|
|
)}
|
|
{!loading && contacts.length == 0 && ContatList.length === 0 && (
|
|
<tr>
|
|
<td colSpan={10}>No Contact Found</td>
|
|
</tr>
|
|
)}
|
|
{!loading &&
|
|
currentItems.map((contact) => (
|
|
<ListViewDirectory
|
|
contact={contact}
|
|
setSelectedContact={setSelectedContact}
|
|
setIsOpenModal={setIsOpenModal}
|
|
/>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
) : (
|
|
<div className="row">
|
|
{currentItems.map((item, index) => (
|
|
<div key={index} className="col-12 col-sm-6 col-md-4 col-lg-4 mb-4">
|
|
<CardViewDirectory contact={item} />
|
|
</div>
|
|
))}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{!loading && (
|
|
<nav aria-label="Page ">
|
|
<ul className="pagination pagination-sm justify-content-end py-1">
|
|
<li
|
|
className={`page-item ${currentPage === 1 ? "disabled" : ""}`}
|
|
>
|
|
<button
|
|
className="page-link btn-xs"
|
|
onClick={() => paginate(currentPage - 1)}
|
|
>
|
|
«
|
|
</button>
|
|
</li>
|
|
{[...Array(totalPages)].map((_, index) => (
|
|
<li
|
|
key={index}
|
|
className={`page-item ${
|
|
currentPage === index + 1 ? "active" : ""
|
|
}`}
|
|
>
|
|
<button
|
|
className="page-link "
|
|
onClick={() => paginate(index + 1)}
|
|
>
|
|
{index + 1}
|
|
</button>
|
|
</li>
|
|
))}
|
|
<li
|
|
className={`page-item ${
|
|
currentPage === totalPages ? "disabled" : ""
|
|
}`}
|
|
>
|
|
<button
|
|
className="page-link "
|
|
onClick={() => paginate(currentPage + 1)}
|
|
>
|
|
»
|
|
</button>
|
|
</li>
|
|
</ul>
|
|
</nav>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default Directory;
|