import { useMutation } from "@tanstack/react-query";
import { useRouter } from "next/navigation";
import CustomFetch from "../APIRequest/AxiosInstance";
import { CustomToast } from "../CustomToast";

const usePost = (url: any, path: any, extraFunction: any, method: any, errorMessage: any = false) => {
    const router = useRouter();
    return useMutation({
        mutationFn: (data) => CustomFetch({ url, data, method: method ? method : "post" }, null, method === "POST" ? true : false, errorMessage),
        onSuccess: (response) => {
            if (response?.status === 200 || response?.status === 201 || response?.status === 204 || response?.status === 205 || response?.status === 206) {
                extraFunction && extraFunction(response)
                path && router.push(path)
            } else {
                CustomToast("error", response?.response?.data?.message)
            }
        },
    })
};

export default usePost;