Updated the pagination on the useUsers and migrading the grading

This commit is contained in:
Tiago Ribeiro
2024-09-07 16:53:58 +01:00
parent 417c9176fe
commit 56f374bbfe
5 changed files with 45 additions and 41 deletions

View File

@@ -5,29 +5,25 @@ import {setupCache} from "axios-cache-interceptor";
const instance = Axios.create();
const axios = setupCache(instance);
export const userHashStudent = { type: "student" } as { type: Type };
export const userHashTeacher = { type: "teacher" } as { type: Type };
export const userHashCorporate = { type: "corporate" } as { type: Type };
export const userHashStudent = {type: "student"} as {type: Type};
export const userHashTeacher = {type: "teacher"} as {type: Type};
export const userHashCorporate = {type: "corporate"} as {type: Type};
export default function useUsers(props?: {type?: Type; page?: number; size?: number}) {
const [users, setUsers] = useState<User[]>([]);
const [total, setTotal] = useState(0);
const [isLoading, setIsLoading] = useState(false);
const [isError, setIsError] = useState(false);
const [latestID, setLatestID] = useState<string>();
const [firstID, setFirstID] = useState<string>();
const [page, setPage] = useState(0);
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());
if (props[key as keyof typeof props] !== undefined) params.append(key, props[key as keyof typeof props]!.toString());
});
if (!!latestID) params.append("latestID", latestID);
if (!!firstID) params.append("firstID", firstID);
console.log(params.toString());
setIsLoading(true);
axios
@@ -39,20 +35,7 @@ export default function useUsers(props?: {type?: Type; page?: number; size?: num
.finally(() => setIsLoading(false));
};
const next = () => {
setLatestID(users[users.length - 1]?.id);
setFirstID(undefined);
setPage((prev) => prev + 1);
};
useEffect(getData, [props?.page, props?.size, props?.type]);
const previous = () => {
setLatestID(undefined);
setFirstID(page > 1 ? users[0]?.id : undefined);
setPage((prev) => prev - 1);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(getData, [props, latestID, firstID]);
return {users, total, page, isLoading, isError, reload: getData, next, previous};
return {users, total, isLoading, isError, reload: getData};
}