added new hook - useContactProfile
This commit is contained in:
parent
3a6be65a48
commit
69abe1bd42
@ -61,3 +61,41 @@ export const useBuckets = () => {
|
|||||||
|
|
||||||
return { buckets, loading, error };
|
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 };
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user