From d10cbdd4bdafb5d43dd0baf2e31306af5df972fd Mon Sep 17 00:00:00 2001 From: Pramod Mahajan Date: Sun, 18 May 2025 02:25:42 +0530 Subject: [PATCH] added update contact modal --- src/pages/Directory/Directory.jsx | 75 +++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 24 deletions(-) diff --git a/src/pages/Directory/Directory.jsx b/src/pages/Directory/Directory.jsx index 7de59ce5..f389167c 100644 --- a/src/pages/Directory/Directory.jsx +++ b/src/pages/Directory/Directory.jsx @@ -8,35 +8,49 @@ 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"; const Directory = () => { const [isOpenModal, setIsOpenModal] = useState(false); - const closedModel = () => setIsOpenModal(false); + const [selectedContact, setSelectedContact] = useState(null); const [ContatList, setContactList] = useState([]); const { contacts, loading } = useDirectory(); - const submitContact = async (data, setLoading, reset) => { - try - { - debugger - const resp = await DirectoryRepository.CreateContact(data) + const submitContact = async (data) => { + try { + let response; + let updatedContacts; const contacts_cache = getCachedData("contacts") || []; - const updated_contacts = [...contacts_cache, resp.data]; - setContactList(updated_contacts); - showToast(resp.message || "Contact created successfully", "success"); - setLoading(false); - reset(); - setIsOpenModal(false); + if (selectedContact) { + setSelectedContact(null); + 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); + } 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.response?.data?.message || error.message || - "Error Occured during api calling !"; + "Error occurred during API call!"; showToast(msg, "error"); } }; + const closedModel = () => { + setIsOpenModal(false); + setSelectedContact(null); + }; useEffect(() => { setContactList(contacts); }, [contacts]); @@ -50,11 +64,23 @@ const Directory = () => { > {isOpenModal && ( - setIsOpenModal(false)} size="lg"> - + setIsOpenModal(false)} + size="lg" + > + {selectedContact ? ( + + ) : ( + + )} )} @@ -93,7 +119,7 @@ const Directory = () => { Name - +
{ - {(loading && ContatList.length === 0) && ( + {loading && ContatList.length === 0 && ( Loading... )} - {(!loading && contacts.length == 0 && ContatList.length === 0) && ( + {!loading && contacts.length == 0 && ContatList.length === 0 && ( No Contact Found - )} + )} {!loading && ContatList.map((contact) => ( ))}