From 41d9db6fcc3133af27c098b2d6ee247033040e2d Mon Sep 17 00:00:00 2001 From: Pramod Mahajan Date: Tue, 27 May 2025 15:05:35 +0530 Subject: [PATCH] created new hook useOrganization --- src/hooks/useDirectory.js | 35 ++++++++++++++++++++++++ src/repositories/DirectoryRepository.jsx | 2 ++ 2 files changed, 37 insertions(+) diff --git a/src/hooks/useDirectory.js b/src/hooks/useDirectory.js index c21044fb..5d113183 100644 --- a/src/hooks/useDirectory.js +++ b/src/hooks/useDirectory.js @@ -146,3 +146,38 @@ const fetchContactNotes = async () => { return { contactNotes, loading, Error }; } + + +export const useOrganization = () => +{ + const [organizationList, setOrganizationList] = useState([]); + const [loading, setLoading] = useState(false); + const [ error, setError ] = useState( "" ); + + + const fetchOrg = async() => + { + const cacheOrg = getCachedData("organizations"); + if (cacheOrg?.length != 0) { + setLoading( true ); + try { + const resp = await DirectoryRepository.GetOrganizations() + cacheData( "organizations", resp.data ); + setOrganizationList( resp.data ); + setLoading(false) + } catch (error) { + const msg = + error?.response?.data?.message || error?.message || "Something went wrong"; + setError(msg); + } + } else { + setOrganizationList(cacheOrg); + } + } + + useEffect(() => { + fetchOrg(); + }, []); + + return { organizationList, loading, error }; +} \ No newline at end of file diff --git a/src/repositories/DirectoryRepository.jsx b/src/repositories/DirectoryRepository.jsx index 3761017a..5df3b1bf 100644 --- a/src/repositories/DirectoryRepository.jsx +++ b/src/repositories/DirectoryRepository.jsx @@ -1,6 +1,8 @@ import {api} from "../utils/axiosClient"; export const DirectoryRepository = { + GetOrganizations:()=>api.get('/api/directory/organization'), + GetContacts: (isActive) => api.get( `/api/directory?active=${isActive}` ), CreateContact: ( data ) => api.post( '/api/directory', data ), UpdateContact: ( id, data ) => api.put( `/api/directory/${ id }`, data ),