diff options
author | Sebastian <sebasjm@gmail.com> | 2022-09-16 14:29:35 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2022-09-16 14:29:35 -0300 |
commit | 6ddb2de84245ae3914c92b2b2eb7399e7f04500e (patch) | |
tree | 425cc377daf053e5283bee6986f6f437f25d7584 /packages/taler-wallet-webextension/src/cta/InvoicePay | |
parent | 860f10e6f004668d3109b5a1d96ddb2b26ddc2f5 (diff) | |
download | wallet-core-6ddb2de84245ae3914c92b2b2eb7399e7f04500e.tar.gz wallet-core-6ddb2de84245ae3914c92b2b2eb7399e7f04500e.tar.bz2 wallet-core-6ddb2de84245ae3914c92b2b2eb7399e7f04500e.zip |
pretty
Diffstat (limited to 'packages/taler-wallet-webextension/src/cta/InvoicePay')
3 files changed, 65 insertions, 51 deletions
diff --git a/packages/taler-wallet-webextension/src/cta/InvoicePay/index.ts b/packages/taler-wallet-webextension/src/cta/InvoicePay/index.ts index 71aedc638..731064661 100644 --- a/packages/taler-wallet-webextension/src/cta/InvoicePay/index.ts +++ b/packages/taler-wallet-webextension/src/cta/InvoicePay/index.ts @@ -14,7 +14,12 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -import { AbsoluteTime, AmountJson, PreparePayResult, TalerErrorDetail } from "@gnu-taler/taler-util"; +import { + AbsoluteTime, + AmountJson, + PreparePayResult, + TalerErrorDetail, +} from "@gnu-taler/taler-util"; import { Loading } from "../../components/Loading.js"; import { HookError } from "../../hooks/useAsyncAsHook.js"; import { ButtonHandler } from "../../mui/handlers.js"; @@ -37,7 +42,6 @@ export type State = | State.Ready; export namespace State { - export interface Loading { status: "loading"; error: undefined; @@ -52,20 +56,20 @@ export namespace State { error: undefined; uri: string; cancel: ButtonHandler; - amount: AmountJson, + amount: AmountJson; goToWalletManualWithdraw: (currency: string) => Promise<void>; - summary: string | undefined, - expiration: AbsoluteTime | undefined, + summary: string | undefined; + expiration: AbsoluteTime | undefined; operationError?: TalerErrorDetail; payStatus: PreparePayResult; } export interface NoBalanceForCurrency extends BaseInfo { - status: "no-balance-for-currency" + status: "no-balance-for-currency"; balance: undefined; } export interface NoEnoughBalance extends BaseInfo { - status: "no-enough-balance" + status: "no-enough-balance"; balance: AmountJson; } @@ -82,9 +86,11 @@ const viewMapping: StateViewMap<State> = { "loading-uri": LoadingUriView, "no-balance-for-currency": ReadyView, "no-enough-balance": ReadyView, - "ready": ReadyView, + ready: ReadyView, }; - -export const InvoicePayPage = compose("InvoicePayPage", (p: Props) => useComponentState(p, wxApi), viewMapping) - +export const InvoicePayPage = compose( + "InvoicePayPage", + (p: Props) => useComponentState(p, wxApi), + viewMapping, +); diff --git a/packages/taler-wallet-webextension/src/cta/InvoicePay/state.ts b/packages/taler-wallet-webextension/src/cta/InvoicePay/state.ts index f87cdf8e1..e69bb4627 100644 --- a/packages/taler-wallet-webextension/src/cta/InvoicePay/state.ts +++ b/packages/taler-wallet-webextension/src/cta/InvoicePay/state.ts @@ -14,7 +14,15 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -import { AbsoluteTime, Amounts, NotificationType, PreparePayResult, PreparePayResultType, TalerErrorDetail, TalerProtocolTimestamp } from "@gnu-taler/taler-util"; +import { + AbsoluteTime, + Amounts, + NotificationType, + PreparePayResult, + PreparePayResultType, + TalerErrorDetail, + TalerProtocolTimestamp, +} from "@gnu-taler/taler-util"; import { TalerError } from "@gnu-taler/taler-wallet-core"; import { useEffect, useState } from "preact/hooks"; import { useAsyncAsHook } from "../../hooks/useAsyncAsHook.js"; @@ -27,11 +35,11 @@ export function useComponentState( ): State { const hook = useAsyncAsHook(async () => { const p2p = await api.checkPeerPullPayment({ - talerUri: talerPayPullUri - }) + talerUri: talerPayPullUri, + }); const balance = await api.getBalance(); - return { p2p, balance } - }) + return { p2p, balance }; + }); useEffect(() => { api.onUpdateNotification([NotificationType.CoinWithdrawn], () => { @@ -39,13 +47,15 @@ export function useComponentState( }); }); - const [operationError, setOperationError] = useState<TalerErrorDetail | undefined>(undefined) + const [operationError, setOperationError] = useState< + TalerErrorDetail | undefined + >(undefined); if (!hook) { return { status: "loading", error: undefined, - } + }; } if (hook.hasError) { return { @@ -56,13 +66,17 @@ export function useComponentState( // const { payStatus } = hook.response.p2p; - const { amount: purseAmount, contractTerms, peerPullPaymentIncomingId } = hook.response.p2p + const { + amount: purseAmount, + contractTerms, + peerPullPaymentIncomingId, + } = hook.response.p2p; - - const amountStr: string = contractTerms?.amount - const amount = Amounts.parseOrThrow(amountStr) - const summary: string | undefined = contractTerms?.summary - const expiration: TalerProtocolTimestamp | undefined = contractTerms?.purse_expiration + const amountStr: string = contractTerms?.amount; + const amount = Amounts.parseOrThrow(amountStr); + const summary: string | undefined = contractTerms?.summary; + const expiration: TalerProtocolTimestamp | undefined = + contractTerms?.purse_expiration; const foundBalance = hook.response.balance.balances.find( (b) => Amounts.parseOrThrow(b.available).currency === amount.currency, @@ -71,35 +85,32 @@ export function useComponentState( const paymentPossible: PreparePayResult = { status: PreparePayResultType.PaymentPossible, proposalId: "fakeID", - contractTerms: { - } as any, + contractTerms: {} as any, contractTermsHash: "asd", amountRaw: hook.response.p2p.amount, amountEffective: hook.response.p2p.amount, noncePriv: "", - } as PreparePayResult + } as PreparePayResult; const insufficientBalance: PreparePayResult = { status: PreparePayResultType.InsufficientBalance, proposalId: "fakeID", - contractTerms: { - } as any, + contractTerms: {} as any, amountRaw: hook.response.p2p.amount, noncePriv: "", - } - + }; const baseResult = { uri: talerPayPullUri, cancel: { - onClick: onClose + onClick: onClose, }, amount, goToWalletManualWithdraw, summary, expiration: expiration ? AbsoluteTime.fromTimestamp(expiration) : undefined, operationError, - } + }; if (!foundBalance) { return { @@ -108,20 +119,21 @@ export function useComponentState( balance: undefined, ...baseResult, payStatus: insufficientBalance, - } + }; } const foundAmount = Amounts.parseOrThrow(foundBalance.available); //FIXME: should use pay result type since it check for coins exceptions - if (Amounts.cmp(foundAmount, amount) < 0) { //payStatus.status === PreparePayResultType.InsufficientBalance) { + if (Amounts.cmp(foundAmount, amount) < 0) { + //payStatus.status === PreparePayResultType.InsufficientBalance) { return { - status: 'no-enough-balance', + status: "no-enough-balance", error: undefined, balance: foundAmount, ...baseResult, payStatus: insufficientBalance, - } + }; } // if (payStatus.status === PreparePayResultType.AlreadyConfirmed) { @@ -135,19 +147,18 @@ export function useComponentState( async function accept(): Promise<void> { try { const resp = await api.acceptPeerPullPayment({ - peerPullPaymentIncomingId - }) - await onClose() + peerPullPaymentIncomingId, + }); + await onClose(); } catch (e) { if (e instanceof TalerError) { - setOperationError(e.errorDetail) + setOperationError(e.errorDetail); } - console.error(e) - throw Error("error trying to accept") + console.error(e); + throw Error("error trying to accept"); } } - return { status: "ready", error: undefined, @@ -155,7 +166,7 @@ export function useComponentState( payStatus: paymentPossible, balance: foundAmount, accept: { - onClick: accept + onClick: accept, }, - } + }; } diff --git a/packages/taler-wallet-webextension/src/cta/InvoicePay/test.ts b/packages/taler-wallet-webextension/src/cta/InvoicePay/test.ts index 631e76d01..eae4d4ca2 100644 --- a/packages/taler-wallet-webextension/src/cta/InvoicePay/test.ts +++ b/packages/taler-wallet-webextension/src/cta/InvoicePay/test.ts @@ -22,10 +22,7 @@ import { expect } from "chai"; describe("test description", () => { - it("should assert", () => { - - expect([]).deep.equals([]) + expect([]).deep.equals([]); }); -}) - +}); |