From b21d8f7e860a6ce86cf858d6ea2eb5b91c7dab4f Mon Sep 17 00:00:00 2001 From: ALi Josheghani Date: Tue, 1 Oct 2024 00:11:20 +0330 Subject: [PATCH] (feat) chnage logic payment status checker --- src/pages/profile/index.tsx | 73 ++++++++++--------------------------- 1 file changed, 20 insertions(+), 53 deletions(-) diff --git a/src/pages/profile/index.tsx b/src/pages/profile/index.tsx index 5c64bd8..7527531 100644 --- a/src/pages/profile/index.tsx +++ b/src/pages/profile/index.tsx @@ -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(false); + const success = query.get("success"); + const [isPayment, setIsPayment] = useState(false); const [message, setMessage] = useState(""); - 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 && (