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,16 +37,12 @@ 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();
@ -55,28 +51,31 @@ export const useBuckets = () => {
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 {
setBuckets(cacheBuckets);
setLoading(false);
}
};
useEffect(() => {
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 [loading, setLoading] = useState(false);
const [Error, setError] = useState("");
const fetchContactProfile = async () => {
const cached = getCachedData("Contact Profile");
@ -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,23 +100,19 @@ const fetchContactProfile = async () => {
};
useEffect(() => {
if ( id )
{
if (id) {
fetchContactProfile(id);
}
}, [id]);
return { conatProfile, loading, Error };
}
};
export const useContactNotes = (id,IsActive) =>
{
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");
@ -127,7 +124,9 @@ const fetchContactNotes = async () => {
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 = () =>
{
export const useOrganization = () => {
const [organizationList, setOrganizationList] = useState([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState("");
const fetchOrg = async() =>
{
const fetchOrg = async () => {
const cacheOrg = getCachedData("organizations");
if (cacheOrg?.length != 0) {
setLoading(true);
try {
const resp = await DirectoryRepository.GetOrganizations()
const resp = await DirectoryRepository.GetOrganizations();
cacheData("organizations", resp.data);
setOrganizationList(resp.data);
setLoading(false)
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 };
}
};