Newer
Older
taehui / taehui-fe / src / commentary / usePutCommentary.ts
@Taehui Taehui on 13 Mar 877 bytes v1.0.0
import { useMutation, useQueryClient } from "@tanstack/react-query";

import { isClientFault, wwwAXIOS } from "src/Www";
import { toast } from "react-toastify";
import { useTranslation } from "react-i18next";

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

  const { t } = useTranslation();

  return useMutation({
    mutationFn: async ({
      commentaryID,
      avatarCipher,
      text,
    }: {
      commentaryID: number;
      avatarCipher: string;
      text: string;
    }) => {
      await wwwAXIOS.put("/commentary", {
        commentaryID,
        avatarCipher,
        text,
      });
    },
    onSuccess: async () => {
      await queryClient.invalidateQueries({ queryKey: ["commentary"] });
    },
    onError: (e) => {
      if (isClientFault(e)) {
        toast.error(t("failedValidateCipher"));
      }
    },
  });
}