From 70b08d1760e1be0f48c3fc4f802dd615bf9621d8 Mon Sep 17 00:00:00 2001 From: boufni95 Date: Fri, 20 Sep 2024 17:03:51 +0000 Subject: [PATCH] move modal to main --- src/Components/Background.tsx | 2 + .../BackgroundJobs/DebitRequestHandler.tsx | 59 +++++++++ src/Pages/LinkedApp/index.tsx | 114 +++++++----------- src/globalTypes.ts | 5 +- 4 files changed, 108 insertions(+), 72 deletions(-) create mode 100644 src/Components/BackgroundJobs/DebitRequestHandler.tsx diff --git a/src/Components/Background.tsx b/src/Components/Background.tsx index d62a5d81..d5a39a12 100644 --- a/src/Components/Background.tsx +++ b/src/Components/Background.tsx @@ -26,6 +26,7 @@ import Toast from "./Toast"; import { useHistory } from "react-router"; import { RemoteBackup } from "./BackgroundJobs/RemoteBackup"; import { App } from "@capacitor/app"; +import { DebitRequestHandler } from "./BackgroundJobs/DebitRequestHandler"; export const Background = () => { @@ -362,6 +363,7 @@ export const Background = () => { + { toggle() }} modalContent={clipBoardContent} headerText={''} /> } diff --git a/src/Components/BackgroundJobs/DebitRequestHandler.tsx b/src/Components/BackgroundJobs/DebitRequestHandler.tsx new file mode 100644 index 00000000..72723e02 --- /dev/null +++ b/src/Components/BackgroundJobs/DebitRequestHandler.tsx @@ -0,0 +1,59 @@ +import { useCallback, useEffect, useState } from "react"; +import { DebitAuthorization, LiveDebitRequest, LiveDebitRequest_debit_type } from "../../Api/pub/autogenerated/ts/types"; +import { SourceDebitRequest } from "../../globalTypes"; +import { getNostrClient, parseNprofile } from "../../Api/nostr"; +import { selectNostrSpends, useSelector } from "../../State/store"; + +export const DebitRequestHandler = () => { + const nostrSpends = useSelector(selectNostrSpends); + const nodedUp = useSelector(state => state.nostrPrivateKey); + const [requestData, setRequestData] = useState(null); + useEffect(() => { + if (!nodedUp) { + return; + } + console.log("inside") + nostrSpends.forEach(source => { + const { pubkey, relays } = parseNprofile(source.pasteField) + getNostrClient({ pubkey, relays }, source.keys).then(c => { + c.GetLiveDebitRequests(debitReq => { + if (debitReq.status === "OK") { + console.log("Got one") + setRequestData({ request: debitReq, source }) + } + }) + }) + }); + }, [nostrSpends, nodedUp]) + const authroizeRequest = useCallback(async (request: SourceDebitRequest) => { + const res = await (await getNostrClient(request.source.pasteField, request.source.keys)).AuthorizeDebit({ authorize_npub: request.request.npub, rules: [] }); + if (res.status !== "OK") { + throw new Error(res.reason); + } + setRequestData(null) + }, []) + if (!requestData) { + return null + } + return ( + <> +
Incoming Request
+
{requestData.request.npub}
+
+ { + requestData.request.debit.type === LiveDebitRequest_debit_type.FREQUENCY + ? + `Wants you to pay ${requestData.request.amount} sats per ${requestData.request.debit.frequency.interval}, ${requestData.request.debit.frequency.number_of_intervals} times` + : + `Wants you to pay ${requestData.request.amount} sats` + } +
+
Wants to spend
+
{requestData.request.amount}
+
+ + +
+ + ) +} \ No newline at end of file diff --git a/src/Pages/LinkedApp/index.tsx b/src/Pages/LinkedApp/index.tsx index 937c3c4e..c3cca5ab 100644 --- a/src/Pages/LinkedApp/index.tsx +++ b/src/Pages/LinkedApp/index.tsx @@ -5,21 +5,21 @@ import { selectNostrSpends, useSelector } from "../../State/store"; import { getNostrClient, parseNprofile } from "../../Api/nostr"; import { DebitAuthorization, LiveDebitRequest, LiveDebitRequest_debit_type } from "../../Api/pub/autogenerated/ts/types"; import { SpendFrom } from "../../globalTypes"; -type SourceDebitRequest = { request: LiveDebitRequest, source: SpendFrom } + export const LinkedApp = () => { const { isShown, toggle } = UseModal(); - const [requestData, setRequestData] = useState(null); + //const [requestData, setRequestData] = useState(null); const [debitAuthorizations, setDebitAuthorizations] = useState([]) const nodedUp = useSelector(state => state.nostrPrivateKey); const nostrSpends = useSelector(selectNostrSpends); - const [updatehook, setUpdateHook] = useState(0) + //const [updatehook, setUpdateHook] = useState(0) useEffect(() => { - if (!nodedUp) { - return; - } - nostrSpends.forEach(source => { - const { pubkey, relays } = parseNprofile(source.pasteField) - getNostrClient({ pubkey, relays }, source.keys).then(c => { + if (!nodedUp) { + return; + } + nostrSpends.forEach(source => { + const { pubkey, relays } = parseNprofile(source.pasteField) + getNostrClient({ pubkey, relays }, source.keys).then(c => { c.GetDebitAuthorizations().then(res => { if (res.status === "OK") { setDebitAuthorizations((state) => [ @@ -30,78 +30,50 @@ export const LinkedApp = () => { ]) } }) - - }) - }); - }, [nostrSpends, nodedUp, updatehook]) - useEffect(() => { - if (!nodedUp) { - return; - } - console.log("inside") - nostrSpends.forEach(source => { - const { pubkey, relays } = parseNprofile(source.pasteField) - getNostrClient({ pubkey, relays }, source.keys).then(c => { - c.GetLiveDebitRequests(debitReq => { - if (debitReq.status === "OK") { - console.log("Got one") - setRequestData({ request: debitReq, source }) - toggle(); - } - }) - }) - }); - }, [nostrSpends, nodedUp]) - const authroizeRequest = useCallback(async (request: SourceDebitRequest) => { - const res = await (await getNostrClient(request.source.pasteField, request.source.keys)).AuthorizeDebit({ authorize_npub: request.request.npub, rules: [] }); - if (res.status !=="OK") { - throw new Error(res.reason); - } - setUpdateHook(Math.random()) - toggle(); - }, [toggle]) - + + }) + }); + }, [nostrSpends, nodedUp, /* updatehook */]) + /* useEffect(() => { + if (!nodedUp) { + return; + } + console.log("inside") + nostrSpends.forEach(source => { + const { pubkey, relays } = parseNprofile(source.pasteField) + getNostrClient({ pubkey, relays }, source.keys).then(c => { + c.GetLiveDebitRequests(debitReq => { + if (debitReq.status === "OK") { + console.log("Got one") + setRequestData({ request: debitReq, source }) + toggle(); + } + }) + }) + }); + }, [nostrSpends, nodedUp]) + const authroizeRequest = useCallback(async (request: SourceDebitRequest) => { + const res = await (await getNostrClient(request.source.pasteField, request.source.keys)).AuthorizeDebit({ authorize_npub: request.request.npub, rules: [] }); + if (res.status !== "OK") { + throw new Error(res.reason); + } + setUpdateHook(Math.random()) + toggle(); + }, [toggle]) */ + return (
Linked Apps
- { debitAuthorizations.map(res => ( + {debitAuthorizations.map(res => (
{res.npub}
- )) } + ))}
- } headerText={''} />
); }; -interface Props { - request: SourceDebitRequest, - authorize: (request: SourceDebitRequest) => Promise, - toggle: () => void -} -const IncomingRequestModal = ({ request, authorize, toggle }: Props) => { - return ( - -
Incoming Request
-
{request.request.npub}
-
- { - request.request.debit.type === LiveDebitRequest_debit_type.FREQUENCY - ? - `Wants you to pay ${request.request.amount} sats per ${request.request.debit.frequency.interval}, ${request.request.debit.frequency.number_of_intervals} times` - : - `Wants you to pay ${request.request.amount} sats` - } -
-
Wants to spend
-
{request.request.amount}
-
- - -
-
- ) -} \ No newline at end of file + diff --git a/src/globalTypes.ts b/src/globalTypes.ts index 3b715009..50cbbc6b 100644 --- a/src/globalTypes.ts +++ b/src/globalTypes.ts @@ -1,5 +1,6 @@ import React from "react"; import { NostrKeyPair } from "./Api/nostrHandler"; +import { LiveDebitRequest } from "./Api/pub/autogenerated/ts/types"; type PriceImgFunction = (arg1: string, arg2: number) => string; @@ -154,4 +155,6 @@ export interface FiatCurrency { url: string; currency: string; symbol: string; -} \ No newline at end of file +} + +export type SourceDebitRequest = { request: LiveDebitRequest, source: SpendFrom } \ No newline at end of file