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

export default function usePostCommentary() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: async ({
      avatarName,
      avatarCipher,
      text,
    }: {
      avatarName: string;
      avatarCipher: string;
      text: string;
    }) => {
      await wwwAPI.post(
        "/commentary",
        {
          avatarName,
          avatarCipher,
          text,
        },
        {
          headers: {
            millis: getMillis(),
          },
        },
      );
    },
    onSuccess: async () => {
      await queryClient.invalidateQueries({ queryKey: ["commentary"] });
    },
  });
}
