Added useApi hook.

This commit is contained in:
Mike Cao 2022-02-22 23:52:31 -08:00
parent 7bd49e6caf
commit d19b6b5a82
22 changed files with 59 additions and 59 deletions

View file

@ -1,14 +1,13 @@
import { useState, useEffect } from 'react';
import { useRouter } from 'next/router';
import { get } from 'lib/web';
import { saveQuery } from 'store/queries';
import useApi from './useApi';
export default function useFetch(url, options = {}, update = []) {
const [response, setResponse] = useState();
const [error, setError] = useState();
const [loading, setLoadiing] = useState(false);
const [count, setCount] = useState(0);
const { basePath } = useRouter();
const { get } = useApi();
const { params = {}, headers = {}, disabled, delay = 0, interval, onDataLoad } = options;
async function loadData(params) {
@ -17,7 +16,7 @@ export default function useFetch(url, options = {}, update = []) {
setError(null);
const time = performance.now();
const { data, status, ok } = await get(`${basePath}${url}`, params, headers);
const { data, status, ok } = await get(url, params, headers);
await saveQuery(url, { time: performance.now() - time, completed: Date.now() });