import {Code, Group, User} from "@/interfaces/user";
import axios from "axios";
import {useEffect, useState} from "react";

export default function useCodes(creator?: string) {
	const [codes, setCodes] = useState<Code[]>([]);
	const [isLoading, setIsLoading] = useState(false);
	const [isError, setIsError] = useState(false);

	const getData = () => {
		setIsLoading(true);
		axios
			.get<Code[]>(`/api/code${creator ? `?creator=${creator}` : ""}`)
			.then((response) => setCodes(response.data))
			.finally(() => setIsLoading(false));
	};

	useEffect(getData, [creator]);

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