diff --git a/src/hooks/useDirectory.js b/src/hooks/useDirectory.js index 21c4a320..93a64058 100644 --- a/src/hooks/useDirectory.js +++ b/src/hooks/useDirectory.js @@ -61,3 +61,41 @@ export const useBuckets = () => { return { buckets, loading, error }; }; + +export const useContactProfile = (id) => +{ + const [ conatProfile, setContactProfile ] = useState( null ); + const [ loading, setLoading ] = useState( false ); + const [ Error, setError ] = useState( "" ); + + +const fetchContactProfile = async () => { + const cached = getCachedData("Contact Profile"); + + if (!cached || cached.contactId !== id) { + setLoading(true); + try { + const resp = await DirectoryRepository.GetContactProfile(id); + setContactProfile(resp.data); + cacheData("Contact Profile", { data: resp.data, contactId: id }); + } catch (err) { + const msg = + err?.response?.data?.message || err?.message || "Something went wrong"; + setError(msg); + } finally { + setLoading(false); + } + } else { + setContactProfile(cached.data); + } + }; + + useEffect(() => { + if ( id ) + { + fetchContactProfile(id); + } + }, [id]); + + return { conatProfile, loading, Error }; +}