From dc842eab6b7a3b2e891ee89a325ec73e04d3be95 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 3 May 2022 00:16:03 -0300 Subject: deposit from payto --- .../taler-wallet-webextension/src/cta/Deposit.tsx | 158 +++++++++++++++++++-- 1 file changed, 144 insertions(+), 14 deletions(-) (limited to 'packages/taler-wallet-webextension/src/cta/Deposit.tsx') diff --git a/packages/taler-wallet-webextension/src/cta/Deposit.tsx b/packages/taler-wallet-webextension/src/cta/Deposit.tsx index 529da11ba..2fc7cbc41 100644 --- a/packages/taler-wallet-webextension/src/cta/Deposit.tsx +++ b/packages/taler-wallet-webextension/src/cta/Deposit.tsx @@ -24,48 +24,120 @@ * Imports. */ +import { + AmountJson, + Amounts, + AmountString, + CreateDepositGroupResponse, +} from "@gnu-taler/taler-util"; import { Fragment, h, VNode } from "preact"; +import { useState } from "preact/hooks"; +import { Amount } from "../components/Amount.js"; import { Loading } from "../components/Loading.js"; import { LoadingError } from "../components/LoadingError.js"; import { LogoHeader } from "../components/LogoHeader.js"; -import { SubTitle, WalletAction } from "../components/styled/index.js"; +import { Part } from "../components/Part.js"; +import { + ButtonSuccess, + SubTitle, + WalletAction, +} from "../components/styled/index.js"; import { useTranslationContext } from "../context/translation.js"; -import { HookError } from "../hooks/useAsyncAsHook.js"; +import { HookError, useAsyncAsHook } from "../hooks/useAsyncAsHook.js"; +import { ButtonHandler } from "../mui/handlers.js"; +import * as wxApi from "../wxApi.js"; interface Props { talerDepositUri?: string; + amount: AmountString; goBack: () => void; } -type State = Loading | Ready; +type State = Loading | Ready | Completed; interface Loading { status: "loading"; hook: HookError | undefined; } interface Ready { status: "ready"; + hook: undefined; + fee: AmountJson; + cost: AmountJson; + effective: AmountJson; + confirm: ButtonHandler; } +interface Completed { + status: "completed"; + hook: undefined; +} + +export function useComponentState( + talerDepositUri: string | undefined, + amountStr: AmountString | undefined, + api: typeof wxApi, +): State { + const [result, setResult] = useState( + undefined, + ); + + const info = useAsyncAsHook(async () => { + if (!talerDepositUri) throw Error("ERROR_NO-URI-FOR-DEPOSIT"); + if (!amountStr) throw Error("ERROR_NO-AMOUNT-FOR-DEPOSIT"); + const amount = Amounts.parse(amountStr); + if (!amount) throw Error("ERROR_INVALID-AMOUNT-FOR-DEPOSIT"); + const deposit = await api.prepareDeposit( + talerDepositUri, + Amounts.stringify(amount), + ); + return { deposit, uri: talerDepositUri, amount }; + }); + + if (!info || info.hasError) { + return { + status: "loading", + hook: info, + }; + } + + const { deposit, uri, amount } = info.response; + async function doDeposit(): Promise { + const resp = await api.createDepositGroup(uri, Amounts.stringify(amount)); + setResult(resp); + } + + if (result !== undefined) { + return { + status: "completed", + hook: undefined, + }; + } -function useComponentState(uri: string | undefined): State { return { - status: "loading", + status: "ready", hook: undefined, + confirm: { + onClick: doDeposit, + }, + fee: Amounts.sub(deposit.totalDepositCost, deposit.effectiveDepositAmount) + .amount, + cost: deposit.totalDepositCost, + effective: deposit.effectiveDepositAmount, }; } -export function DepositPage({ talerDepositUri, goBack }: Props): VNode { +export function DepositPage({ talerDepositUri, amount, goBack }: Props): VNode { const { i18n } = useTranslationContext(); - const state = useComponentState(talerDepositUri); - if (state.status === "loading") { - if (!state.hook) return ; + const state = useComponentState(talerDepositUri, amount, wxApi); + + if (!talerDepositUri) { return ( - Could not load pay status} - error={state.hook} - /> + + missing taler deposit uri + ); } + return ; } @@ -75,13 +147,71 @@ export interface ViewProps { export function View({ state }: ViewProps): VNode { const { i18n } = useTranslationContext(); + if (state.status === "loading") { + if (!state.hook) return ; + return ( + Could not load deposit status} + error={state.hook} + /> + ); + } + + if (state.status === "completed") { + return ( + + + + + Digital cash deposit + +
+

+ deposit completed +

+
+
+ ); + } + return ( - Digital cash refund + Digital cash deposit +
+ {Amounts.isNonZero(state.cost) && ( + Cost} + text={} + kind="negative" + /> + )} + {Amounts.isNonZero(state.fee) && ( + Fee} + text={} + kind="negative" + /> + )} + To be received} + text={} + kind="positive" + /> +
+
+ + + Deposit {} + + +
); } -- cgit v1.2.3