diff options
author | Sebastian <sebasjm@gmail.com> | 2023-04-05 12:06:24 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-04-05 12:06:24 -0300 |
commit | 8eee38d55936c194637ecead302dde69ec0b9897 (patch) | |
tree | fc87a818fa586152e5f6ad1dfb29e917abe959f7 /packages/taler-wallet-webextension/src/hooks/useSelectedExchange.ts | |
parent | d5c5c7463e5210916822c8030bb59c7b0f3ccc1c (diff) | |
download | wallet-core-8eee38d55936c194637ecead302dde69ec0b9897.tar.xz |
fix #7791
Diffstat (limited to 'packages/taler-wallet-webextension/src/hooks/useSelectedExchange.ts')
-rw-r--r-- | packages/taler-wallet-webextension/src/hooks/useSelectedExchange.ts | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/packages/taler-wallet-webextension/src/hooks/useSelectedExchange.ts b/packages/taler-wallet-webextension/src/hooks/useSelectedExchange.ts index 6ceae2d47..6907a247d 100644 --- a/packages/taler-wallet-webextension/src/hooks/useSelectedExchange.ts +++ b/packages/taler-wallet-webextension/src/hooks/useSelectedExchange.ts @@ -19,13 +19,14 @@ import { useState } from "preact/hooks"; import { useAlertContext } from "../context/alert.js"; import { ButtonHandler } from "../mui/handlers.js"; -type State = State.Ready | State.NoExchange | State.Selecting; +type State = State.Ready | State.NoExchangeFound | State.Selecting; export namespace State { - export interface NoExchange { - status: "no-exchange"; + export interface NoExchangeFound { + status: "no-exchange-found"; error: undefined; - currency: string | undefined; + currency: string; + defaultExchange: string | undefined; } export interface Ready { status: "ready"; @@ -39,7 +40,7 @@ export namespace State { onCancel: () => Promise<void>; list: ExchangeListItem[]; currency: string; - currentExchange: string; + initialValue: string; } } @@ -64,31 +65,35 @@ export function useSelectedExchange({ if (!list.length) { return { - status: "no-exchange", + status: "no-exchange-found", error: undefined, - currency: undefined, + currency, + defaultExchange, }; } - const listCurrency = list.filter((e) => e.currency === currency); - if (!listCurrency.length) { + const exchangesWithThisCurrency = list.filter((e) => e.currency === currency); + if (!exchangesWithThisCurrency.length) { // there should be at least one exchange for this currency return { - status: "no-exchange", + status: "no-exchange-found", error: undefined, currency, + defaultExchange, }; } if (isSelecting) { const currentExchange = - selectedExchange ?? defaultExchange ?? listCurrency[0].exchangeBaseUrl; + selectedExchange ?? + defaultExchange ?? + exchangesWithThisCurrency[0].exchangeBaseUrl; return { status: "selecting-exchange", error: undefined, - list: listCurrency, + list: exchangesWithThisCurrency, currency, - currentExchange: currentExchange, + initialValue: currentExchange, onSelection: async (exchangeBaseUrl: string) => { setIsSelecting(false); setSelectedExchange(exchangeBaseUrl); @@ -131,6 +136,6 @@ export function useSelectedExchange({ doSelect: { onClick: pushAlertOnError(async () => setIsSelecting(true)), }, - selected: listCurrency[0], + selected: exchangesWithThisCurrency[0], }; } |