From 5415210d70548e9aaac4706eef8cebdb2675e663 Mon Sep 17 00:00:00 2001 From: Pramod Mahajan Date: Thu, 15 May 2025 12:19:30 +0530 Subject: [PATCH] created new hook for get a contacts --- src/hooks/useDirectory.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/hooks/useDirectory.js diff --git a/src/hooks/useDirectory.js b/src/hooks/useDirectory.js new file mode 100644 index 00000000..f1e31c14 --- /dev/null +++ b/src/hooks/useDirectory.js @@ -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} +} \ No newline at end of file