diff options
Diffstat (limited to 'packages/taler-wallet-webextension/src/wallet/ExchangeSelection')
3 files changed, 73 insertions, 47 deletions
diff --git a/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/index.ts b/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/index.ts index 16ab7ab16..3b2708eff 100644 --- a/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/index.ts +++ b/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/index.ts @@ -14,16 +14,25 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -import { FeeDescription, FeeDescriptionPair, AbsoluteTime, ExchangeFullDetails, OperationMap } from "@gnu-taler/taler-util"; +import { + FeeDescription, + FeeDescriptionPair, + AbsoluteTime, + ExchangeFullDetails, + OperationMap, +} from "@gnu-taler/taler-util"; import { Loading } from "../../components/Loading.js"; import { HookError } from "../../hooks/useAsyncAsHook.js"; import { ButtonHandler, SelectFieldHandler } from "../../mui/handlers.js"; import { compose, StateViewMap } from "../../utils/index.js"; import * as wxApi from "../../wxApi.js"; import { useComponentState } from "./state.js"; -import { ComparingView, LoadingUriView, NoExchangesView, ReadyView } from "./views.js"; - - +import { + ComparingView, + LoadingUriView, + NoExchangesView, + ReadyView, +} from "./views.js"; export interface Props { currency?: string; @@ -39,7 +48,6 @@ export type State = | State.NoExchanges; export namespace State { - export interface Loading { status: "loading"; error: undefined; @@ -75,13 +83,16 @@ export namespace State { } } - const viewMapping: StateViewMap<State> = { loading: Loading, "loading-uri": LoadingUriView, - "comparing": ComparingView, + comparing: ComparingView, "no-exchanges": NoExchangesView, - "ready": ReadyView, + ready: ReadyView, }; -export const ExchangeSelectionPage = compose("ExchangeSelectionPage", (p: Props) => useComponentState(p, wxApi), viewMapping) +export const ExchangeSelectionPage = compose( + "ExchangeSelectionPage", + (p: Props) => useComponentState(p, wxApi), + viewMapping, +); diff --git a/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/state.ts b/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/state.ts index 352952da0..8c0c21486 100644 --- a/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/state.ts +++ b/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/state.ts @@ -14,7 +14,6 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ - import { FeeDescription, OperationMap } from "@gnu-taler/taler-util"; import { createDenominationPairTimeline } from "@gnu-taler/taler-wallet-core"; import { useState } from "preact/hooks"; @@ -26,26 +25,32 @@ export function useComponentState( { onCancel, onSelection, currency }: Props, api: typeof wxApi, ): State { - const initialValue = 0 + const initialValue = 0; const [value, setValue] = useState(String(initialValue)); const hook = useAsyncAsHook(async () => { - const { exchanges } = await api.listExchanges() - - const selectedIdx = parseInt(value, 10) - const selectedExchange = exchanges.length == 0 ? undefined : exchanges[selectedIdx] - const selected = !selectedExchange ? undefined : await api.getExchangeDetailedInfo(selectedExchange.exchangeBaseUrl) - - const initialExchange = selectedIdx === initialValue ? undefined : exchanges[initialValue] - const original = !initialExchange ? undefined : await api.getExchangeDetailedInfo(initialExchange.exchangeBaseUrl) - return { exchanges, selected, original } + const { exchanges } = await api.listExchanges(); + + const selectedIdx = parseInt(value, 10); + const selectedExchange = + exchanges.length == 0 ? undefined : exchanges[selectedIdx]; + const selected = !selectedExchange + ? undefined + : await api.getExchangeDetailedInfo(selectedExchange.exchangeBaseUrl); + + const initialExchange = + selectedIdx === initialValue ? undefined : exchanges[initialValue]; + const original = !initialExchange + ? undefined + : await api.getExchangeDetailedInfo(initialExchange.exchangeBaseUrl); + return { exchanges, selected, original }; }); if (!hook) { return { status: "loading", error: undefined, - } + }; } if (hook.hasError) { return { @@ -60,11 +65,14 @@ export function useComponentState( //!selected <=> exchanges.length === 0 return { status: "no-exchanges", - error: undefined - } + error: undefined, + }; } - const exchangeMap = exchanges.reduce((prev, cur, idx) => ({ ...prev, [cur.exchangeBaseUrl]: String(idx) }), {} as Record<string, string>) + const exchangeMap = exchanges.reduce( + (prev, cur, idx) => ({ ...prev, [cur.exchangeBaseUrl]: String(idx) }), + {} as Record<string, string>, + ); if (!original) { // !original <=> selected == original @@ -74,24 +82,36 @@ export function useComponentState( list: exchangeMap, value: value, onChange: async (v) => { - setValue(v) - } + setValue(v); + }, }, error: undefined, onClose: { - onClick: onCancel + onClick: onCancel, }, selected, - timeline: selected.feesDescription - } + timeline: selected.feesDescription, + }; } const pairTimeline: OperationMap<FeeDescription[]> = { - deposit: createDenominationPairTimeline(selected.feesDescription.deposit, original.feesDescription.deposit), - refresh: createDenominationPairTimeline(selected.feesDescription.refresh, original.feesDescription.refresh), - refund: createDenominationPairTimeline(selected.feesDescription.refund, original.feesDescription.refund), - withdraw: createDenominationPairTimeline(selected.feesDescription.withdraw, original.feesDescription.withdraw), - } + deposit: createDenominationPairTimeline( + selected.feesDescription.deposit, + original.feesDescription.deposit, + ), + refresh: createDenominationPairTimeline( + selected.feesDescription.refresh, + original.feesDescription.refresh, + ), + refund: createDenominationPairTimeline( + selected.feesDescription.refund, + original.feesDescription.refund, + ), + withdraw: createDenominationPairTimeline( + selected.feesDescription.withdraw, + original.feesDescription.withdraw, + ), + }; return { status: "comparing", @@ -99,23 +119,21 @@ export function useComponentState( list: exchangeMap, value: value, onChange: async (v) => { - setValue(v) - } + setValue(v); + }, }, error: undefined, onReset: { onClick: async () => { - setValue(String(initialValue)) - } + setValue(String(initialValue)); + }, }, onSelect: { onClick: async () => { - onSelection(selected.exchangeBaseUrl) - } + onSelection(selected.exchangeBaseUrl); + }, }, selected, pairTimeline, - } - + }; } - diff --git a/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/test.ts b/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/test.ts index 61bc6425f..3c7235851 100644 --- a/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/test.ts +++ b/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/test.ts @@ -19,8 +19,5 @@ * @author Sebastian Javier Marchano (sebasjm) */ -import { - AbsoluteTime, - Amounts, DenominationInfo -} from "@gnu-taler/taler-util"; +import { AbsoluteTime, Amounts, DenominationInfo } from "@gnu-taler/taler-util"; import { expect } from "chai"; |