(feat) chnage logic payment status checker
This commit is contained in:
@@ -2,81 +2,48 @@ import ProfileDetails from "./section/ProfileDetails";
|
||||
import ProductConfigList from "./section/ProductConfigList";
|
||||
import { useQueryParams } from "../../hooks/useQueryParams";
|
||||
import { useEffect, useState } from "react";
|
||||
import toast from "react-hot-toast";
|
||||
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import useAxios from "../../hooks/useAxios";
|
||||
import { AxiosError, AxiosResponse } from "axios";
|
||||
import ModalPayment from "./section/components/ModalPayment";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import toast from "react-hot-toast";
|
||||
|
||||
const Profile = () => {
|
||||
const query = useQueryParams();
|
||||
const axios = useAxios();
|
||||
const [isOpen, setIsOpen] = useState<boolean>(false);
|
||||
const success = query.get("success");
|
||||
|
||||
const [isPayment, setIsPayment] = useState<boolean>(false);
|
||||
const [message, setMessage] = useState<string>("");
|
||||
const navigate = useNavigate();
|
||||
const mutation = useMutation({
|
||||
mutationKey: ["statusPayment"],
|
||||
mutationFn: (values: { trackId: string }) => {
|
||||
return axios.post("/home/config/confirm", values);
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
setIsOpen(true);
|
||||
setIsPayment(true);
|
||||
setMessage(data?.data?.result?.alert);
|
||||
navigate("/profile", { replace: true });
|
||||
},
|
||||
onError: (err: AxiosError) => {
|
||||
let errorMessage = "یک خطای غیرمنتظره رخ داد";
|
||||
if (
|
||||
err.response?.data &&
|
||||
typeof err.response.data === "object" &&
|
||||
"alert" in err.response.data
|
||||
) {
|
||||
errorMessage = (err.response.data as { alert: string }).alert;
|
||||
}
|
||||
setIsOpen(true);
|
||||
setIsPayment(false);
|
||||
setMessage(errorMessage);
|
||||
navigate("/profile", { replace: true });
|
||||
},
|
||||
});
|
||||
useEffect(() => {
|
||||
const trackId = query.get("trackId");
|
||||
|
||||
if (trackId) {
|
||||
toast.promise(mutation.mutateAsync({ trackId }), {
|
||||
loading: "درحال تایید درگاه پرداخت...",
|
||||
success: (res: AxiosResponse) => {
|
||||
return `${res.data.alert}`;
|
||||
},
|
||||
error: (err: AxiosError) => {
|
||||
let errorMessage = "یک خطای غیرمنتظره رخ داد";
|
||||
if (
|
||||
err.response?.data &&
|
||||
typeof err.response.data === "object" &&
|
||||
"alert" in err.response.data
|
||||
) {
|
||||
errorMessage = (err.response.data as { alert: string }).alert;
|
||||
}
|
||||
return errorMessage;
|
||||
},
|
||||
});
|
||||
useEffect(() => {
|
||||
|
||||
if (success) {
|
||||
if(success === "1"){
|
||||
setIsOpen(true)
|
||||
setIsPayment(true)
|
||||
setMessage("موفقیت آمیز")
|
||||
}else if(success === "0"){
|
||||
setIsOpen(true)
|
||||
setIsPayment(false)
|
||||
setMessage("خطا")
|
||||
}else{
|
||||
toast.error("متاسفانه کد پرداخت به درستی دریافت نشد")
|
||||
}
|
||||
} else {
|
||||
console.error("Track ID is missing or invalid.");
|
||||
}
|
||||
}, []);
|
||||
|
||||
const { data, isLoading, isFetching, error } = useQuery({
|
||||
queryKey: ["profile", mutation],
|
||||
queryKey: ["profile"],
|
||||
queryFn: () => {
|
||||
return axios.get("/profile");
|
||||
},
|
||||
});
|
||||
return (
|
||||
<>
|
||||
{!mutation.isPending && (
|
||||
{success && (
|
||||
<ModalPayment
|
||||
isOpen={isOpen}
|
||||
setIsOpen={setIsOpen}
|
||||
|
||||
Reference in New Issue
Block a user