Axios API Calls
Mock API calls
Axios API call using SWR
import useSWR, { mutate } from 'swr';
import { useMemo } from 'react';
// utils
import { fetcher } from 'utils/axios';
// types
import { Address } from 'types/e-commerce';
const endpoints = {
key: 'api/address',
list: '/list', // server URL
insert: '/new', // server URL
update: '/edit', // server URL
delete: '/delete' // server URL
};
export function useGetAddress() {
const { data, isLoading, error, isValidating } = useSWR(endpoints.key + endpoints.list, fetcher, {
revalidateIfStale: false,
revalidateOnFocus: false,
revalidateOnReconnect: false
});
const memoizedValue = useMemo(
() => ({
address: data?.address as Address[],
addressLoading: isLoading,
addressError: error,
addressValidating: isValidating,
addressEmpty: !isLoading && !data?.address?.length
}),
[data, error, isLoading, isValidating]
);
return memoizedValue;
}
...