|
| 1 | +import ArrowButton from "@/components/ui/button/Arrow"; |
| 2 | +import { useTranslation } from "next-i18next"; |
| 3 | +import { useDispatch } from "@/hooks/useTypedDispatch"; |
| 4 | +import { setCurrentWallet } from "@/store/feature/user/wallets.slice"; |
| 5 | +import { toggleBodyScrolling } from "@/store/feature/ui.slice"; |
| 6 | +import { useSelector } from "@/hooks/useTypedSelector"; |
| 7 | +import { openVerificationModal } from "@/store/feature/kyc.slice"; |
| 8 | +import { Wallet } from "@/types/wallet"; |
| 9 | +import { ReactElement, useCallback, useMemo } from "react"; |
| 10 | + |
| 11 | +interface CashoutAddressProps { |
| 12 | + wallet: Wallet; |
| 13 | + setShowEditModal: (show: boolean) => void; |
| 14 | + disabled: boolean; |
| 15 | + setShowPayoutModal: (show: boolean) => void; |
| 16 | + testId?: string; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Cashout address component |
| 21 | + * |
| 22 | + * @returns {ReactElement} |
| 23 | + */ |
| 24 | +export default function CashoutAddress({ wallet, setShowEditModal, disabled, setShowPayoutModal, testId = "cashoutAddressId" }: CashoutAddressProps): ReactElement { |
| 25 | + const { t } = useTranslation(); |
| 26 | + const dispatch = useDispatch(); |
| 27 | + const user = useSelector((state) => state.user.data); |
| 28 | + const isKycVerified = user?.kycStatus === "VERIFIED"; |
| 29 | + const address = useMemo(() => (wallet.address ? wallet.address.match(/.{1,4}/g) : null), [wallet.address]); |
| 30 | + const cashable = useMemo(() => String(wallet.token).toUpperCase() !== "DAC", [wallet.token]); |
| 31 | + |
| 32 | + const triggerEditAddress = useCallback(() => { |
| 33 | + dispatch(setCurrentWallet(wallet)); |
| 34 | + setShowEditModal(true); |
| 35 | + dispatch(toggleBodyScrolling(true)); |
| 36 | + }, [dispatch, setShowEditModal, wallet]); |
| 37 | + |
| 38 | + const triggerCashout = useCallback(() => { |
| 39 | + setShowPayoutModal(true); |
| 40 | + dispatch(toggleBodyScrolling(true)); |
| 41 | + }, [setShowPayoutModal, dispatch]); |
| 42 | + |
| 43 | + const triggerKYCVerification = useCallback(() => { |
| 44 | + dispatch( |
| 45 | + openVerificationModal({ |
| 46 | + description: t("kyc.payout.reason"), |
| 47 | + completedActionText: t("kyc.payout.button.completed"), |
| 48 | + completedAction: () => { |
| 49 | + triggerCashout(); |
| 50 | + }, |
| 51 | + }) |
| 52 | + ); |
| 53 | + }, [dispatch, t, triggerCashout]); |
| 54 | + const cashout = () => { |
| 55 | + if (!isKycVerified) return triggerKYCVerification(); |
| 56 | + triggerCashout(); |
| 57 | + }; |
| 58 | + return ( |
| 59 | + <div className="px-7 pt-6 flex-1 pb-24 lg:pb-24" data-testid={testId}> |
| 60 | + {cashable ? ( |
| 61 | + <div className="text-sm text-gray-700"> |
| 62 | + {address ? ( |
| 63 | + <p className="leading-5 text-sm flex gap-x-2 gap-y-1 flex-wrap font-mono font-normal"> |
| 64 | + {address.map((part, index) => ( |
| 65 | + <span key={`address-${index}`} className="mr-2"> |
| 66 | + {part} |
| 67 | + </span> |
| 68 | + ))} |
| 69 | + </p> |
| 70 | + ) : ( |
| 71 | + <p>{wallet.description}</p> |
| 72 | + )} |
| 73 | + <div className="text-gray-700 text-sm mt-3"> |
| 74 | + <span className="cursor-pointer hover:underline" onClick={triggerEditAddress}> |
| 75 | + {address ? t("profile.wallets.address-change") : t("profile.wallets.address-set")} |
| 76 | + </span> |
| 77 | + </div> |
| 78 | + </div> |
| 79 | + ) : ( |
| 80 | + <div className="prose"> |
| 81 | + <p |
| 82 | + dangerouslySetInnerHTML={{ |
| 83 | + __html: t("profile.wallets.uncashable", { |
| 84 | + token: `${wallet.title}`, |
| 85 | + link: "https://discord.gg/5yDZvVnpQQ", |
| 86 | + }), |
| 87 | + }} |
| 88 | + /> |
| 89 | + </div> |
| 90 | + )} |
| 91 | + {cashable && ( |
| 92 | + <div className="right-2 absolute bottom-2 mt-5"> |
| 93 | + <ArrowButton disabled={!wallet.balance || !wallet.address || disabled} variant="outline-primary" minWidthClass="min-w-40" onClick={cashout}> |
| 94 | + {t("profile.wallets.cash-out")} |
| 95 | + </ArrowButton> |
| 96 | + </div> |
| 97 | + )} |
| 98 | + </div> |
| 99 | + ); |
| 100 | +} |
0 commit comments