diff --git a/src/pages/testing/testing.tsx b/src/pages/testing/testing.tsx index f9b06cf..d3f2c35 100644 --- a/src/pages/testing/testing.tsx +++ b/src/pages/testing/testing.tsx @@ -4,7 +4,7 @@ import { BackButton } from "../../components/button"; import "./testing.css"; import { useNavigate } from "react-router-dom"; import { useLocation } from "react-router-dom"; -import { useEffect } from "react"; +import { useEffect, useState } from "react"; import { FindLatestTransactionArray, FindLatestTransactionNumber, FindNftIpfsAddressWithConractId, IsUserUpdatingIcon } from "../../NftSystem/updateUserNftStorage"; import { selectWalletNodeHost, useLedger } from "../../redux/useLedger"; import { useDispatch, useSelector } from "react-redux"; @@ -26,18 +26,19 @@ const Testing: React.FunctionComponent = (props) => { const [login, { isSuccess: isLoginSuccess, isLoading: isLoginLoading, data: loginData, error: loginError }] = useAccessMutation(); const [getCouponsByUser, { isSuccess: isGetCouponsByUser, error: getCouponError }] = useGetCouponsByUserMutation(); const [getFilterOption, { isSuccess: isGetFilterOptionSuccess, error: getFilterOptionError }] = useGetFilterOptionMutation(); - const [getCouponDetail, { isSuccess: isGetCouponCodeSuccess, error: getCouponCodeError }] = useGetCouponDetailMutation(); + const [getCouponDetail, { isSuccess: isGetCouponCodeSuccess, error: getCouponCodeError, status: couponDetailStatus }] = useGetCouponDetailMutation(); const loginedEmail = useSelector(selectCurrentEmail); const couponList = useSelector(selectCurrentCouponList); const filterOption: FilterOption = useSelector(selectCurrentFilterOption); const selectedCoupon = useSelector(selectCurrentSelectedCoupon); + const [isTriggerGetCouponCode, setIsTriggerGetCouponCode] = useState(false); const [logout, { isSuccess: isLogoutSuccess, error: logoutError }] = useLogoutMutation(); const location = useLocation(); - const [email, setEmail] = React.useState(""); - const [userEnterCouponId, setUserEnterCouponId] = React.useState(0); + const [email, setEmail] = useState(""); + const [userEnterCouponId, setUserEnterCouponId] = useState(0); useEffect(() => { const searchParams = new URLSearchParams(location.search); @@ -59,6 +60,27 @@ const Testing: React.FunctionComponent = (props) => { } }, [location.search]); + // build a function to get coupon code for every 30 seconds + // if isTriggerGetCouponCode is false, stop the interval + useEffect(() => { + if (isTriggerGetCouponCode && couponDetailStatus !== "rejected") { + const interval = setInterval(() => { + getCouponDetail(userEnterCouponId) + .then((res) => { + console.log("res", res); + if ("data" in res) { + dispatch(couponSlice.actions.setSelectedCoupon(res.data)); + } + }) + .catch((err) => { + console.log(err); + }); + }, 3000); + + return () => clearInterval(interval); + } + }, [isTriggerGetCouponCode]); + const emailLogin = async () => { const sendEmail: SendEmailLinkContent = { @@ -110,20 +132,22 @@ const Testing: React.FunctionComponent = (props) => { } const getCoupon = async () => { - getCouponDetail(userEnterCouponId) - .then((res) => { - console.log("res", res); - if ("data" in res) { - dispatch(couponSlice.actions.setSelectedCoupon(res.data)); - } - }) - .catch((err) => { - console.log(err); - }); + setIsTriggerGetCouponCode(true); + // getCouponDetail(userEnterCouponId) + // .then((res) => { + // console.log("res", res); + // if ("data" in res) { + // dispatch(couponSlice.actions.setSelectedCoupon(res.data)); + // } + // }) + // .catch((err) => { + // console.log(err); + // }); } const content: JSX.Element = ( <> + setEmail(e.target.value)} /> @@ -131,6 +155,7 @@ const Testing: React.FunctionComponent = (props) => { setUserEnterCouponId(parseInt(e.target.value))} /> + @@ -162,11 +187,12 @@ const Testing: React.FunctionComponent = (props) => { ); })} - {isGetCouponCodeSuccess &&

get coupon code

} - {isGetCouponCodeSuccess &&

{selectedCoupon.c_name}

} - {isGetCouponCodeSuccess &&

{selectedCoupon.c_description}

} - {isGetCouponCodeSuccess &&

{selectedCoupon.coupon_code}

} - + {couponDetailStatus === "fulfilled" &&

get coupon detail

} + {couponDetailStatus === "fulfilled" &&

{selectedCoupon.c_name}

} + {couponDetailStatus === "fulfilled" &&

{selectedCoupon.c_description}

} + {couponDetailStatus === "fulfilled" &&

{selectedCoupon.coupon_code}

} +

{couponDetailStatus}

+

{isTriggerGetCouponCode ? "triggered get coupon code" : "stop get coupon code"}

); @@ -186,11 +212,11 @@ export default Testing; // const nftDistributor = process.env.REACT_APP_NFT_DISTRIBUTOR!; // const NftContractStorage = useSelector(getNftContractStorage); -// const [nftId, setNftId] = React.useState(""); -// const [isLoading, setIsLoading] = React.useState(true); -// const [nftIpfsAddress, setNftIpfsAddress] = React.useState(""); -// const [nftNumber, setNftNumber] = React.useState(""); -// const [imgAddress, setImgAddress] = React.useState(""); +// const [nftId, setNftId] = useState(""); +// const [isLoading, setIsLoading] = useState(true); +// const [nftIpfsAddress, setNftIpfsAddress] = useState(""); +// const [nftNumber, setNftNumber] = useState(""); +// const [imgAddress, setImgAddress] = useState(""); // const fetchUserIcon = async () => { // const isUserSettingUpdating = await IsUserUpdatingIcon(ledger2, userAccountId); // if (isUserSettingUpdating === true) {