import { useEffect, useState } from "react";
import { ApiRequestStatus } from "../constants/api_request_states";

export const useGet = <T>(callback: () => Promise<T>) => {
    const [data, setData] = useState<T>({} as T);
    const [requestStatus, setRequestStatus] = useState(ApiRequestStatus.LOADING);

    useEffect(() => {
        const fetchData = async () => {
            try {
                const response = await callback();
                setData(response);
                setRequestStatus(ApiRequestStatus.SUCCESS);
            } catch (error) {
                console.error(error);
                setRequestStatus(ApiRequestStatus.ERROR);
            }
        };
        fetchData();
    }, []);

    return { data, requestStatus };
}