diff options
author | Sebastian <sebasjm@gmail.com> | 2022-10-14 11:40:38 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2022-10-14 11:41:53 -0300 |
commit | da9ec5eb16298d8ca5690800eca1c15f5a6cfaa5 (patch) | |
tree | 56b637054c94462cada2a067cfb7cce46fefe651 /packages/taler-wallet-webextension/src/hooks/useSelectedExchange.ts | |
parent | ca8da4ed380b308abdc894145c7e1a102bfd6cf0 (diff) | |
download | wallet-core-da9ec5eb16298d8ca5690800eca1c15f5a6cfaa5.tar.xz |
refactored terms of service to remove duplicated code
prettfied some sources
Diffstat (limited to 'packages/taler-wallet-webextension/src/hooks/useSelectedExchange.ts')
-rw-r--r-- | packages/taler-wallet-webextension/src/hooks/useSelectedExchange.ts | 92 |
1 files changed, 49 insertions, 43 deletions
diff --git a/packages/taler-wallet-webextension/src/hooks/useSelectedExchange.ts b/packages/taler-wallet-webextension/src/hooks/useSelectedExchange.ts index 7219c30d2..c04dcce84 100644 --- a/packages/taler-wallet-webextension/src/hooks/useSelectedExchange.ts +++ b/packages/taler-wallet-webextension/src/hooks/useSelectedExchange.ts @@ -22,21 +22,21 @@ type State = State.Ready | State.NoExchange | State.Selecting; export namespace State { export interface NoExchange { - status: "no-exchange" + status: "no-exchange"; error: undefined; currency: string | undefined; } export interface Ready { - status: "ready", - doSelect: ButtonHandler, + status: "ready"; + doSelect: ButtonHandler; selected: ExchangeListItem; } export interface Selecting { - status: "selecting-exchange", - error: undefined, + status: "selecting-exchange"; + error: undefined; onSelection: (url: string) => Promise<void>; onCancel: () => Promise<void>; - list: ExchangeListItem[], + list: ExchangeListItem[]; currency: string; currentExchange: string; } @@ -45,38 +45,42 @@ export namespace State { interface Props { currency: string; //there is a preference for the default at the initial state - defaultExchange?: string, + defaultExchange?: string; //list of exchanges - list: ExchangeListItem[], + list: ExchangeListItem[]; } - - -export function useSelectedExchange({ currency, defaultExchange, list }: Props): State { +export function useSelectedExchange({ + currency, + defaultExchange, + list, +}: Props): State { const [isSelecting, setIsSelecting] = useState(false); - const [selectedExchange, setSelectedExchange] = useState<string | undefined>(undefined); + const [selectedExchange, setSelectedExchange] = useState<string | undefined>( + undefined, + ); if (!list.length) { return { status: "no-exchange", error: undefined, currency: undefined, - } + }; } - const listCurrency = list.filter((e) => e.currency === currency) + const listCurrency = list.filter((e) => e.currency === currency); if (!listCurrency.length) { // there should be at least one exchange for this currency return { status: "no-exchange", error: undefined, currency, - } + }; } - if (isSelecting) { - const currentExchange = selectedExchange ?? defaultExchange ?? listCurrency[0].exchangeBaseUrl; + const currentExchange = + selectedExchange ?? defaultExchange ?? listCurrency[0].exchangeBaseUrl; return { status: "selecting-exchange", error: undefined, @@ -85,44 +89,46 @@ export function useSelectedExchange({ currency, defaultExchange, list }: Props): currentExchange: currentExchange, onSelection: async (exchangeBaseUrl: string) => { setIsSelecting(false); - setSelectedExchange(exchangeBaseUrl) + setSelectedExchange(exchangeBaseUrl); }, onCancel: async () => { setIsSelecting(false); - } - } + }, + }; } { - const found = !selectedExchange ? undefined : list.find( - (e) => e.exchangeBaseUrl === selectedExchange, - ) - if (found) return { - status: "ready", - doSelect: { - onClick: async () => setIsSelecting(true) - }, - selected: found - }; + const found = !selectedExchange + ? undefined + : list.find((e) => e.exchangeBaseUrl === selectedExchange); + if (found) + return { + status: "ready", + doSelect: { + onClick: async () => setIsSelecting(true), + }, + selected: found, + }; } { - const found = !defaultExchange ? undefined : list.find( - (e) => e.exchangeBaseUrl === defaultExchange, - ) - if (found) return { - status: "ready", - doSelect: { - onClick: async () => setIsSelecting(true) - }, - selected: found - }; + const found = !defaultExchange + ? undefined + : list.find((e) => e.exchangeBaseUrl === defaultExchange); + if (found) + return { + status: "ready", + doSelect: { + onClick: async () => setIsSelecting(true), + }, + selected: found, + }; } return { status: "ready", doSelect: { - onClick: async () => setIsSelecting(true) + onClick: async () => setIsSelecting(true), }, - selected: listCurrency[0] - } + selected: listCurrency[0], + }; } |