From 16389bf102f0014d9e7d91835a3f6d65fc3fbb9c Mon Sep 17 00:00:00 2001 From: Pramod Mahajan Date: Wed, 28 May 2025 12:25:02 +0530 Subject: [PATCH] modified useBucket for refetch data --- src/hooks/useDirectory.js | 118 ++++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 61 deletions(-) diff --git a/src/hooks/useDirectory.js b/src/hooks/useDirectory.js index 5d113183..df352b98 100644 --- a/src/hooks/useDirectory.js +++ b/src/hooks/useDirectory.js @@ -37,47 +37,46 @@ export const useDirectory = (isActive) => { }; }; - - export const useBuckets = () => { const [buckets, setBuckets] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); const fetchBuckets = async () => { - const cacheBuckets = getCachedData("buckets"); - if (!cacheBuckets) { - setLoading( true ); - try { - const resp = await DirectoryRepository.GetBucktes(); - setBuckets(resp.data); - cacheData( "buckets", resp.data ); - setLoading(false); - } catch (error) { - const msg = - error?.response?.data?.message || error?.message || "Something went wrong"; - setError(msg); - } - } else { - setBuckets(cacheBuckets); + setLoading(true); + try { + const resp = await DirectoryRepository.GetBucktes(); + setBuckets(resp.data); + cacheData("buckets", resp.data); + setLoading(false); + } catch (error) { + const msg = + error?.response?.data?.message || + error?.message || + "Something went wrong"; + setError( msg ); + setLoading(false); } }; useEffect(() => { - fetchBuckets(); + const cacheBuckets = getCachedData("buckets"); + if (!cacheBuckets) { + fetchBuckets(); + } else { + setBuckets(cacheBuckets); + } }, []); - return { buckets, loading, error }; + return { buckets, loading, error, refetch: fetchBuckets }; }; -export const useContactProfile = (id) => -{ - const [ conatProfile, setContactProfile ] = useState( null ); - const [ loading, setLoading ] = useState( false ); - const [ Error, setError ] = useState( "" ); +export const useContactProfile = (id) => { + const [conatProfile, setContactProfile] = useState(null); + const [loading, setLoading] = useState(false); + const [Error, setError] = useState(""); - -const fetchContactProfile = async () => { + const fetchContactProfile = async () => { const cached = getCachedData("Contact Profile"); if (!cached || cached.contactId !== id) { @@ -88,7 +87,9 @@ const fetchContactProfile = async () => { cacheData("Contact Profile", { data: resp.data, contactId: id }); } catch (err) { const msg = - err?.response?.data?.message || err?.message || "Something went wrong"; + err?.response?.data?.message || + err?.message || + "Something went wrong"; setError(msg); } finally { setLoading(false); @@ -99,35 +100,33 @@ const fetchContactProfile = async () => { }; useEffect(() => { - if ( id ) - { + if (id) { fetchContactProfile(id); } }, [id]); return { conatProfile, loading, Error }; -} +}; +export const useContactNotes = (id, IsActive) => { + const [contactNotes, setContactNotes] = useState([]); + const [loading, setLoading] = useState(false); + const [Error, setError] = useState(""); -export const useContactNotes = (id,IsActive) => -{ - const [ contactNotes, setContactNotes ] = useState( [] ); - const [ loading, setLoading ] = useState( false ); - const [ Error, setError ] = useState( "" ); - - -const fetchContactNotes = async () => { + const fetchContactNotes = async () => { const cached = getCachedData("Contact Notes"); if (!cached || cached.contactId !== id) { setLoading(true); try { - const resp = await DirectoryRepository.GetNote(id,IsActive); + const resp = await DirectoryRepository.GetNote(id, IsActive); setContactNotes(resp.data); cacheData("Contact Notes", { data: resp.data, contactId: id }); } catch (err) { const msg = - err?.response?.data?.message || err?.message || "Something went wrong"; + err?.response?.data?.message || + err?.message || + "Something went wrong"; setError(msg); } finally { setLoading(false); @@ -138,46 +137,43 @@ const fetchContactNotes = async () => { }; useEffect(() => { - if ( id ) - { + if (id) { fetchContactNotes(id); } }, [id]); return { contactNotes, loading, Error }; -} +}; - -export const useOrganization = () => -{ - const [organizationList, setOrganizationList] = useState([]); +export const useOrganization = () => { + const [organizationList, setOrganizationList] = useState([]); const [loading, setLoading] = useState(false); - const [ error, setError ] = useState( "" ); - + const [error, setError] = useState(""); - const fetchOrg = async() => - { - const cacheOrg = getCachedData("organizations"); + const fetchOrg = async () => { + const cacheOrg = getCachedData("organizations"); if (cacheOrg?.length != 0) { - setLoading( true ); + setLoading(true); try { - const resp = await DirectoryRepository.GetOrganizations() - cacheData( "organizations", resp.data ); - setOrganizationList( resp.data ); - setLoading(false) + const resp = await DirectoryRepository.GetOrganizations(); + cacheData("organizations", resp.data); + setOrganizationList(resp.data); + setLoading(false); } catch (error) { const msg = - error?.response?.data?.message || error?.message || "Something went wrong"; + error?.response?.data?.message || + error?.message || + "Something went wrong"; setError(msg); - } + } } else { setOrganizationList(cacheOrg); } - } + }; useEffect(() => { fetchOrg(); }, []); return { organizationList, loading, error }; -} \ No newline at end of file +};