import { Plan } from "../types";
import { useEffect, useState } from "react";

export function useGetPlans() {
  const [isLoading, setLoading] = useState(false);
  const [data, setData] = useState<null | Plan[]>(null);

  useEffect(() => {
    const BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL;
    const getPlans = async () => {
      setLoading(false);
      try {
        const url = new URL(BASE_URL + `planes`);
        const response = await fetch(url);
        const data = await response.json();
        setData(data);
      } catch (error) {
        console.warn(error);
      }
    };
    getPlans();
  }, []);

  return { data, isLoading };
}
