From 75186b0fd5bdd62f4fc81c3b6da75922fa894466 Mon Sep 17 00:00:00 2001 From: Heye Date: Fri, 3 Jan 2025 05:40:23 +0100 Subject: [PATCH] feat: use DualCurrency input field (#35) (#67) * feat: use DualCurrency input field (#35) * chore: minor ui improvements --------- Co-authored-by: Roland Bewick --- .../components/CreateSubscriptionForm.tsx | 50 +++++++++++++++---- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/app/create/components/CreateSubscriptionForm.tsx b/app/create/components/CreateSubscriptionForm.tsx index 3285951..6c8a42c 100644 --- a/app/create/components/CreateSubscriptionForm.tsx +++ b/app/create/components/CreateSubscriptionForm.tsx @@ -2,7 +2,7 @@ import { useForm } from "react-hook-form"; import { useRouter } from "next/navigation"; -import React from "react"; +import React, { useState, useEffect } from "react"; import { Timeframe, timeframes } from "types/Timeframe"; import { Loading } from "app/components/Loading"; import { CreateSubscriptionRequest } from "types/CreateSubscriptionRequest"; @@ -13,6 +13,7 @@ import { LUD18PayerData, LightningAddress, RequestInvoiceArgs, + fiat, } from "@getalby/lightning-tools"; type CreateSubscriptionFormData = Omit< @@ -71,6 +72,26 @@ export function CreateSubscriptionForm() { push(`/confirm?${searchParams.toString()}`); }); const watchedAmount = watch("amount"); + + const [convertedAmount, setConvertedAmount] = useState(""); + + useEffect(() => { + const updateConversion = async () => { + if (watchedAmount === undefined) { + return; + } + + const value = await fiat.getFormattedFiatValue({ + satoshi: watchedAmount, + currency: "usd", + locale: "en-US", + }); + setConvertedAmount(`~${value}`); + }; + + updateConversion(); + }, [watchedAmount]); + const watchedTimeframe = watch("timeframe"); const setSelectedTimeframe = React.useCallback( (timeframe: Timeframe) => setValue("timeframe", timeframe), @@ -136,15 +157,24 @@ export function CreateSubscriptionForm() { - - !isValidPositiveValue(parseInt(value)) - ? "Please enter a positive value" - : undefined, - })} - className="zp-input" - /> +
+ {convertedAmount && ( +

+ {convertedAmount} +

+ )} + { + return !isValidPositiveValue(parseInt(value)) + ? "Please enter a positive value" + : undefined; + }, + })} + className="zp-input flex-1" + placeholder={`Enter amount in sats`} + /> +
{errors.amount && (

{errors.amount.message}

)}