import { useTaehuiStore } from "@/state/Stores";
import { wwwAPI } from "@/utilities/wwwAPI";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { getMillis } from "taehui-lib/date";

export default function usePostEssay() {
  const { totem } = useTaehuiStore();

  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: async ({
      forumID,
      title,
      text,
    }: {
      forumID: string;
      title: string;
      text: string;
    }) => {
      const { data } = await wwwAPI.post(
        `/essay/${forumID}`,
        {
          title,
          text,
        },
        {
          headers: {
            millis: getMillis(),
            totem,
          },
        },
      );
      return data;
    },
    onSuccess: async () => {
      await queryClient.invalidateQueries({ queryKey: ["essay"] });
    },
  });
}
