Continued improving this
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
import {Assignment} from "@/interfaces/results";
|
||||
import axios from "axios";
|
||||
import Axios from "axios";
|
||||
import {setupCache} from "axios-cache-interceptor";
|
||||
import {useEffect, useState} from "react";
|
||||
|
||||
const instance = Axios.create();
|
||||
const axios = setupCache(instance);
|
||||
|
||||
export default function useAssignments({assigner, assignees, corporate}: {assigner?: string; assignees?: string; corporate?: string}) {
|
||||
const [assignments, setAssignments] = useState<Assignment[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
import {Exam} from "@/interfaces/exam";
|
||||
import {Permission, PermissionType} from "@/interfaces/permissions";
|
||||
import {ExamState} from "@/stores/examStore";
|
||||
import axios from "axios";
|
||||
import Axios from "axios";
|
||||
import {setupCache} from "axios-cache-interceptor";
|
||||
import {useEffect, useState} from "react";
|
||||
|
||||
const instance = Axios.create();
|
||||
const axios = setupCache(instance);
|
||||
|
||||
export default function usePermissions(user: string) {
|
||||
const [permissions, setPermissions] = useState<PermissionType[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
import {Exam} from "@/interfaces/exam";
|
||||
import {ExamState} from "@/stores/examStore";
|
||||
import axios from "axios";
|
||||
import Axios from "axios";
|
||||
import {setupCache} from "axios-cache-interceptor";
|
||||
import {useEffect, useState} from "react";
|
||||
|
||||
const instance = Axios.create();
|
||||
const axios = setupCache(instance);
|
||||
|
||||
export type Session = ExamState & {user: string; id: string; date: string};
|
||||
|
||||
export default function useSessions(user?: string) {
|
||||
|
||||
@@ -8,24 +8,48 @@ const axios = setupCache(instance);
|
||||
|
||||
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 (!!latestID) params.append("latestID", latestID);
|
||||
if (!!firstID) params.append("firstID", firstID);
|
||||
|
||||
setIsLoading(true);
|
||||
axios
|
||||
.get<User[]>(`/api/users/list?${params.toString()}`, {headers: {page: "register"}})
|
||||
.then((response) => setUsers(response.data))
|
||||
.get<{users: User[]; total: number}>(`/api/users/list?${params.toString()}`, {headers: {page: "register"}})
|
||||
.then((response) => {
|
||||
setUsers(response.data.users);
|
||||
setTotal(response.data.total);
|
||||
})
|
||||
.finally(() => setIsLoading(false));
|
||||
};
|
||||
|
||||
useEffect(getData, [props]);
|
||||
const next = () => {
|
||||
setLatestID(users[users.length - 1]?.id);
|
||||
setFirstID(undefined);
|
||||
setPage((prev) => prev + 1);
|
||||
};
|
||||
|
||||
return {users, isLoading, isError, reload: getData};
|
||||
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};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user