(feat) chnage logic payment status checker

This commit is contained in:
ALi Josheghani
2024-10-01 00:11:20 +03:30
parent b3bd39f6f0
commit b21d8f7e86

View File

@@ -2,81 +2,48 @@ import ProfileDetails from "./section/ProfileDetails";
import ProductConfigList from "./section/ProductConfigList"; import ProductConfigList from "./section/ProductConfigList";
import { useQueryParams } from "../../hooks/useQueryParams"; import { useQueryParams } from "../../hooks/useQueryParams";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import toast from "react-hot-toast"; import { useQuery } from "@tanstack/react-query";
import { useMutation, useQuery } from "@tanstack/react-query";
import useAxios from "../../hooks/useAxios"; import useAxios from "../../hooks/useAxios";
import { AxiosError, AxiosResponse } from "axios";
import ModalPayment from "./section/components/ModalPayment"; import ModalPayment from "./section/components/ModalPayment";
import { useNavigate } from "react-router-dom"; import toast from "react-hot-toast";
const Profile = () => { const Profile = () => {
const query = useQueryParams(); const query = useQueryParams();
const axios = useAxios(); const axios = useAxios();
const [isOpen, setIsOpen] = useState<boolean>(false); const [isOpen, setIsOpen] = useState<boolean>(false);
const success = query.get("success");
const [isPayment, setIsPayment] = useState<boolean>(false); const [isPayment, setIsPayment] = useState<boolean>(false);
const [message, setMessage] = useState<string>(""); 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) { useEffect(() => {
toast.promise(mutation.mutateAsync({ trackId }), {
loading: "درحال تایید درگاه پرداخت...", if (success) {
success: (res: AxiosResponse) => { if(success === "1"){
return `${res.data.alert}`; setIsOpen(true)
}, setIsPayment(true)
error: (err: AxiosError) => { setMessage("موفقیت آمیز")
let errorMessage = "یک خطای غیرمنتظره رخ داد"; }else if(success === "0"){
if ( setIsOpen(true)
err.response?.data && setIsPayment(false)
typeof err.response.data === "object" && setMessage("خطا")
"alert" in err.response.data }else{
) { toast.error("متاسفانه کد پرداخت به درستی دریافت نشد")
errorMessage = (err.response.data as { alert: string }).alert;
} }
return errorMessage;
},
});
} else { } else {
console.error("Track ID is missing or invalid."); console.error("Track ID is missing or invalid.");
} }
}, []); }, []);
const { data, isLoading, isFetching, error } = useQuery({ const { data, isLoading, isFetching, error } = useQuery({
queryKey: ["profile", mutation], queryKey: ["profile"],
queryFn: () => { queryFn: () => {
return axios.get("/profile"); return axios.get("/profile");
}, },
}); });
return ( return (
<> <>
{!mutation.isPending && ( {success && (
<ModalPayment <ModalPayment
isOpen={isOpen} isOpen={isOpen}
setIsOpen={setIsOpen} setIsOpen={setIsOpen}