diff --git a/src/Context/DireContext.jsx b/src/Context/DireContext.jsx new file mode 100644 index 00000000..a8e842e9 --- /dev/null +++ b/src/Context/DireContext.jsx @@ -0,0 +1,21 @@ +import React, { createContext, useContext, useState } from "react"; + +const DireContext = createContext(undefined); + +export const DireProvider = ({ children }) => { + const [dirActions, setDirActions] = useState([]); + + return ( + + {children} + + ); +}; + +export const useDir = () => { + const context = useContext(DireContext); + if (!context) { + throw new Error("useDir must be used within a "); + } + return context; +}; diff --git a/src/components/Directory/CardViewDirectory.jsx b/src/components/Directory/CardViewDirectory.jsx index 1dc16cec..33fcad6f 100644 --- a/src/components/Directory/CardViewDirectory.jsx +++ b/src/components/Directory/CardViewDirectory.jsx @@ -3,6 +3,7 @@ import Avatar from "../common/Avatar"; import { getBucketNameById } from "./DirectoryUtils"; import { useBuckets } from "../../hooks/useDirectory"; import { getPhoneIcon } from "./DirectoryUtils"; +import { useDir } from "../../Context/DireContext"; const CardViewDirectory = ({ IsActive, contact, @@ -11,8 +12,10 @@ const CardViewDirectory = ({ setOpen_contact, setIsOpenModalNote, IsDeleted, + restore, }) => { const { buckets } = useBuckets(); + const { dirActions, setDirActions } = useDir(); return ( {" "} - {contact.name} + {contact.name} - - - - - - { - setSelectedContact(contact); - setIsOpenModal(true); - }} + {IsActive && ( + + - - - Modify - - - - IsDeleted(contact.id)} + + + + { + setSelectedContact(contact); + setIsOpenModal(true); + }} > - - Delete - - - - + + + Modify + + + + IsDeleted(contact.id)} + > + + Delete + + + + + )} + {!IsActive && ( + { + setDirActions({ action: false, id: contact.id }); + restore(contact.id); + }} + > + )} - + {contact.organization} @@ -97,7 +116,7 @@ const CardViewDirectory = ({ {contact.contactEmails[0] && ( - + {contact.contactEmails[0]?.emailAddress} @@ -122,27 +141,28 @@ const CardViewDirectory = ({ - - + {contact.contactCategory.name} - - {contact.bucketIds?.map((bucketId) => ( - - - - - {getBucketNameById(buckets, bucketId)} - - - - ))} - - + + {contact.bucketIds?.map((bucketId) => ( + + + + + {getBucketNameById(buckets, bucketId)} + + + + ))} + ); diff --git a/src/components/Directory/ListViewDirectory.jsx b/src/components/Directory/ListViewDirectory.jsx index 20636c2e..d227d99d 100644 --- a/src/components/Directory/ListViewDirectory.jsx +++ b/src/components/Directory/ListViewDirectory.jsx @@ -1,74 +1,128 @@ -import React from 'react' -import Avatar from '../common/Avatar'; -import { getEmailIcon,getPhoneIcon } from './DirectoryUtils'; +import React, { useEffect } from "react"; +import Avatar from "../common/Avatar"; +import { getEmailIcon, getPhoneIcon } from "./DirectoryUtils"; +import { useDir } from "../../Context/DireContext"; +const ListViewDirectory = ({ + IsActive, + contact, + setSelectedContact, + setIsOpenModal, + setOpen_contact, + setIsOpenModalNote, + IsDeleted, + restore, +}) => { + const { dirActions, setDirActions } = useDir(); -const ListViewDirectory = ({IsActive, contact,setSelectedContact,setIsOpenModal,setOpen_contact,setIsOpenModalNote,IsDeleted}) => { return ( - - { - if (IsActive) { - setIsOpenModalNote(true); - setOpen_contact(contact); - } - }}> - - - {contact?.name || ""} - - - - - - {contact.contactEmails?.map((email, index) => ( - - - {email.emailAddress} - - ))} - - - - - - {contact.contactPhones?.map((phone, index) => ( - - - {phone.phoneNumber} - - ))} - - - - - {contact.organization} - - - - - {contact?.contactCategory?.name} - - - - {IsActive && ( - - + { - setSelectedContact(contact); - setIsOpenModal(true); - }}> - IsDeleted(contact.id)}> - - )} - + if (IsActive) { + setIsOpenModalNote(true); + setOpen_contact(contact); + } + }} + > + + + + {contact?.name || ""} + + + + + + {contact.contactEmails?.map((email, index) => ( + + + + {email.emailAddress} + + + ))} + + + + + + {contact.contactPhones?.map((phone, index) => ( + + + {phone.phoneNumber} + + ))} + + + + + {contact.organization} + + + + + {contact?.contactCategory?.name} + + + + + {IsActive && ( + <> + { + setSelectedContact(contact); + setIsOpenModal(true); + }} + > + IsDeleted(contact.id)} + > + > + )} + {!IsActive && ( + { + setDirActions({ action: false, id: contact.id }); + restore(contact.id); + }} + > + )} + + ); }; -export default ListViewDirectory; \ No newline at end of file +export default ListViewDirectory; diff --git a/src/pages/Directory/Directory.jsx b/src/pages/Directory/Directory.jsx index b19eecf3..a9ceab49 100644 --- a/src/pages/Directory/Directory.jsx +++ b/src/pages/Directory/Directory.jsx @@ -19,6 +19,7 @@ import DirectoryListTableHeader from "./DirectoryListTableHeader"; import DirectoryPageHeader from "./DirectoryPageHeader"; import ManageBucket from "../../components/Directory/ManageBucket"; import {useFab} from "../../Context/FabContext"; +import {DireProvider, useDir} from "../../Context/DireContext"; const Directory = () => { @@ -33,12 +34,15 @@ const Directory = () => const [listView, setListView] = useState(false); const [selectedBucketIds, setSelectedBucketIds] = useState([]); const [deleteContact, setDeleteContact] = useState(null); - const [ IsDeleting, setIsDeletng ] = useState( false ); - const [openBucketModal,setOpenBucketModal] = useState(false) + const [ IsDeleting, setDeleting ] = useState( false ); + const [ openBucketModal, setOpenBucketModal ] = useState( false ) + const [tempSelectedBucketIds, setTempSelectedBucketIds] = useState([]); const [ tempSelectedCategoryIds, setTempSelectedCategoryIds ] = useState( [] ); - const {setActions} = useFab() + const {setActions} = useFab() + const { dirActions, setDirActions } = useDir(); + const { contacts, loading , refetch} = useDirectory(IsActive); const { contactCategory, loading: contactCategoryLoading } = @@ -78,28 +82,43 @@ const Directory = () => } }; - const handleDeleteContact = async () => { - try { - setIsDeletng(true); - const contacts_cache = getCachedData("contacts")?.data || []; - - const response = await DirectoryRepository.DeleteContact(deleteContact); - const updatedContacts = ContactList.filter( ( c ) => c.id !== deleteContact ); - setContactList(updatedContacts); - cacheData("Contacts", {data:updatedContacts,isActive:IsActive}); - 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); +const handleDeleteContact = async (overrideId = null) => { + try + { + if (!IsActive) { + setDirActions((prev) => ({ ...prev, action: true })); + } else { + setDeleting(true); } - }; + const id = overrideId || (!IsActive ? dirActions.id : deleteContact); + if (!id) { + showToast("No contact selected for deletion", "error"); + return; + } + + await DirectoryRepository.DeleteContact(id, !IsActive); + + const updatedContacts = ContactList.filter((c) => c.id !== id); + setContactList(updatedContacts); + cacheData("Contacts", { data: updatedContacts, isActive: IsActive }); + + showToast(`Contact ${IsActive ? "Deleted":"Restored"} successfully`, "success"); + + setDeleteContact(null); + setDirActions({ action: false, id: null }); + setDeleting(false); + } catch (error) { + const msg = + error?.response?.data?.message || + error.message || + "Error occurred during API call"; + showToast(msg, "error"); + + setDeleting(false); + setDirActions({ action: false, id: null }); + } +}; + const closedModel = () => { setIsOpenModal(false); @@ -306,7 +325,6 @@ const Directory = () => applyFilter={applyFilter} loading={loading} IsActive={IsActive} - setIsOpenModal={setIsOpenModal} setOpenBucketModal={setOpenBucketModal} /> {!listView && loading && Loading...} @@ -314,7 +332,7 @@ const Directory = () => No Matching Contact Found )} {listView ? ( - + {loading && ( Loading... @@ -336,6 +354,7 @@ const Directory = () => setOpen_contact={setOpen_contact} setIsOpenModalNote={setIsOpenModalNote} IsDeleted={setDeleteContact} + restore={handleDeleteContact} /> ))} @@ -354,15 +373,16 @@ const Directory = () => setOpen_contact={setOpen_contact} setIsOpenModalNote={setIsOpenModalNote} IsDeleted={setDeleteContact} + restore={handleDeleteContact} /> ))} )} - - + + - {!loading && currentItems < ITEMS_PER_PAGE && ( + {!loading && currentItems < ITEMS_PER_PAGE && ( )} - + ); };
Loading...
No Matching Contact Found