From 5a8c903136120b2d87710836270e921a2d580f6c Mon Sep 17 00:00:00 2001 From: siddharth Date: Tue, 14 May 2024 18:37:39 +0530 Subject: [PATCH] chore(voucher): updated confirm voucher modal --- .../confirm-modal/confirm-modal.module.css | 6 +- .../components/create/confirm-modal/index.tsx | 270 +++++++++--------- 2 files changed, 141 insertions(+), 135 deletions(-) diff --git a/apps/voucher/components/create/confirm-modal/confirm-modal.module.css b/apps/voucher/components/create/confirm-modal/confirm-modal.module.css index d5d2d7b2cc..94a7063607 100644 --- a/apps/voucher/components/create/confirm-modal/confirm-modal.module.css +++ b/apps/voucher/components/create/confirm-modal/confirm-modal.module.css @@ -1,9 +1,9 @@ -/* ConfirmModal.module.css */ .modal_container { display: flex; flex-direction: column; - gap: 0.7em; + gap: 1.3em; min-height: 10em; + justify-content: space-between; } .modalTitle { @@ -17,7 +17,7 @@ } .modalSubtitle { - font-size: 1rem; + font-size: 0.9rem; font-weight: bold; } diff --git a/apps/voucher/components/create/confirm-modal/index.tsx b/apps/voucher/components/create/confirm-modal/index.tsx index b75085c68b..efad31f956 100644 --- a/apps/voucher/components/create/confirm-modal/index.tsx +++ b/apps/voucher/components/create/confirm-modal/index.tsx @@ -26,6 +26,16 @@ type Props = { usdWallet: WalletDetails } +const calculateProfitAmount = ({ + amount, + commissionRatePercentage, +}: { + amount: number + commissionRatePercentage: number +}) => { + return (amount * commissionRatePercentage) / 100 +} + const ConfirmModal = ({ open, onClose, @@ -35,28 +45,40 @@ const ConfirmModal = ({ btcWallet, usdWallet, }: Props) => { + const router = useRouter() + const { update } = useSession() + const [selectedWalletId, setSelectedWalletId] = useState("") const [modalLoading, setModalLoading] = useState(false) const [modalError, setModalError] = useState(null) const [formIsValid, setFormIsValid] = useState(false) + + const [createWithdrawLink, { loading: withdrawLinkLoading }] = + useCreateWithdrawLinkMutation() + const { data: currencyConversion, refetch } = useCurrencyConversionEstimationQuery({ - variables: { - amount: Number(amount), - currency, - }, - context: { - endpoint: "GALOY", - }, + variables: { amount: Number(amount), currency }, + context: { endpoint: "GALOY" }, pollInterval: 60000, }) + const { data: currencyDataForOneUnit } = useCurrencyConversionEstimationQuery({ + variables: { amount: 1, currency }, + context: { endpoint: "GALOY" }, + }) + useEffect(() => { - refetch({ - amount: Number(amount), - currency, - }) + refetch({ amount: Number(amount), currency }) }, [amount, currency, refetch]) + const usdToCurrencyRate = Number( + currencyDataForOneUnit?.currencyConversionEstimation.usdCentAmount / 100, + ) + const profitAmount = calculateProfitAmount({ + amount: Number(amount), + commissionRatePercentage: Number(commissionPercentage), + }) + const amountInDollars = Number( (currencyConversion?.currencyConversionEstimation.usdCentAmount / 100).toFixed(2), ) @@ -66,11 +88,6 @@ const ConfirmModal = ({ commissionRatePercentage: Number(commissionPercentage), }) - const { update } = useSession() - const [createWithdrawLink, { loading: withdrawLinkLoading }] = - useCreateWithdrawLinkMutation() - const router = useRouter() - const handleSubmit = async ({ voucherAmountInCents, commissionPercentage, @@ -83,13 +100,7 @@ const ConfirmModal = ({ setModalLoading(true) try { const createWithdrawLinkResult = await createWithdrawLink({ - variables: { - input: { - voucherAmountInCents, - commissionPercentage, - walletId, - }, - }, + variables: { input: { voucherAmountInCents, commissionPercentage, walletId } }, }) update() @@ -112,123 +123,118 @@ const ConfirmModal = ({ const handleSelectChange = (e: React.ChangeEvent) => { const selected = e.target.value - const isValid = selected !== "default" setSelectedWalletId(selected === "btc" ? btcWallet.id : usdWallet.id) - setFormIsValid(isValid) + setFormIsValid(selected !== "default") } - if (modalLoading || withdrawLinkLoading) { - return ( - -
- -
-
- ) - } + const renderErrorModal = () => ( +
+

Error

+
+

{modalError}

+
+ +
+ ) - return ( - -
- {modalError ? ( - <> -

Error

-
-

{modalError}

-
- -
-
- - ) : ( - <> -

Confirm

-
-

Sales Amount

+ const renderConfirmationModal = () => ( + <> +
+
+

Price

+

+ {formatCurrency({ amount: Number(amount), currency })} +

+
+
+

Value

+

{Number(voucherAmountInDollars)} US Dollar

+
+
+
+
+

Commission

+

+ {Number(commissionPercentage)}% + {currency !== "USD" && + ` (${formatCurrency({ amount: profitAmount, currency })})`} +

+
+
+ {currency === "USD" ? ( + <> +

Profit

- {formatCurrency({ - amount: Number(amount), - currency, - })} + {formatCurrency({ amount: profitAmount, currency })} {currency}

-
-
-

Voucher Amount

+ + ) : ( + <> +

Rate

- {Number(voucherAmountInDollars)} US Dollar + 1 USD = {formatCurrency({ amount: usdToCurrencyRate, currency })}{" "} + {currency}

-
- -
-

Sales Commission

-

{Number(commissionPercentage)}%

-
- -
-

Paying Wallet

- -
- -
- - -
- + + )} +
+
+
+

Paying Wallet

+ +
+
+ + +
+ + ) + + return ( + +
+ {modalLoading || withdrawLinkLoading ? ( + + ) : modalError ? ( + renderErrorModal() + ) : ( + renderConfirmationModal() )}