modified useBucket for refetch data

This commit is contained in:
Pramod Mahajan 2025-05-28 12:25:02 +05:30
parent c6e9d3fcbe
commit a0930197d9

View File

@ -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 };
}
};