diff options
author | Sebastian <sebasjm@gmail.com> | 2024-06-07 13:40:33 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2024-06-07 13:40:33 -0300 |
commit | 76042e8ddcee595d332c0e9c83b49d2f21895de2 (patch) | |
tree | d32a26f6a84373657fc2cc6a41b49963a47d2648 /packages/taler-wallet-webextension/src/cta/PaymentTemplate/state.ts | |
parent | 3f35ef8ed328c7736cb4eb76b4ec7874274b4d7d (diff) | |
download | wallet-core-76042e8ddcee595d332c0e9c83b49d2f21895de2.tar.xz |
payment template: show fixed values
Diffstat (limited to 'packages/taler-wallet-webextension/src/cta/PaymentTemplate/state.ts')
-rw-r--r-- | packages/taler-wallet-webextension/src/cta/PaymentTemplate/state.ts | 105 |
1 files changed, 64 insertions, 41 deletions
diff --git a/packages/taler-wallet-webextension/src/cta/PaymentTemplate/state.ts b/packages/taler-wallet-webextension/src/cta/PaymentTemplate/state.ts index 1a92c4073..ba854a93c 100644 --- a/packages/taler-wallet-webextension/src/cta/PaymentTemplate/state.ts +++ b/packages/taler-wallet-webextension/src/cta/PaymentTemplate/state.ts @@ -47,12 +47,18 @@ export function useComponentState({ const hook = useAsyncAsHook(async () => { if (!talerTemplateUri) throw Error("ERROR_NO-URI-FOR-PAYMENT-TEMPLATE"); const templateP = await api.wallet.call( - WalletApiOperation.CheckPayForTemplate, { talerPayTemplateUri: talerTemplateUri }, + WalletApiOperation.CheckPayForTemplate, + { talerPayTemplateUri: talerTemplateUri }, ); - const requireMoreInfo = !templateP.templateDetails.template_contract.amount || !templateP.templateDetails.template_contract.summary; + const requireMoreInfo = + !templateP.templateDetails.template_contract.amount || + !templateP.templateDetails.template_contract.summary; let payStatus: PreparePayResult | undefined = undefined; if (!requireMoreInfo) { - payStatus = await api.wallet.call(WalletApiOperation.PreparePayForTemplate, { talerPayTemplateUri: talerTemplateUri }); + payStatus = await api.wallet.call( + WalletApiOperation.PreparePayForTemplate, + { talerPayTemplateUri: talerTemplateUri }, + ); } const balance = await api.wallet.call(WalletApiOperation.GetBalances, {}); return { payStatus, balance, uri: talerTemplateUri, templateP }; @@ -102,20 +108,28 @@ export function useComponentState({ const cfg = hook.response.templateP.templateDetails.template_contract; const def = hook.response.templateP.templateDetails.editable_defaults; - const fixedAmount = cfg.amount !== undefined ? Amounts.parseOrThrow(cfg.amount) : undefined; - const fixedSummary = cfg.summary !== undefined ? cfg.summary : undefined; - - const defaultAmount = def?.amount !== undefined ? Amounts.parseOrThrow(def.amount) : undefined; - const defaultSummary = def?.summary !== undefined ? def.summary : undefined; - - const zero = fixedAmount ? Amounts.zeroOfAmount(fixedAmount) : - cfg.currency !== undefined ? Amounts.zeroOfCurrency(cfg.currency) : - defaultAmount !== undefined ? Amounts.zeroOfAmount(defaultAmount) : - def?.currency !== undefined ? Amounts.zeroOfCurrency(def.currency) : - Amounts.zeroOfCurrency(hook.response.templateP.supportedCurrencies[0]); - - const [amount, setAmount] = useState(defaultAmount ?? zero); - const [summary, setSummary] = useState(defaultSummary ?? ""); + const fixedAmount = + cfg.amount !== undefined ? Amounts.parseOrThrow(cfg.amount) : undefined; + const fixedSummary = cfg.summary; + + const defaultAmount = + def?.amount !== undefined ? Amounts.parseOrThrow(def.amount) : undefined; + const defaultSummary = def?.summary; + + const zero = fixedAmount + ? Amounts.zeroOfAmount(fixedAmount) + : cfg.currency !== undefined + ? Amounts.zeroOfCurrency(cfg.currency) + : defaultAmount !== undefined + ? Amounts.zeroOfAmount(defaultAmount) + : def?.currency !== undefined + ? Amounts.zeroOfCurrency(def.currency) + : Amounts.zeroOfCurrency( + hook.response.templateP.supportedCurrencies[0], + ); + + const [amount, setAmount] = useState(defaultAmount ?? fixedAmount ?? zero); + const [summary, setSummary] = useState(defaultSummary ?? fixedSummary ?? ""); async function createOrder() { try { @@ -140,41 +154,50 @@ export function useComponentState({ } const errors = undefinedIfEmpty({ - amount: fixedAmount !== undefined ? undefined : amount && Amounts.isZero(amount) ? i18n.str`required` : undefined, - summary: fixedSummary !== undefined ? undefined : summary !== undefined && !summary ? i18n.str`required` : undefined, + amount: + fixedAmount !== undefined + ? undefined + : amount && Amounts.isZero(amount) + ? i18n.str`required` + : undefined, + summary: + fixedSummary !== undefined + ? undefined + : summary !== undefined && !summary + ? i18n.str`required` + : undefined, }); return { status: "fill-template", error: undefined, minAge: cfg.minimum_age ?? 0, - amount: - fixedAmount === undefined - ? ({ - onInput: (a) => { - setAmount(a); - }, - value: amount, - error: errors?.amount, - } as AmountFieldHandler) - : undefined, - summary: - fixedSummary === undefined - ? ({ - onInput: (t) => { - setSummary(t); - }, - value: summary, - error: errors?.summary, - } as TextFieldHandler) - : undefined, + amount: { + onInput: + fixedAmount !== undefined + ? undefined + : (a) => { + setAmount(a); + }, + value: amount, + error: errors?.amount, + } as AmountFieldHandler, + summary: { + onInput: + fixedSummary !== undefined + ? undefined + : (t) => { + setSummary(t); + }, + value: summary, + error: errors?.summary, + } as TextFieldHandler, onCreate: { onClick: errors ? undefined : safely("create order for pay template", createOrder), }, }; - } - + }; } function undefinedIfEmpty<T extends object>(obj: T): T | undefined { |