import { Form } from "@/payload-types";
import { useState } from "react";
import { fetchFormREST } from "../rest/form";

export function useFormQuery() {
  const [data, setData] = useState<Form | null>(null);
  const [isFetching, setFetching] = useState(false);

  async function _fetch(id: number) {
    setFetching(true);
    const res = await fetchFormREST(id);
    setFetching(false);

    if (!!res) {
      setData(res);
    } else {
      setData(null);
    }
  }

  return {
    _fetch,
    data,
    isFetching,
  };
}
