diff options
Diffstat (limited to 'packages/taler-wallet-webextension/src/cta/TransferCreate/state.ts')
-rw-r--r-- | packages/taler-wallet-webextension/src/cta/TransferCreate/state.ts | 80 |
1 files changed, 49 insertions, 31 deletions
diff --git a/packages/taler-wallet-webextension/src/cta/TransferCreate/state.ts b/packages/taler-wallet-webextension/src/cta/TransferCreate/state.ts index 089f46047..c5e143f42 100644 --- a/packages/taler-wallet-webextension/src/cta/TransferCreate/state.ts +++ b/packages/taler-wallet-webextension/src/cta/TransferCreate/state.ts @@ -14,7 +14,11 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -import { Amounts, TalerErrorDetail, TalerProtocolTimestamp } from "@gnu-taler/taler-util"; +import { + Amounts, + TalerErrorDetail, + TalerProtocolTimestamp, +} from "@gnu-taler/taler-util"; import { TalerError, WalletApiOperation } from "@gnu-taler/taler-wallet-core"; import { format, isFuture, parse } from "date-fns"; import { useState } from "preact/hooks"; @@ -29,52 +33,57 @@ export function useComponentState( const amount = Amounts.parseOrThrow(amountStr); const [subject, setSubject] = useState<string | undefined>(); - const [timestamp, setTimestamp] = useState<string | undefined>() + const [timestamp, setTimestamp] = useState<string | undefined>(); const [operationError, setOperationError] = useState< TalerErrorDetail | undefined >(undefined); - const hook = useAsyncAsHook(async () => { - const resp = await api.wallet.call(WalletApiOperation.PreparePeerPushPayment, { - amount: amountStr - }) - return resp - }) + const resp = await api.wallet.call( + WalletApiOperation.PreparePeerPushPayment, + { + amount: amountStr, + }, + ); + return resp; + }); if (!hook) { return { status: "loading", - error: undefined - } + error: undefined, + }; } if (hook.hasError) { return { status: "loading-uri", - error: hook - } + error: hook, + }; } - const { amountEffective, amountRaw } = hook.response - const debitAmount = Amounts.parseOrThrow(amountRaw) - const toBeReceived = Amounts.parseOrThrow(amountEffective) + const { amountEffective, amountRaw } = hook.response; + const debitAmount = Amounts.parseOrThrow(amountRaw); + const toBeReceived = Amounts.parseOrThrow(amountEffective); - let purse_expiration: TalerProtocolTimestamp | undefined = undefined + let purse_expiration: TalerProtocolTimestamp | undefined = undefined; let timestampError: string | undefined = undefined; - const t = timestamp === undefined ? undefined : parse(timestamp, "dd/MM/yyyy", new Date()) + const t = + timestamp === undefined + ? undefined + : parse(timestamp, "dd/MM/yyyy", new Date()); if (t !== undefined) { if (Number.isNaN(t.getTime())) { - timestampError = 'Should have the format "dd/MM/yyyy"' + timestampError = 'Should have the format "dd/MM/yyyy"'; } else { if (!isFuture(t)) { - timestampError = 'Should be in the future' + timestampError = "Should be in the future"; } else { purse_expiration = { - t_s: t.getTime() / 1000 - } + t_s: t.getTime() / 1000, + }; } } } @@ -82,13 +91,16 @@ export function useComponentState( async function accept(): Promise<void> { if (!subject || !purse_expiration) return; try { - const resp = await api.wallet.call(WalletApiOperation.InitiatePeerPushPayment, { - partialContractTerms: { - summary: subject, - amount: amountStr, - purse_expiration + const resp = await api.wallet.call( + WalletApiOperation.InitiatePeerPushPayment, + { + partialContractTerms: { + summary: subject, + amount: amountStr, + purse_expiration, + }, }, - }); + ); onSuccess(resp.transactionId); } catch (e) { if (e instanceof TalerError) { @@ -99,7 +111,8 @@ export function useComponentState( } } - const unableToCreate = !subject || Amounts.isZero(amount) || !purse_expiration + const unableToCreate = + !subject || Amounts.isZero(amount) || !purse_expiration; return { status: "ready", @@ -107,7 +120,12 @@ export function useComponentState( onClick: onClose, }, subject: { - error: subject === undefined ? undefined : !subject ? "Can't be empty" : undefined, + error: + subject === undefined + ? undefined + : !subject + ? "Can't be empty" + : undefined, value: subject ?? "", onInput: async (e) => setSubject(e), }, @@ -115,8 +133,8 @@ export function useComponentState( error: timestampError, value: timestamp === undefined ? "" : timestamp, onInput: async (e) => { - setTimestamp(e) - } + setTimestamp(e); + }, }, create: { onClick: unableToCreate ? undefined : accept, |