38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import {Assignment} from "@/interfaces/results";
|
|
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);
|
|
const [isError, setIsError] = useState(false);
|
|
|
|
const getData = () => {
|
|
setIsLoading(true);
|
|
axios
|
|
.get<Assignment[]>(!corporate ? "/api/assignments" : `/api/assignments/corporate/${corporate}`)
|
|
.then(async (response) => {
|
|
if (assigner) {
|
|
setAssignments(response.data.filter((a) => a.assigner === assigner || (!a.teachers ? false : a.teachers.includes(assigner))));
|
|
return;
|
|
}
|
|
|
|
if (assignees) {
|
|
setAssignments(response.data.filter((a) => a.assignees.filter((x) => assignees.includes(x)).length > 0));
|
|
return;
|
|
}
|
|
|
|
setAssignments(response.data);
|
|
})
|
|
.finally(() => setIsLoading(false));
|
|
};
|
|
|
|
useEffect(getData, [assignees, assigner, corporate]);
|
|
|
|
return {assignments, isLoading, isError, reload: getData};
|
|
}
|