Newer
Older
taehui / taehui-fe / src / forum / useWipeComment.ts
@Taehui Taehui on 13 Mar 897 bytes v1.0.0
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { toast } from "react-toastify";
import { useTranslation } from "react-i18next";
import { getMillis } from "taehui-ts/date";

import { isClientFault, wwwAXIOS } from "src/Www";

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

  const { t } = useTranslation();

  return useMutation({
    mutationFn: async ({
      commentID,
      totem,
    }: {
      commentID: number;
      totem: string;
    }) => {
      await wwwAXIOS.delete(`/comment/${commentID}`, {
        headers: {
          millis: getMillis(),
          totem,
        },
      });
    },
    onSuccess: async () => {
      await queryClient.invalidateQueries({ queryKey: ["comment"] });
    },
    onError: (e) => {
      if (isClientFault(e)) {
        toast.error(t("failedValidateCipher"));
      }
    },
  });
}