modified hook, for taking active or incative params

This commit is contained in:
Pramod Mahajan 2025-05-23 16:55:42 +05:30
parent 0d249c7cdb
commit 6010e630e8

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([]);