import { useMutation, useQueryClient } from "@tanstack/react-query";
import { getMillis } from "taehui-ts/date";
import { useTo } from "taehui-ts/fe-utility";

import { wwwAXIOS } from "src/Www";
import { useAvatarStore } from "src/Stores";

export default function useWipeEssay(forumID: string) {
  const { totem } = useAvatarStore();

  const queryClient = useQueryClient();

  const to = useTo();

  return useMutation({
    mutationFn: async (essayID: string) => {
      await wwwAXIOS.delete(`/essay/${essayID}`, {
        headers: {
          millis: getMillis(),
          totem,
        },
      });
    },
    onSuccess: async () => {
      await queryClient.invalidateQueries({ queryKey: ["forum"] });
      to(`/forum/${forumID}`);
    },
  });
}
