From 33c0267b37eecf44dc9f04e124eb44d27cba700c Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 22 Nov 2023 13:33:44 -0300 Subject: settings and preferences, getting conversion info --- packages/demobank-ui/src/hooks/backend.ts | 1 - packages/demobank-ui/src/hooks/circuit.ts | 63 ++++++++---------- packages/demobank-ui/src/hooks/preferences.ts | 95 +++++++++++++++++++++++++++ packages/demobank-ui/src/hooks/settings.ts | 95 --------------------------- 4 files changed, 123 insertions(+), 131 deletions(-) create mode 100644 packages/demobank-ui/src/hooks/preferences.ts delete mode 100644 packages/demobank-ui/src/hooks/settings.ts (limited to 'packages/demobank-ui/src/hooks') diff --git a/packages/demobank-ui/src/hooks/backend.ts b/packages/demobank-ui/src/hooks/backend.ts index 93d647e73..863b47bf3 100644 --- a/packages/demobank-ui/src/hooks/backend.ts +++ b/packages/demobank-ui/src/hooks/backend.ts @@ -29,7 +29,6 @@ import { useLocalStorage } from "@gnu-taler/web-util/browser"; import { useSWRConfig } from "swr"; -import { bankUiSettings } from "../settings.js"; /** * Has the information to reach and diff --git a/packages/demobank-ui/src/hooks/circuit.ts b/packages/demobank-ui/src/hooks/circuit.ts index d0d180a53..c0164d60a 100644 --- a/packages/demobank-ui/src/hooks/circuit.ts +++ b/packages/demobank-ui/src/hooks/circuit.ts @@ -34,9 +34,7 @@ export type TransferCalculation = { }; type EstimatorFunction = ( amount: AmountJson, - currency: string, - sellFee: AmountJson, - sellRate: number, + fee: AmountJson, ) => Promise; type CashoutEstimators = { @@ -73,25 +71,19 @@ export function useEstimator(): CashoutEstimators { const { state } = useBackendState(); const { api } = useBankCoreApiContext(); return { - estimateByCredit: async (fiatAmount, regionalCurrency, fee, rate) => { - // const resp = await api.getConversionInfoAPI().getCashoutRate({ - // credit: amount - // }); - // if (resp.type === "fail") { - // // can't happen - // // not-supported: it should not be able to call this function - // // wrong-calculation: we are using just one parameter - // throw TalerError.fromDetail(resp.detail.code, {}, resp.detail.hint) - // } - const credit = fiatAmount; - const beforeFee = Amounts.sub(credit, fee).amount; - - // const debit = Amounts.parseOrThrow(resp.body.amount_debit); - //FIXME: remove this when endpoint works - const debit = Amounts.add( - Amounts.zeroOfCurrency(regionalCurrency), - beforeFee - ).amount; + estimateByCredit: async (fiatAmount, fee) => { + const resp = await api.getConversionInfoAPI().getCashoutRate({ + credit: fiatAmount + }); + if (resp.type === "fail") { + // can't happen + // not-supported: it should not be able to call this function + // wrong-calculation: we are using just one parameter + throw TalerError.fromDetail(resp.detail.code, {}, resp.detail.hint) + } + const credit = Amounts.parseOrThrow(resp.body.amount_credit); + const debit = Amounts.parseOrThrow(resp.body.amount_debit); + const beforeFee = Amounts.add(credit, fee).amount; return { debit, @@ -99,19 +91,20 @@ export function useEstimator(): CashoutEstimators { credit, }; }, - estimateByDebit: async (regionalAmount, fiatCurrency, fee, rate) => { - // const resp = await api.getConversionInfoAPI().getCashoutRate({ debit: amount }); - // if (resp.type === "fail") { - // // can't happen - // // not-supported: it should not be able to call this function - // // wrong-calculation: we are using just one parameter - // throw TalerError.fromDetail(resp.detail.code, {}, resp.detail.hint) - // } - // const credit = Amounts.parseOrThrow(resp.body.amount_credit); - const debit = regionalAmount; - const _credit = Amounts.parseOrThrow(regionalAmount); - const beforeFee = { ..._credit, currency: fiatCurrency }; - const credit = Amounts.sub(beforeFee, fee).amount; + estimateByDebit: async (regionalAmount, fee) => { + const resp = await api.getConversionInfoAPI().getCashoutRate({ + debit: regionalAmount + }); + if (resp.type === "fail") { + // can't happen + // not-supported: it should not be able to call this function + // wrong-calculation: we are using just one parameter + throw TalerError.fromDetail(resp.detail.code, {}, resp.detail.hint) + } + const credit = Amounts.parseOrThrow(resp.body.amount_credit); + const debit = Amounts.parseOrThrow(resp.body.amount_debit); + const beforeFee = Amounts.add(credit, fee).amount; + return { debit, beforeFee, diff --git a/packages/demobank-ui/src/hooks/preferences.ts b/packages/demobank-ui/src/hooks/preferences.ts new file mode 100644 index 000000000..a1525ac80 --- /dev/null +++ b/packages/demobank-ui/src/hooks/preferences.ts @@ -0,0 +1,95 @@ +/* + This file is part of GNU Taler + (C) 2022 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see + */ + +import { + Codec, + TranslatedString, + buildCodecForObject, + codecForBoolean, + codecForNumber, + codecForString, + codecOptional +} from "@gnu-taler/taler-util"; +import { buildStorageKey, useLocalStorage, useTranslationContext } from "@gnu-taler/web-util/browser"; + +interface Preferences { + currentWithdrawalOperationId: string | undefined; + showWithdrawalSuccess: boolean; + showDemoDescription: boolean; + showInstallWallet: boolean; + maxWithdrawalAmount: number; + fastWithdrawal: boolean; + showDebugInfo: boolean; + +} + +export function getAllBooleanPreferences(): Array { + return ["fastWithdrawal", "showDebugInfo", "showDemoDescription", "showInstallWallet", "showWithdrawalSuccess"] +} + +export function getLabelForPreferences(k: keyof Preferences, i18n: ReturnType["i18n"]): TranslatedString { + switch (k) { + case "currentWithdrawalOperationId": return i18n.str`Current withdrawal operation` + case "maxWithdrawalAmount": return i18n.str`Max withdrawal amount` + case "showWithdrawalSuccess": return i18n.str`Show withdrawal confirmation` + case "showDemoDescription": return i18n.str`Show demo description` + case "showInstallWallet": return i18n.str`Show install wallet first` + case "fastWithdrawal": return i18n.str`Use fast withdrawal form` + case "showDebugInfo": return i18n.str`Show debug info` + } +} + +export const codecForPreferences = (): Codec => + buildCodecForObject() + .property("currentWithdrawalOperationId", codecOptional(codecForString())) + .property("showWithdrawalSuccess", (codecForBoolean())) + .property("showDemoDescription", (codecForBoolean())) + .property("showInstallWallet", (codecForBoolean())) + .property("fastWithdrawal", (codecForBoolean())) + .property("showDebugInfo", (codecForBoolean())) + .property("maxWithdrawalAmount", codecForNumber()) + .build("Settings"); + +const defaultPreferences: Preferences = { + currentWithdrawalOperationId: undefined, + showWithdrawalSuccess: true, + showDemoDescription: true, + showInstallWallet: true, + maxWithdrawalAmount: 25, + fastWithdrawal: false, + showDebugInfo: false, +}; + +const BANK_PREFERENCES_KEY = buildStorageKey( + "bank-preferences", + codecForPreferences(), +); + +export function usePreferences(): [ + Readonly, + (key: T, value: Preferences[T]) => void, +] { + const { value, update } = useLocalStorage( + BANK_PREFERENCES_KEY, + defaultPreferences, + ); + + function updateField(k: T, v: Preferences[T]) { + const newValue = { ...value, [k]: v }; + update(newValue); + } + return [value, updateField]; +} diff --git a/packages/demobank-ui/src/hooks/settings.ts b/packages/demobank-ui/src/hooks/settings.ts deleted file mode 100644 index bd48ca680..000000000 --- a/packages/demobank-ui/src/hooks/settings.ts +++ /dev/null @@ -1,95 +0,0 @@ -/* - This file is part of GNU Taler - (C) 2022 Taler Systems S.A. - - GNU Taler is free software; you can redistribute it and/or modify it under the - terms of the GNU General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along with - GNU Taler; see the file COPYING. If not, see - */ - -import { - Codec, - TranslatedString, - buildCodecForObject, - codecForBoolean, - codecForNumber, - codecForString, - codecOptional -} from "@gnu-taler/taler-util"; -import { buildStorageKey, useLocalStorage, useTranslationContext } from "@gnu-taler/web-util/browser"; - -interface Settings { - currentWithdrawalOperationId: string | undefined; - showWithdrawalSuccess: boolean; - showDemoDescription: boolean; - showInstallWallet: boolean; - maxWithdrawalAmount: number; - fastWithdrawal: boolean; - showDebugInfo: boolean; - -} - -export function getAllBooleanSettings(): Array { - return ["fastWithdrawal", "showDebugInfo", "showDemoDescription", "showInstallWallet", "showWithdrawalSuccess"] -} - -export function getLabelForSetting(k: keyof Settings, i18n: ReturnType["i18n"]): TranslatedString { - switch (k) { - case "currentWithdrawalOperationId": return i18n.str`Current withdrawal operation` - case "maxWithdrawalAmount": return i18n.str`Max withdrawal amount` - case "showWithdrawalSuccess": return i18n.str`Show withdrawal confirmation` - case "showDemoDescription": return i18n.str`Show demo description` - case "showInstallWallet": return i18n.str`Show install wallet first` - case "fastWithdrawal": return i18n.str`Use fast withdrawal form` - case "showDebugInfo": return i18n.str`Show debug info` - } -} - -export const codecForSettings = (): Codec => - buildCodecForObject() - .property("currentWithdrawalOperationId", codecOptional(codecForString())) - .property("showWithdrawalSuccess", (codecForBoolean())) - .property("showDemoDescription", (codecForBoolean())) - .property("showInstallWallet", (codecForBoolean())) - .property("fastWithdrawal", (codecForBoolean())) - .property("showDebugInfo", (codecForBoolean())) - .property("maxWithdrawalAmount", codecForNumber()) - .build("Settings"); - -const defaultSettings: Settings = { - currentWithdrawalOperationId: undefined, - showWithdrawalSuccess: true, - showDemoDescription: true, - showInstallWallet: true, - maxWithdrawalAmount: 25, - fastWithdrawal: false, - showDebugInfo: false, -}; - -const BANK_SETTINGS_KEY = buildStorageKey( - "bank-settings", - codecForSettings(), -); - -export function useSettings(): [ - Readonly, - (key: T, value: Settings[T]) => void, -] { - const { value, update } = useLocalStorage( - BANK_SETTINGS_KEY, - defaultSettings, - ); - - function updateField(k: T, v: Settings[T]) { - const newValue = { ...value, [k]: v }; - update(newValue); - } - return [value, updateField]; -} -- cgit v1.2.3