pramod_Enhancement-#333 #136

Merged
vikas.nale merged 9 commits from pramod_Enhancement-#333 into Feature_Directory 2025-05-24 04:39:57 +00:00
Showing only changes of commit af996196a1 - Show all commits

View File

@ -2,34 +2,42 @@ import { useEffect, useState } from "react";
import { DirectoryRepository } from "../repositories/DirectoryRepository"; import { DirectoryRepository } from "../repositories/DirectoryRepository";
import { cacheData, getCachedData } from "../slices/apiDataManager"; import { cacheData, getCachedData } from "../slices/apiDataManager";
export const useDirectory = () => { export const useDirectory = (isActive) => {
const [contacts, setContacts] = useState([]); const [contacts, setContacts] = useState([]);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [error, setError] = useState(); const [error, setError] = useState(null);
const fetch = async () => { const fetch = async (activeParam = isActive) => {
const cache_contacts = getCachedData("contacts");
if (!cache_contacts) {
setLoading(true); setLoading(true);
try { try {
const response = await DirectoryRepository.GetContacts(); const response = await DirectoryRepository.GetContacts(activeParam);
setContacts(response.data); setContacts(response.data);
cacheData("contacts", response.data); cacheData("contacts", { data: response.data, isActive: activeParam });
setLoading(false);
} catch (error) { } catch (error) {
setError(error); setError(error);
} finally {
setLoading(false); setLoading(false);
} }
} else {
setContacts(cache_contacts);
}
}; };
useState(() => { useEffect(() => {
fetch(); const cachedContacts = getCachedData("contacts");
}, []); if (!cachedContacts?.data || cachedContacts.isActive !== isActive) {
return { contacts, loading, error }; fetch(isActive);
} else {
setContacts(cachedContacts.data);
}
}, [isActive]);
return {
contacts,
loading,
error,
refetch: fetch,
}; };
};
export const useBuckets = () => { export const useBuckets = () => {
const [buckets, setBuckets] = useState([]); const [buckets, setBuckets] = useState([]);