141 lines
3.5 KiB
JavaScript
141 lines
3.5 KiB
JavaScript
import { useEffect, 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);
|
|
setLoading(false);
|
|
} catch (error) {
|
|
setError(error);
|
|
setLoading(false);
|
|
}
|
|
} else {
|
|
setContacts(cache_contacts);
|
|
}
|
|
};
|
|
|
|
useState(() => {
|
|
fetch();
|
|
}, []);
|
|
return { contacts, loading, error };
|
|
};
|
|
|
|
export const useBuckets = () => {
|
|
const [buckets, setBuckets] = useState([]);
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState("");
|
|
|
|
const fetchBuckets = async () => {
|
|
const cacheBuckets = getCachedData("buckets");
|
|
if (!cacheBuckets) {
|
|
setLoading( true );
|
|
try {
|
|
const resp = await DirectoryRepository.GetBucktes();
|
|
setBuckets(resp.data);
|
|
cacheData( "buckets", resp.data );
|
|
setLoading(false);
|
|
} catch (error) {
|
|
const msg =
|
|
error?.response?.data?.message || error?.message || "Something went wrong";
|
|
setError(msg);
|
|
}
|
|
} else {
|
|
setBuckets(cacheBuckets);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
fetchBuckets();
|
|
}, []);
|
|
|
|
return { buckets, loading, error };
|
|
};
|
|
|
|
export const useContactProfile = (id) =>
|
|
{
|
|
const [ conatProfile, setContactProfile ] = useState( null );
|
|
const [ loading, setLoading ] = useState( false );
|
|
const [ Error, setError ] = useState( "" );
|
|
|
|
|
|
const fetchContactProfile = async () => {
|
|
const cached = getCachedData("Contact Profile");
|
|
|
|
if (!cached || cached.contactId !== id) {
|
|
setLoading(true);
|
|
try {
|
|
const resp = await DirectoryRepository.GetContactProfile(id);
|
|
setContactProfile(resp.data);
|
|
cacheData("Contact Profile", { data: resp.data, contactId: id });
|
|
} catch (err) {
|
|
const msg =
|
|
err?.response?.data?.message || err?.message || "Something went wrong";
|
|
setError(msg);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
} else {
|
|
setContactProfile(cached.data);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
if ( id )
|
|
{
|
|
fetchContactProfile(id);
|
|
}
|
|
}, [id]);
|
|
|
|
return { conatProfile, loading, Error };
|
|
}
|
|
|
|
|
|
export const useContactNotes = (id) =>
|
|
{
|
|
const [ conatNotes, setContactNotes ] = useState( [] );
|
|
const [ loading, setLoading ] = useState( false );
|
|
const [ Error, setError ] = useState( "" );
|
|
|
|
|
|
const fetchContactNotes = async () => {
|
|
const cached = getCachedData("Contact Notes");
|
|
|
|
if (!cached || cached.contactId !== id) {
|
|
setLoading(true);
|
|
try {
|
|
const resp = await DirectoryRepository.GetNote(id);
|
|
setContactNotes(resp.data);
|
|
cacheData("Contact Notes", { data: resp.data, contactId: id });
|
|
} catch (err) {
|
|
const msg =
|
|
err?.response?.data?.message || err?.message || "Something went wrong";
|
|
setError(msg);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
} else {
|
|
setContactNotes(cached.data);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
if ( id )
|
|
{
|
|
fetchContactNotes(id);
|
|
}
|
|
}, [id]);
|
|
|
|
return { conatProfile, loading, Error };
|
|
}
|