From f9c3c136b397dc16719294cf53e33d70d7645087 Mon Sep 17 00:00:00 2001 From: Pramod Mahajan Date: Tue, 20 May 2025 18:09:18 +0530 Subject: [PATCH] created useContactNote hook --- src/hooks/useDirectory.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/hooks/useDirectory.js b/src/hooks/useDirectory.js index 93a64058..7d18a554 100644 --- a/src/hooks/useDirectory.js +++ b/src/hooks/useDirectory.js @@ -99,3 +99,42 @@ const fetchContactProfile = async () => { return { conatProfile, loading, Error }; } + + +export const useContactNotes = (id) => +{ + const [ conatNotes, setContactNotes ] = useState( [] ); + const [ loading, setLoading ] = useState( false ); + const [ Error, setError ] = useState( "" ); + + +const fetchContactNotes = async () => { + const cached = getCachedData("Contact Notes"); + + if (!cached || cached.contactId !== id) { + setLoading(true); + try { + const resp = await DirectoryRepository.GetNote(id); + setContactNotes(resp.data); + cacheData("Contact Notes", { data: resp.data, contactId: id }); + } catch (err) { + const msg = + err?.response?.data?.message || err?.message || "Something went wrong"; + setError(msg); + } finally { + setLoading(false); + } + } else { + setContactNotes(cached.data); + } + }; + + useEffect(() => { + if ( id ) + { + fetchContactNotes(id); + } + }, [id]); + + return { conatProfile, loading, Error }; +}