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 usePostComment() {
  const { totem } = useTaehuiStore();

  const queryClient = useQueryClient();

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