diff --git a/app/confirm/components/ConfirmSubscriptionForm.tsx b/app/confirm/components/ConfirmSubscriptionForm.tsx index 2de9e5c..50b8c1c 100644 --- a/app/confirm/components/ConfirmSubscriptionForm.tsx +++ b/app/confirm/components/ConfirmSubscriptionForm.tsx @@ -1,10 +1,8 @@ -"use client"; - import { useForm } from "react-hook-form"; import { CreateSubscriptionRequest } from "types/CreateSubscriptionRequest"; import { useRouter } from "next/navigation"; import { CreateSubscriptionResponse } from "types/CreateSubscriptionResponse"; -import React from "react"; +import React, { useEffect, useState } from "react"; import { webln } from "@getalby/sdk"; import { UnconfirmedSubscription } from "types/UnconfirmedSubscription"; import { isValidNostrConnectUrl } from "lib/validation"; @@ -42,7 +40,7 @@ export function ConfirmSubscriptionForm({ }, }); - const [isNavigating, setNavigating] = React.useState(false); + const [isNavigating, setNavigating] = useState(false); const { push } = useRouter(); const hasLinkedWallet = !!watch("nostrWalletConnectUrl"); @@ -64,7 +62,7 @@ export function ConfirmSubscriptionForm({ if (isValidNostrConnectUrl(url)) { setValue("nostrWalletConnectUrl", url); } else { - throw new Error("Received invalid NWC URL"); + throw an Error("Received invalid NWC URL"); } } catch (error) { if (error) { @@ -73,11 +71,39 @@ export function ConfirmSubscriptionForm({ } }; + const getMillisecondsForInterval = (interval: string) => { + switch (interval) { + case "daily": + return 24 * 60 * 60 * 1000; // 1 day in milliseconds + case "weekly": + return 7 * 24 * 60 * 60 * 1000; // 1 week in milliseconds + case "monthly": + // Note: This is a simplified calculation; adjust as needed + return 30 * 24 * 60 * 60 * 1000; // 30 days in milliseconds + default: + return 24 * 60 * 60 * 1000; // Default to daily (1 day in milliseconds) + } + }; + const onSubmit = handleSubmit(async (data) => { if (!data.nostrWalletConnectUrl) { toast.error("Please link your wallet"); return; } + + const selectedInterval = data.sleepDuration; + const intervalMilliseconds = getMillisecondsForInterval(selectedInterval); + + const oneYearInMilliseconds = 31536000000; // One year in milliseconds + if (selectedInterval === "yearly" && intervalMilliseconds > oneYearInMilliseconds) { + toast.error("Interval cannot be more than 1 year"); + return; + } + + const satoshis = getSatoshisForInterval(intervalMilliseconds); + + data.max_amount = satoshis; + const subscriptionId = await createSubscription(data); if (subscriptionId) { toast.success("Recurring payment created"); @@ -85,7 +111,7 @@ export function ConfirmSubscriptionForm({ push( `/subscriptions/${subscriptionId}${ returnUrl ? `?returnUrl=${returnUrl}` : "" - }`, + }` ); } }); @@ -141,7 +167,7 @@ export function ConfirmSubscriptionForm({
- Nostr Wallet Connect allows you to securely authorise + Nostr Wallet Connect allows you to securely authorize ZapPlanner to perform transactions from your lightning wallet on your behalf.
@@ -255,7 +281,7 @@ export function ConfirmSubscriptionForm({ } async function createSubscription( - createSubscriptionRequest: CreateSubscriptionRequest, + createSubscriptionRequest: CreateSubscriptionRequest ): Promise