import { useEffect, useState } from "react"; import { DirectoryRepository } from "../repositories/DirectoryRepository"; import { cacheData, getCachedData } from "../slices/apiDataManager"; export const useDirectory = (isActive) => { const [contacts, setContacts] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const fetch = async (activeParam = isActive) => { setLoading(true); try { const response = await DirectoryRepository.GetContacts(activeParam); setContacts(response.data); cacheData("contacts", { data: response.data, isActive: activeParam }); } catch (error) { setError(error); } finally { setLoading(false); } }; useEffect(() => { const cachedContacts = getCachedData("contacts"); if (!cachedContacts?.data || cachedContacts.isActive !== isActive) { fetch(isActive); } else { setContacts(cachedContacts.data); } }, [isActive]); return { contacts, loading, error, refetch: fetch, }; }; 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); } }; useEffect(() => { fetchBuckets(); }, []); 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 }; } export const useContactNotes = (id,IsActive) => { const [ contactNotes, 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,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"; setError(msg); } finally { setLoading(false); } } else { setContactNotes(cached.data); } }; useEffect(() => { if ( id ) { fetchContactNotes(id); } }, [id]); return { contactNotes, loading, Error }; }