Advertisement
ebleach7

Untitled

Apr 18th, 2024
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { queryClient } from "@/app/providers";
  2. import { ICreateFaqItemBody, faqServie } from "@/shared/api";
  3. import { removeToken } from "@/shared/lib";
  4. import { useMutation } from "@tanstack/react-query";
  5. import { App } from "antd";
  6. import axios from "axios";
  7. import { useNavigate } from "react-router-dom";
  8.  
  9. export const useCreateFaqItem = () => {
  10.   const { notification } = App.useApp();
  11.   const navigate = useNavigate();
  12.  
  13.   const createFaqItem = useMutation({
  14.     mutationFn: (body: ICreateFaqItemBody) => faqServie.createFaqItem(body),
  15.     onSuccess: (data) => {
  16.       if (data.data.code === "SUCCESS") {
  17.         notification.success({
  18.           message: data.data.text,
  19.         });
  20.  
  21.         queryClient.invalidateQueries({
  22.           queryKey: ["faq"],
  23.         });
  24.       }
  25.  
  26.       if (data.data.code === "ERROR") {
  27.         notification.error({
  28.           message: data.data.text,
  29.         });
  30.       }
  31.     },
  32.     onError: (error) => {
  33.       if (axios.isAxiosError(error)) {
  34.         notification.error({
  35.           message: error.message,
  36.         });
  37.  
  38.         if (error.response && error.response.status === 401) {
  39.           navigate("/sign-in");
  40.         }
  41.       } else {
  42.         notification.error({
  43.           message: error.message ? error.message : "Что то пошло не так!",
  44.         });
  45.  
  46.         removeToken();
  47.         navigate("/sign-in");
  48.  
  49.         console.log("Create Faq Item Error", error);
  50.       }
  51.     },
  52.   });
  53.  
  54.   return { createFaqItem };
  55. };
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement