created new hook for get a contacts
This commit is contained in:
parent
3670409977
commit
5415210d70
38
src/hooks/useDirectory.js
Normal file
38
src/hooks/useDirectory.js
Normal 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}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user