diff options
Diffstat (limited to 'packages/taler-wallet-webextension/src/wallet/ExchangeSelection')
4 files changed, 49 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 299c236c4..d711f1ecc 100644 --- a/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/index.ts +++ b/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/index.ts @@ -38,7 +38,7 @@ import { export interface Props { list: ExchangeListItem[]; - currentExchange: string; + initialValue: string; onCancel: () => Promise<void>; onSelection: (exchange: string) => Promise<void>; } @@ -50,7 +50,7 @@ export type State = | State.Comparing | State.ShowingTos | State.ShowingPrivacy - | SelectExchangeState.NoExchange; + | SelectExchangeState.NoExchangeFound; export namespace State { export interface Loading { @@ -102,7 +102,7 @@ const viewMapping: StateViewMap<State> = { loading: Loading, error: ErrorAlertView, comparing: ComparingView, - "no-exchange": NoExchangesView, + "no-exchange-found": NoExchangesView, "showing-tos": TosContentView, "showing-privacy": PrivacyContentView, ready: ReadyView, diff --git a/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/state.ts b/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/state.ts index 7ad11e67c..ff3800ee8 100644 --- a/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/state.ts +++ b/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/state.ts @@ -30,37 +30,37 @@ export function useComponentState({ onCancel, onSelection, list: exchanges, - currentExchange, + initialValue, }: Props): State { const api = useBackendContext(); const { pushAlertOnError } = useAlertContext(); const { i18n } = useTranslationContext(); - const initialValue = exchanges.findIndex( - (e) => e.exchangeBaseUrl === currentExchange, + const initialValueIdx = exchanges.findIndex( + (e) => e.exchangeBaseUrl === initialValue, ); - if (initialValue === -1) { + if (initialValueIdx === -1) { throw Error( - `wrong usage of ExchangeSelection component, currentExchange '${currentExchange}' is not in the list of exchanges`, + `wrong usage of ExchangeSelection component, currentExchange '${initialValue}' is not in the list of exchanges`, ); } - const [value, setValue] = useState(String(initialValue)); + const [value, setValue] = useState(String(initialValueIdx)); const selectedIdx = parseInt(value, 10); - const selectedExchange = - exchanges.length == 0 ? undefined : exchanges[selectedIdx]; + const selectedExchange = exchanges[selectedIdx]; - const comparingExchanges = selectedIdx !== initialValue; + const comparingExchanges = selectedIdx !== initialValueIdx; const initialExchange = comparingExchanges - ? exchanges[initialValue] + ? exchanges[initialValueIdx] : undefined; const hook = useAsyncAsHook(async () => { - const selected = !selectedExchange - ? undefined - : await api.wallet.call(WalletApiOperation.GetExchangeDetailedInfo, { - exchangeBaseUrl: selectedExchange.exchangeBaseUrl, - }); + const selected = await api.wallet.call( + WalletApiOperation.GetExchangeDetailedInfo, + { + exchangeBaseUrl: selectedExchange.exchangeBaseUrl, + }, + ); const original = !initialExchange ? undefined @@ -70,7 +70,7 @@ export function useComponentState({ return { exchanges, - selected: selected?.exchange, + selected: selected.exchange, original: original?.exchange, }; }, [selectedExchange, initialExchange]); @@ -98,14 +98,6 @@ export function useComponentState({ const { selected, original } = hook.response; - if (selectedExchange === undefined || !selected) { - return { - status: "no-exchange", - error: undefined, - currency: undefined, - }; - } - const exchangeMap = exchanges.reduce( (prev, cur, idx) => ({ ...prev, [String(idx)]: cur.exchangeBaseUrl }), {} as Record<string, string>, diff --git a/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/stories.tsx b/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/stories.tsx index a65f85c6a..741875ad5 100644 --- a/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/stories.tsx +++ b/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/stories.tsx @@ -20,12 +20,17 @@ */ import { tests } from "@gnu-taler/web-util/lib/index.browser"; -import { ComparingView, ReadyView } from "./views.js"; +import { ComparingView, ReadyView, NoExchangesView } from "./views.js"; export default { title: "select exchange", }; +export const NoExchangeFound = tests.createExample(NoExchangesView, { + currency: "USD", + defaultExchange: "https://exchange.taler.ar", +}); + export const Bitcoin1 = tests.createExample(ReadyView, { exchanges: { list: { "0": "https://exchange.taler.ar" }, diff --git a/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/views.tsx b/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/views.tsx index fd5c0cfe3..242a8e823 100644 --- a/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/views.tsx +++ b/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/views.tsx @@ -141,28 +141,33 @@ export function TosContentView({ } export function NoExchangesView({ + defaultExchange, currency, -}: SelectExchangeState.NoExchange): VNode { +}: SelectExchangeState.NoExchangeFound): VNode { const { i18n } = useTranslationContext(); - if (!currency) { - return ( - <AlertView - alert={{ - type: "warning", - message: i18n.str`Could not find any exchange `, - description: i18n.str`You are trying to withdraw coins but there is no exchange and the bank didn't suggested one.`, - }} - /> - ); - } return ( - <AlertView - alert={{ - type: "warning", - message: i18n.str`Could not find any exchange `, - description: i18n.str`You are trying to withdraw coins for the currency ${currency} but there is no exchange registered in this wallet and the bank didn't suggested one.`, - }} - /> + <Fragment> + <p> + <AlertView + alert={{ + type: "error", + message: i18n.str`There is no exchange available for currency ${currency}`, + description: i18n.str`You can add more exchanges from the settings.`, + cause: undefined, + context: undefined, + }} + /> + </p> + {defaultExchange && ( + <AlertView + alert={{ + type: "warning", + message: i18n.str`Exchange ${defaultExchange} is not available`, + description: i18n.str`Exchange status can view accessed from the settings.`, + }} + /> + )} + </Fragment> ); } |