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,35 +2,43 @@ 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"); setLoading(true);
if (!cache_contacts) { try {
setLoading(true); const response = await DirectoryRepository.GetContacts(activeParam);
try { setContacts(response.data);
const response = await DirectoryRepository.GetContacts(); cacheData("contacts", { data: response.data, isActive: activeParam });
setContacts(response.data); } catch (error) {
cacheData("contacts", response.data); setError(error);
setLoading(false); } finally {
} catch (error) { setLoading(false);
setError(error);
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([]);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);