diff --git a/src/pages/Directory/Directory.jsx b/src/pages/Directory/Directory.jsx index d66e8651..8ec2c23e 100644 --- a/src/pages/Directory/Directory.jsx +++ b/src/pages/Directory/Directory.jsx @@ -23,14 +23,13 @@ const Directory = () => { const [ContactList, setContactList] = useState([]); const [contactCategories, setContactCategories] = useState([]); const [searchText, setSearchText] = useState(""); - const [ listView, setListView ] = useState( false ); + const [listView, setListView] = useState(false); const [selectedBucketIds, setSelectedBucketIds] = useState([]); - const { contacts, loading } = useDirectory(); const { contactCategory, loading: contactCategoryLoading } = useContactCategory(); - const {buckets} = useBuckets() + const { buckets } = useBuckets(); const submitContact = async (data) => { try { let response; @@ -88,35 +87,35 @@ const Directory = () => { }; const handleBucketChange = (id) => { - setSelectedBucketIds((prev) => - prev.includes(id) ? prev.filter((bid) => bid !== id) : [...prev, id] + setSelectedBucketIds((prev) => + prev.includes(id) ? prev.filter((bid) => bid !== id) : [...prev, id] + ); + }; + const usedBucketIds = [ + ...new Set(contacts.flatMap((c) => c.bucketIds || [])), + ]; + + const filteredBuckets = buckets.filter((bucket) => + usedBucketIds.includes(bucket.id) ); -}; -const usedBucketIds = [ - ...new Set(contacts.flatMap((c) => c.bucketIds || [])), -]; -const filteredBuckets = buckets.filter((bucket) => - usedBucketIds.includes(bucket.id) -); + const filteredContacts = useMemo(() => { + return ContactList.filter((c) => { + const matchesSearch = + c.name.toLowerCase().includes(searchText.toLowerCase()) || + c.organization.toLowerCase().includes(searchText.toLowerCase()); -const filteredContacts = useMemo(() => { - return ContactList.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); - const matchesCategory = - selectedCategoryIds.length === 0 || - selectedCategoryIds.includes(c.contactCategory?.id); + const matchesBucket = + selectedBucketIds.length === 0 || + (c.bucketIds || []).some((id) => selectedBucketIds.includes(id)); - const matchesBucket = - selectedBucketIds.length === 0 || - (c.bucketIds || []).some((id) => selectedBucketIds.includes(id)); - - return matchesSearch && matchesCategory && matchesBucket; - }).sort((a, b) => a.name.localeCompare(b.name)); -}, [ContactList, searchText, selectedCategoryIds, selectedBucketIds]); + return matchesSearch && matchesCategory && matchesBucket; + }).sort((a, b) => a.name.localeCompare(b.name)); + }, [ContactList, searchText, selectedCategoryIds, selectedBucketIds]); const { currentPage, totalPages, currentItems, paginate } = usePagination( filteredContacts, @@ -153,10 +152,9 @@ const filteredContacts = useMemo(() => { {isOpenModal && ( - { - setSelectedContact(null) - setIsOpenModal(false) + closeModal={() => { + setSelectedContact(null); + setIsOpenModal(false); }} size="lg" > @@ -166,14 +164,19 @@ const filteredContacts = useMemo(() => { {isOpenModalNote && ( - { - setOpen_contact(null) - setIsOpenModalNote(false) + closeModal={() => { + setOpen_contact(null); + setIsOpenModalNote(false); }} size="lg" > - {open_contact && setIsOpenModalNote(false)} />} + {open_contact && ( + setIsOpenModalNote(false)} + /> + )} )}
@@ -224,49 +227,57 @@ const filteredContacts = useMemo(() => { > - +
@@ -282,6 +293,7 @@ const filteredContacts = useMemo(() => { {!listView && loading &&

Loading...

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

Not Found Contact

} {listView ? (
@@ -332,9 +344,6 @@ const filteredContacts = useMemo(() => { @@ -347,12 +356,12 @@ const filteredContacts = useMemo(() => { )} {!loading && - contacts.length == 0 && - ContatList.length === 0 && ( + currentItems.length === 0 && ( - + )} + {!loading && currentItems.map((contact) => (
Category Action
No Contact FoundNot Found Contact