created new hook for get a contacts

This commit is contained in:
Pramod Mahajan 2025-05-15 12:19:30 +05:30
parent 3670409977
commit 5415210d70

38
src/hooks/useDirectory.js Normal file
View File

@ -0,0 +1,38 @@
import {useState} from "react"
import {DirectoryRepository} from "../repositories/DirectoryRepository";
import {cacheData, getCachedData} from "../slices/apiDataManager";
export const useDirectory = () =>
{
const [ contacts, setContacts ] = useState( [] )
const [ loading, setLoading ] = useState( false )
const [ error, setError ] = useState();
const fetch = async() =>
{
const cache_contacts = getCachedData( "contacts" );
if ( !cache_contacts )
{
setLoading(true)
try
{
const response = await DirectoryRepository.GetContacts();
setContacts( response.data )
cacheData("contacts",response.data)
} catch ( error )
{
setError( error );
setLoading(false)
}
}
}
useState( () =>
{
fetch()
}, [] )
return {contacts,loading,error}
}