Spaces:
Build error
Build error
File size: 1,066 Bytes
5be784e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import { useState } from "react"
import axios from "@/utils/axios";
import { ApiRoute } from "@/utils/type";
export const useRequest = (endpoint: string, params: any) => {
const [loading, setLoading] = useState<boolean>(false)
const [data, setData] = useState<any>(null)
const submit = async () => {
setLoading(true);
const url = new URL(endpoint, process.env.NEXT_PUBLIC_APP_APIURL);
if (params) {
const parameters = Object.entries(params).filter(
([key, value]) =>
value !== "" &&
value !== null &&
value !== undefined &&
value !== false
);
parameters.forEach(([key, value]) => {
url.searchParams.append(key, value as string);
});
}
axios
.get(url.pathname, {
params: url.searchParams,
})
.then((res: any) => {
console.log("res ", res);
if (res.ok) {
setData(res.data);
}
})
.finally(() => setLoading(false));
};
return {
submit,
loading,
setLoading,
data
}
} |