From e1fb33b83d3dcb592d582db9351bf40f480296ff Mon Sep 17 00:00:00 2001 From: Adarsh Jha <132337675+adarsh-jha-dev@users.noreply.github.com> Date: Sun, 15 Oct 2023 22:52:34 +0530 Subject: [PATCH 1/3] Update ConfirmSubscriptionForm.tsx --- .../components/ConfirmSubscriptionForm.tsx | 41 +++++++++++++++---- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/app/confirm/components/ConfirmSubscriptionForm.tsx b/app/confirm/components/ConfirmSubscriptionForm.tsx index 2de9e5c..bea2148 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"); @@ -73,11 +71,39 @@ export function ConfirmSubscriptionForm({ } }; + const getSatoshisForInterval = (interval: string) => { + // Calculate the number of satoshis based on the desired interval + switch (interval) { + case "daily": + return 1; // Adjust this as needed + case "weekly": + return 7; // Adjust this as needed + case "monthly": + return 30; // Adjust this as needed + default: + return 1; // Default to daily + } + }; + const onSubmit = handleSubmit(async (data) => { if (!data.nostrWalletConnectUrl) { toast.error("Please link your wallet"); return; } + + const selectedInterval = data.sleepDuration; + + // Enforce that the interval should not be more than 1 year (365 days) + if (selectedInterval === "yearly") { + toast.error("Interval cannot be more than 1 year"); + return; + } + + // Calculate the number of satoshis based on the desired interval + const satoshis = getSatoshisForInterval(selectedInterval); + + data.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}` : "" - }`, + }` ); } }); @@ -255,7 +281,7 @@ export function ConfirmSubscriptionForm({ } async function createSubscription( - createSubscriptionRequest: CreateSubscriptionRequest, + createSubscriptionRequest: CreateSubscriptionRequest ): Promise { const res = await fetch("/api/subscriptions", { method: "POST", @@ -267,7 +293,6 @@ async function createSubscription( toast.error(res.status + " " + res.statusText); return undefined; } - const createSubscriptionResponse = - (await res.json()) as CreateSubscriptionResponse; + const createSubscriptionResponse = (await res.json()) as CreateSubscriptionResponse; return createSubscriptionResponse.subscriptionId; } From 9914093a5eb7965b3ace0dab44e06f605ff17d13 Mon Sep 17 00:00:00 2001 From: Adarsh Jha <132337675+adarsh-jha-dev@users.noreply.github.com> Date: Mon, 16 Oct 2023 16:22:42 +0530 Subject: [PATCH 2/3] Enforce 1-year Max Interval and Correct max_amount Field --- .../components/ConfirmSubscriptionForm.tsx | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/app/confirm/components/ConfirmSubscriptionForm.tsx b/app/confirm/components/ConfirmSubscriptionForm.tsx index bea2148..5de40a6 100644 --- a/app/confirm/components/ConfirmSubscriptionForm.tsx +++ b/app/confirm/components/ConfirmSubscriptionForm.tsx @@ -10,7 +10,7 @@ import { Box } from "app/components/Box"; import { SubscriptionSummary } from "app/confirm/components/SubscriptionSummary"; import Link from "next/link"; import { Button } from "app/components/Button"; -import { Loading } from "app/components/Loading"; +import { Loading from "app/components/Loading"; import { toast } from "react-hot-toast"; import Image from "next/image"; import { Modal } from "app/components/Modal"; @@ -72,7 +72,6 @@ export function ConfirmSubscriptionForm({ }; const getSatoshisForInterval = (interval: string) => { - // Calculate the number of satoshis based on the desired interval switch (interval) { case "daily": return 1; // Adjust this as needed @@ -90,20 +89,19 @@ export function ConfirmSubscriptionForm({ toast.error("Please link your wallet"); return; } - + const selectedInterval = data.sleepDuration; - - // Enforce that the interval should not be more than 1 year (365 days) - if (selectedInterval === "yearly") { + + const oneYearInMilliseconds = 31536000000; + if (selectedInterval === "yearly" && selectedInterval > oneYearInMilliseconds) { toast.error("Interval cannot be more than 1 year"); return; } - - // Calculate the number of satoshis based on the desired interval + const satoshis = getSatoshisForInterval(selectedInterval); - - data.amount = satoshis; - + + data.max_amount = satoshis; + const subscriptionId = await createSubscription(data); if (subscriptionId) { toast.success("Recurring payment created"); @@ -116,6 +114,7 @@ export function ConfirmSubscriptionForm({ } }); + const isLoading = isSubmitting || isNavigating; return ( From c743d75a6b0f2fa5f07632699b3c03e094bbd5fe Mon Sep 17 00:00:00 2001 From: Adarsh Jha <132337675+adarsh-jha-dev@users.noreply.github.com> Date: Mon, 16 Oct 2023 16:45:11 +0530 Subject: [PATCH 3/3] Update ConfirmSubscriptionForm.tsx --- .../components/ConfirmSubscriptionForm.tsx | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/app/confirm/components/ConfirmSubscriptionForm.tsx b/app/confirm/components/ConfirmSubscriptionForm.tsx index 5de40a6..50b8c1c 100644 --- a/app/confirm/components/ConfirmSubscriptionForm.tsx +++ b/app/confirm/components/ConfirmSubscriptionForm.tsx @@ -10,7 +10,7 @@ import { Box } from "app/components/Box"; import { SubscriptionSummary } from "app/confirm/components/SubscriptionSummary"; import Link from "next/link"; import { Button } from "app/components/Button"; -import { Loading from "app/components/Loading"; +import { Loading } from "app/components/Loading"; import { toast } from "react-hot-toast"; import Image from "next/image"; import { Modal } from "app/components/Modal"; @@ -62,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) { @@ -71,16 +71,17 @@ export function ConfirmSubscriptionForm({ } }; - const getSatoshisForInterval = (interval: string) => { + const getMillisecondsForInterval = (interval: string) => { switch (interval) { case "daily": - return 1; // Adjust this as needed + return 24 * 60 * 60 * 1000; // 1 day in milliseconds case "weekly": - return 7; // Adjust this as needed + return 7 * 24 * 60 * 60 * 1000; // 1 week in milliseconds case "monthly": - return 30; // Adjust this as needed + // Note: This is a simplified calculation; adjust as needed + return 30 * 24 * 60 * 60 * 1000; // 30 days in milliseconds default: - return 1; // Default to daily + return 24 * 60 * 60 * 1000; // Default to daily (1 day in milliseconds) } }; @@ -91,14 +92,15 @@ export function ConfirmSubscriptionForm({ } const selectedInterval = data.sleepDuration; + const intervalMilliseconds = getMillisecondsForInterval(selectedInterval); - const oneYearInMilliseconds = 31536000000; - if (selectedInterval === "yearly" && selectedInterval > oneYearInMilliseconds) { + 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(selectedInterval); + const satoshis = getSatoshisForInterval(intervalMilliseconds); data.max_amount = satoshis; @@ -114,7 +116,6 @@ export function ConfirmSubscriptionForm({ } }); - const isLoading = isSubmitting || isNavigating; return ( @@ -166,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.