modified useBucket for refetch data

This commit is contained in:
Pramod Mahajan 2025-05-28 12:25:02 +05:30
parent a87d2c9143
commit 16389bf102

View File

@ -37,47 +37,46 @@ export const useDirectory = (isActive) => {
}; };
}; };
export const useBuckets = () => { export const useBuckets = () => {
const [buckets, setBuckets] = useState([]); const [buckets, setBuckets] = useState([]);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [error, setError] = useState(""); const [error, setError] = useState("");
const fetchBuckets = async () => { const fetchBuckets = async () => {
const cacheBuckets = getCachedData("buckets"); setLoading(true);
if (!cacheBuckets) { try {
setLoading( true ); const resp = await DirectoryRepository.GetBucktes();
try { setBuckets(resp.data);
const resp = await DirectoryRepository.GetBucktes(); cacheData("buckets", resp.data);
setBuckets(resp.data); setLoading(false);
cacheData( "buckets", resp.data ); } catch (error) {
setLoading(false); const msg =
} catch (error) { error?.response?.data?.message ||
const msg = error?.message ||
error?.response?.data?.message || error?.message || "Something went wrong"; "Something went wrong";
setError(msg); setError( msg );
} setLoading(false);
} else {
setBuckets(cacheBuckets);
} }
}; };
useEffect(() => { 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) => export const useContactProfile = (id) => {
{ const [conatProfile, setContactProfile] = useState(null);
const [ conatProfile, setContactProfile ] = useState( null ); const [loading, setLoading] = useState(false);
const [ loading, setLoading ] = useState( false ); const [Error, setError] = useState("");
const [ Error, setError ] = useState( "" );
const fetchContactProfile = async () => {
const fetchContactProfile = async () => {
const cached = getCachedData("Contact Profile"); const cached = getCachedData("Contact Profile");
if (!cached || cached.contactId !== id) { if (!cached || cached.contactId !== id) {
@ -88,7 +87,9 @@ const fetchContactProfile = async () => {
cacheData("Contact Profile", { data: resp.data, contactId: id }); cacheData("Contact Profile", { data: resp.data, contactId: id });
} catch (err) { } catch (err) {
const msg = const msg =
err?.response?.data?.message || err?.message || "Something went wrong"; err?.response?.data?.message ||
err?.message ||
"Something went wrong";
setError(msg); setError(msg);
} finally { } finally {
setLoading(false); setLoading(false);
@ -99,35 +100,33 @@ const fetchContactProfile = async () => {
}; };
useEffect(() => { useEffect(() => {
if ( id ) if (id) {
{
fetchContactProfile(id); fetchContactProfile(id);
} }
}, [id]); }, [id]);
return { conatProfile, loading, Error }; 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 fetchContactNotes = async () => {
{
const [ contactNotes, setContactNotes ] = useState( [] );
const [ loading, setLoading ] = useState( false );
const [ Error, setError ] = useState( "" );
const fetchContactNotes = async () => {
const cached = getCachedData("Contact Notes"); const cached = getCachedData("Contact Notes");
if (!cached || cached.contactId !== id) { if (!cached || cached.contactId !== id) {
setLoading(true); setLoading(true);
try { try {
const resp = await DirectoryRepository.GetNote(id,IsActive); const resp = await DirectoryRepository.GetNote(id, IsActive);
setContactNotes(resp.data); setContactNotes(resp.data);
cacheData("Contact Notes", { data: resp.data, contactId: id }); cacheData("Contact Notes", { data: resp.data, contactId: id });
} catch (err) { } catch (err) {
const msg = const msg =
err?.response?.data?.message || err?.message || "Something went wrong"; err?.response?.data?.message ||
err?.message ||
"Something went wrong";
setError(msg); setError(msg);
} finally { } finally {
setLoading(false); setLoading(false);
@ -138,46 +137,43 @@ const fetchContactNotes = async () => {
}; };
useEffect(() => { useEffect(() => {
if ( id ) if (id) {
{
fetchContactNotes(id); fetchContactNotes(id);
} }
}, [id]); }, [id]);
return { contactNotes, loading, Error }; return { contactNotes, loading, Error };
} };
export const useOrganization = () => {
export const useOrganization = () => const [organizationList, setOrganizationList] = useState([]);
{
const [organizationList, setOrganizationList] = useState([]);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [ error, setError ] = useState( "" ); const [error, setError] = useState("");
const fetchOrg = async () => {
const fetchOrg = async() => const cacheOrg = getCachedData("organizations");
{
const cacheOrg = getCachedData("organizations");
if (cacheOrg?.length != 0) { if (cacheOrg?.length != 0) {
setLoading( true ); setLoading(true);
try { try {
const resp = await DirectoryRepository.GetOrganizations() const resp = await DirectoryRepository.GetOrganizations();
cacheData( "organizations", resp.data ); cacheData("organizations", resp.data);
setOrganizationList( resp.data ); setOrganizationList(resp.data);
setLoading(false) setLoading(false);
} catch (error) { } catch (error) {
const msg = const msg =
error?.response?.data?.message || error?.message || "Something went wrong"; error?.response?.data?.message ||
error?.message ||
"Something went wrong";
setError(msg); setError(msg);
} }
} else { } else {
setOrganizationList(cacheOrg); setOrganizationList(cacheOrg);
} }
} };
useEffect(() => { useEffect(() => {
fetchOrg(); fetchOrg();
}, []); }, []);
return { organizationList, loading, error }; return { organizationList, loading, error };
} };