aboutsummaryrefslogtreecommitdiff
path: root/packages/demobank-ui/src/hooks
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2023-11-22 13:33:44 -0300
committerSebastian <sebasjm@gmail.com>2023-11-22 15:20:28 -0300
commit33c0267b37eecf44dc9f04e124eb44d27cba700c (patch)
tree9d5549dbb5a415b44005ab05711fc66c4643cbc9 /packages/demobank-ui/src/hooks
parent5eec408d9fb5e8c2375937166997ef8267a4053c (diff)
downloadwallet-core-33c0267b37eecf44dc9f04e124eb44d27cba700c.tar.xz
settings and preferences, getting conversion info
Diffstat (limited to 'packages/demobank-ui/src/hooks')
-rw-r--r--packages/demobank-ui/src/hooks/backend.ts1
-rw-r--r--packages/demobank-ui/src/hooks/circuit.ts63
-rw-r--r--packages/demobank-ui/src/hooks/preferences.ts (renamed from packages/demobank-ui/src/hooks/settings.ts)30
3 files changed, 43 insertions, 51 deletions
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<TransferCalculation>;
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/settings.ts b/packages/demobank-ui/src/hooks/preferences.ts
index bd48ca680..a1525ac80 100644
--- a/packages/demobank-ui/src/hooks/settings.ts
+++ b/packages/demobank-ui/src/hooks/preferences.ts
@@ -25,7 +25,7 @@ import {
} from "@gnu-taler/taler-util";
import { buildStorageKey, useLocalStorage, useTranslationContext } from "@gnu-taler/web-util/browser";
-interface Settings {
+interface Preferences {
currentWithdrawalOperationId: string | undefined;
showWithdrawalSuccess: boolean;
showDemoDescription: boolean;
@@ -36,11 +36,11 @@ interface Settings {
}
-export function getAllBooleanSettings(): Array<keyof Settings> {
+export function getAllBooleanPreferences(): Array<keyof Preferences> {
return ["fastWithdrawal", "showDebugInfo", "showDemoDescription", "showInstallWallet", "showWithdrawalSuccess"]
}
-export function getLabelForSetting(k: keyof Settings, i18n: ReturnType<typeof useTranslationContext>["i18n"]): TranslatedString {
+export function getLabelForPreferences(k: keyof Preferences, i18n: ReturnType<typeof useTranslationContext>["i18n"]): TranslatedString {
switch (k) {
case "currentWithdrawalOperationId": return i18n.str`Current withdrawal operation`
case "maxWithdrawalAmount": return i18n.str`Max withdrawal amount`
@@ -52,8 +52,8 @@ export function getLabelForSetting(k: keyof Settings, i18n: ReturnType<typeof us
}
}
-export const codecForSettings = (): Codec<Settings> =>
- buildCodecForObject<Settings>()
+export const codecForPreferences = (): Codec<Preferences> =>
+ buildCodecForObject<Preferences>()
.property("currentWithdrawalOperationId", codecOptional(codecForString()))
.property("showWithdrawalSuccess", (codecForBoolean()))
.property("showDemoDescription", (codecForBoolean()))
@@ -63,7 +63,7 @@ export const codecForSettings = (): Codec<Settings> =>
.property("maxWithdrawalAmount", codecForNumber())
.build("Settings");
-const defaultSettings: Settings = {
+const defaultPreferences: Preferences = {
currentWithdrawalOperationId: undefined,
showWithdrawalSuccess: true,
showDemoDescription: true,
@@ -73,21 +73,21 @@ const defaultSettings: Settings = {
showDebugInfo: false,
};
-const BANK_SETTINGS_KEY = buildStorageKey(
- "bank-settings",
- codecForSettings(),
+const BANK_PREFERENCES_KEY = buildStorageKey(
+ "bank-preferences",
+ codecForPreferences(),
);
-export function useSettings(): [
- Readonly<Settings>,
- <T extends keyof Settings>(key: T, value: Settings[T]) => void,
+export function usePreferences(): [
+ Readonly<Preferences>,
+ <T extends keyof Preferences>(key: T, value: Preferences[T]) => void,
] {
const { value, update } = useLocalStorage(
- BANK_SETTINGS_KEY,
- defaultSettings,
+ BANK_PREFERENCES_KEY,
+ defaultPreferences,
);
- function updateField<T extends keyof Settings>(k: T, v: Settings[T]) {
+ function updateField<T extends keyof Preferences>(k: T, v: Preferences[T]) {
const newValue = { ...value, [k]: v };
update(newValue);
}