Updated the MasterCorporate with the improved queries

This commit is contained in:
Tiago Ribeiro
2024-09-06 14:57:23 +01:00
parent a6bf53e84c
commit de35e1a8b7
8 changed files with 153 additions and 146 deletions

View File

@@ -1,21 +1,31 @@
import {User} from "@/interfaces/user";
import axios from "axios";
import {Type, User} from "@/interfaces/user";
import Axios from "axios";
import {useEffect, useState} from "react";
import {setupCache} from "axios-cache-interceptor";
export default function useUsers() {
const instance = Axios.create();
const axios = setupCache(instance);
export default function useUsers(props?: {type?: Type; page?: number; size?: number}) {
const [users, setUsers] = useState<User[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [isError, setIsError] = useState(false);
const getData = () => {
const params = new URLSearchParams();
if (!!props)
Object.keys(props).forEach((key) => {
if (!!props[key as keyof typeof props]) params.append(key, props[key as keyof typeof props]!.toString());
});
setIsLoading(true);
axios
.get<User[]>("/api/users/list", {headers: {page: "register"}})
.get<User[]>(`/api/users/list?${params.toString()}`, {headers: {page: "register"}})
.then((response) => setUsers(response.data))
.finally(() => setIsLoading(false));
};
useEffect(getData, []);
useEffect(getData, [props]);
return {users, isLoading, isError, reload: getData};
}