diff --git a/src/pages/Directory/Directory.jsx b/src/pages/Directory/Directory.jsx index df3a1ac5..dfbcbb81 100644 --- a/src/pages/Directory/Directory.jsx +++ b/src/pages/Directory/Directory.jsx @@ -25,9 +25,12 @@ const Directory = () => { const [contactCategories, setContactCategories] = useState([]); const [searchText, setSearchText] = useState(""); const [listView, setListView] = useState(false); - const [ selectedBucketIds, setSelectedBucketIds ] = useState( [] ); - const [deleteContact,setDeleteContact] = useState(null) - const[IsDeleting,setIsDeletng] = useState(false) + const [selectedBucketIds, setSelectedBucketIds] = useState([]); + const [deleteContact, setDeleteContact] = useState(null); + const [IsDeleting, setIsDeletng] = useState(false); + + const [tempSelectedBucketIds, setTempSelectedBucketIds] = useState([]); + const [tempSelectedCategoryIds, setTempSelectedCategoryIds] = useState([]); const { contacts, loading } = useDirectory(); const { contactCategory, loading: contactCategoryLoading } = @@ -64,59 +67,64 @@ const Directory = () => { } }; - const handleDeleteContact = async() => - { - try - { setIsDeletng(true) - const contacts_cache = getCachedData( "contacts" ) || []; + const handleDeleteContact = async () => { + try { + setIsDeletng(true); + const contacts_cache = getCachedData("contacts") || []; - const response = await DirectoryRepository.DeleteContact( deleteContact ); - const updatedContacts = ContactList.filter((c) => c.id !== deleteContact); - setContactList( updatedContacts ); - cacheData("Contacts",updatedContacts) + const response = await DirectoryRepository.DeleteContact(deleteContact); + const updatedContacts = ContactList.filter((c) => c.id !== deleteContact); + setContactList(updatedContacts); + cacheData("Contacts", updatedContacts); showToast("Contact deleted successfully", "success"); - setDeleteContact( null ) - - setIsDeletng(false) - } catch ( error ) - { - const msg = error.response.data.message || error.message || "Error occured during API calling" - showToast( msg, "error" ) - setIsDeletng(false) - } - - } + setDeleteContact(null); + + setIsDeletng(false); + } catch (error) { + const msg = + error.response.data.message || + error.message || + "Error occured during API calling"; + showToast(msg, "error"); + setIsDeletng(false); + } + }; const closedModel = () => { setIsOpenModal(false); setSelectedContact(null); setOpen_contact(null); }; - useEffect(() => { - setContactList(contacts); - }, [contacts]); - const [selectedCategoryIds, setSelectedCategoryIds] = useState( contactCategory.map((category) => category.id) ); + useEffect(() => { + setContactList(contacts); + + // Set temp filter list only (UI checkboxes, not actual filtering yet) + setTempSelectedCategoryIds([]); + setTempSelectedBucketIds([]); + }, [contacts]); + const usedCategoryIds = [ ...new Set(contacts.map((c) => c.contactCategory?.id)), ]; const filteredCategories = contactCategory.filter((category) => usedCategoryIds.includes(category.id) ); - const handleCategoryChange = (id) => { - setSelectedCategoryIds((prev) => + const handleTempBucketChange = (id) => { + setTempSelectedBucketIds((prev) => + prev.includes(id) ? prev.filter((bid) => bid !== id) : [...prev, id] + ); + }; + + const handleTempCategoryChange = (id) => { + setTempSelectedCategoryIds((prev) => prev.includes(id) ? prev.filter((cid) => cid !== id) : [...prev, id] ); }; - const handleBucketChange = (id) => { - setSelectedBucketIds((prev) => - prev.includes(id) ? prev.filter((bid) => bid !== id) : [...prev, id] - ); - }; const usedBucketIds = [ ...new Set(contacts.flatMap((c) => c.bucketIds || [])), ]; @@ -143,6 +151,18 @@ const Directory = () => { }).sort((a, b) => a.name.localeCompare(b.name)); }, [ContactList, searchText, selectedCategoryIds, selectedBucketIds]); + const applyFilter = () => { + setSelectedBucketIds(tempSelectedBucketIds); + setSelectedCategoryIds(tempSelectedCategoryIds); + }; + + const clearFilter = () => { + setTempSelectedBucketIds([]); + setTempSelectedCategoryIds([]); + setSelectedBucketIds([]); + setSelectedCategoryIds([]); + }; + const { currentPage, totalPages, currentItems, paginate } = usePagination( filteredContacts, ITEMS_PER_PAGE @@ -171,7 +191,7 @@ const Directory = () => { @@ -206,7 +226,6 @@ const Directory = () => { )} {deleteContact && ( -
{ }} aria-hidden="false" > - setDeleteContact(null)} loading={IsDeleting} /> -
- )} + + )}
@@ -268,69 +287,108 @@ const Directory = () => {
-
- - + +
-
+
{!listView && loading &&

Loading...

} - {!listView && !loading && currentItems.length == 0 &&

Not Found Contact

} + {!listView && !loading && currentItems.length == 0 && ( +

No Matching Contact Found

+ )} {listView ? (
@@ -392,10 +452,7 @@ const Directory = () => { - + @@ -404,13 +461,12 @@ const Directory = () => { )} - {!loading && - currentItems.length === 0 && ( - - - - )} - + {!loading && currentItems.length === 0 && ( + + + + )} + {!loading && currentItems.map((contact) => ( { contact={contact} setSelectedContact={setSelectedContact} setIsOpenModal={setIsOpenModal} - setOpen_contact={setOpen_contact} - setIsOpenModalNote={setIsOpenModalNote} - IsDeleted={setDeleteContact} + setOpen_contact={setOpen_contact} + setIsOpenModalNote={setIsOpenModalNote} + IsDeleted={setDeleteContact} /> ))} @@ -446,7 +502,7 @@ const Directory = () => { )} - {!loading && ( + {!loading && currentItems < ITEMS_PER_PAGE && (
Category - Action - Action
Loading...
Not Found Contact
No Matching Contact Found