import {Exam} from "@/interfaces/exam";
import {ExamState} from "@/stores/examStore";
import axios from "axios";
import {useEffect, useState} from "react";

export type Session = ExamState & {user: string; id: string; date: string};

export default function useSessions(user?: string) {
	const [sessions, setSessions] = useState<Session[]>([]);
	const [isLoading, setIsLoading] = useState(false);
	const [isError, setIsError] = useState(false);

	const getData = () => {
		setIsLoading(true);
		axios
			.get<Session[]>(`/api/sessions${user ? `?user=${user}` : ""}`)
			.then((response) => setSessions(response.data))
			.finally(() => setIsLoading(false));
	};

	useEffect(getData, [user]);

	return {sessions, isLoading, isError, reload: getData};
}
