summaryrefslogtreecommitdiff
path: root/packages/taler-wallet-webextension/src/cta
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2022-09-20 20:26:41 -0300
committerSebastian <sebasjm@gmail.com>2022-09-20 20:26:47 -0300
commit859991a40c4a7757d874f9ae6e6db7b76145a3c3 (patch)
tree2d029521929fa63f731e0b62bd3342f6f70ad2f8 /packages/taler-wallet-webextension/src/cta
parent7adaeff0a57bc2d0633dd6a2c750a91facc03c4d (diff)
downloadwallet-core-859991a40c4a7757d874f9ae6e6db7b76145a3c3.tar.gz
wallet-core-859991a40c4a7757d874f9ae6e6db7b76145a3c3.tar.bz2
wallet-core-859991a40c4a7757d874f9ae6e6db7b76145a3c3.zip
exchange selection for invoices and some fixes
Diffstat (limited to 'packages/taler-wallet-webextension/src/cta')
-rw-r--r--packages/taler-wallet-webextension/src/cta/InvoiceCreate/index.ts20
-rw-r--r--packages/taler-wallet-webextension/src/cta/InvoiceCreate/state.ts120
-rw-r--r--packages/taler-wallet-webextension/src/cta/Withdraw/index.ts22
-rw-r--r--packages/taler-wallet-webextension/src/cta/Withdraw/state.ts16
-rw-r--r--packages/taler-wallet-webextension/src/cta/Withdraw/views.tsx10
5 files changed, 92 insertions, 96 deletions
diff --git a/packages/taler-wallet-webextension/src/cta/InvoiceCreate/index.ts b/packages/taler-wallet-webextension/src/cta/InvoiceCreate/index.ts
index 2bee51669..61f286d1f 100644
--- a/packages/taler-wallet-webextension/src/cta/InvoiceCreate/index.ts
+++ b/packages/taler-wallet-webextension/src/cta/InvoiceCreate/index.ts
@@ -14,14 +14,19 @@
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
+import { AmountJson, TalerErrorDetail } from "@gnu-taler/taler-util";
import { Loading } from "../../components/Loading.js";
import { HookError } from "../../hooks/useAsyncAsHook.js";
+import {
+ State as SelectExchangeState
+} from "../../hooks/useSelectedExchange.js";
+import { ButtonHandler, TextFieldHandler } from "../../mui/handlers.js";
import { compose, StateViewMap } from "../../utils/index.js";
-import { LoadingUriView, ReadyView } from "./views.js";
+import { ExchangeSelectionPage } from "../../wallet/ExchangeSelection/index.js";
+import { NoExchangesView } from "../../wallet/ExchangeSelection/views.js";
import * as wxApi from "../../wxApi.js";
import { useComponentState } from "./state.js";
-import { AmountJson, TalerErrorDetail } from "@gnu-taler/taler-util";
-import { ButtonHandler, TextFieldHandler } from "../../mui/handlers.js";
+import { LoadingUriView, ReadyView } from "./views.js";
export interface Props {
amount: string;
@@ -29,7 +34,12 @@ export interface Props {
onSuccess: (tx: string) => Promise<void>;
}
-export type State = State.Loading | State.LoadingUriError | State.Ready;
+export type State = State.Loading
+ | State.LoadingUriError
+ | State.Ready
+ | SelectExchangeState.Selecting
+ | SelectExchangeState.NoExchange
+ ;
export namespace State {
export interface Loading {
@@ -63,6 +73,8 @@ export namespace State {
const viewMapping: StateViewMap<State> = {
loading: Loading,
"loading-uri": LoadingUriView,
+ "no-exchange": NoExchangesView,
+ "selecting-exchange": ExchangeSelectionPage,
ready: ReadyView,
};
diff --git a/packages/taler-wallet-webextension/src/cta/InvoiceCreate/state.ts b/packages/taler-wallet-webextension/src/cta/InvoiceCreate/state.ts
index 9b67b4414..4f75e982d 100644
--- a/packages/taler-wallet-webextension/src/cta/InvoiceCreate/state.ts
+++ b/packages/taler-wallet-webextension/src/cta/InvoiceCreate/state.ts
@@ -14,26 +14,24 @@
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
+/* eslint-disable react-hooks/rules-of-hooks */
import { Amounts, TalerErrorDetail } from "@gnu-taler/taler-util";
import { TalerError } from "@gnu-taler/taler-wallet-core";
import { useState } from "preact/hooks";
import { useAsyncAsHook } from "../../hooks/useAsyncAsHook.js";
+import { useSelectedExchange } from "../../hooks/useSelectedExchange.js";
import * as wxApi from "../../wxApi.js";
import { Props, State } from "./index.js";
+type RecursiveState<S extends object> = S | (() => RecursiveState<S>)
+
export function useComponentState(
{ amount: amountStr, onClose, onSuccess }: Props,
api: typeof wxApi,
-): State {
+): RecursiveState<State> {
const amount = Amounts.parseOrThrow(amountStr);
- const [subject, setSubject] = useState("");
-
const hook = useAsyncAsHook(api.listExchanges);
- const [exchangeIdx, setExchangeIdx] = useState("0");
- const [operationError, setOperationError] = useState<
- TalerErrorDetail | undefined
- >(undefined);
if (!hook) {
return {
@@ -48,56 +46,68 @@ export function useComponentState(
};
}
- const exchanges = hook.response.exchanges.filter(
- (e) => e.currency === amount.currency,
- );
- const exchangeMap = exchanges.reduce(
- (prev, cur, idx) => ({ ...prev, [String(idx)]: cur.exchangeBaseUrl }),
- {} as Record<string, string>,
- );
- const selected = exchanges[Number(exchangeIdx)];
-
- async function accept(): Promise<void> {
- try {
- const resp = await api.initiatePeerPullPayment({
- amount: Amounts.stringify(amount),
- exchangeBaseUrl: selected.exchangeBaseUrl,
- partialContractTerms: {
- summary: subject,
- },
- });
-
- onSuccess(resp.transactionId);
- } catch (e) {
- if (e instanceof TalerError) {
- setOperationError(e.errorDetail);
+ const exchangeList = hook.response.exchanges
+
+ return () => {
+ const [subject, setSubject] = useState("");
+
+ const [operationError, setOperationError] = useState<
+ TalerErrorDetail | undefined
+ >(undefined);
+
+
+ const selectedExchange = useSelectedExchange({ currency: amount.currency, defaultExchange: undefined, list: exchangeList })
+
+ if (selectedExchange.status !== 'ready') {
+ return selectedExchange
+ }
+
+ const exchange = selectedExchange.selected
+
+ async function accept(): Promise<void> {
+ try {
+ const resp = await api.initiatePeerPullPayment({
+ amount: Amounts.stringify(amount),
+ exchangeBaseUrl: exchange.exchangeBaseUrl,
+ partialContractTerms: {
+ summary: subject,
+ },
+ });
+
+ onSuccess(resp.transactionId);
+ } catch (e) {
+ if (e instanceof TalerError) {
+ setOperationError(e.errorDetail);
+ }
+ console.error(e);
+ throw Error("error trying to accept");
}
- console.error(e);
- throw Error("error trying to accept");
}
+
+ return {
+ status: "ready",
+ subject: {
+ error: !subject ? "cant be empty" : undefined,
+ value: subject,
+ onInput: async (e) => setSubject(e),
+ },
+ doSelectExchange: selectedExchange.doSelect,
+ invalid: !subject || Amounts.isZero(amount),
+ exchangeUrl: exchange.exchangeBaseUrl,
+ create: {
+ onClick: accept,
+ },
+ cancel: {
+ onClick: onClose,
+ },
+ chosenAmount: amount,
+ toBeReceived: amount,
+ error: undefined,
+ operationError,
+ };
}
- return {
- status: "ready",
- subject: {
- error: !subject ? "cant be empty" : undefined,
- value: subject,
- onInput: async (e) => setSubject(e),
- },
- doSelectExchange: {
- //FIX
- },
- invalid: !subject || Amounts.isZero(amount),
- exchangeUrl: selected.exchangeBaseUrl,
- create: {
- onClick: accept,
- },
- cancel: {
- onClick: onClose,
- },
- chosenAmount: amount,
- toBeReceived: amount,
- error: undefined,
- operationError,
- };
+
+
+
}
diff --git a/packages/taler-wallet-webextension/src/cta/Withdraw/index.ts b/packages/taler-wallet-webextension/src/cta/Withdraw/index.ts
index d38c27a2f..9de9c693a 100644
--- a/packages/taler-wallet-webextension/src/cta/Withdraw/index.ts
+++ b/packages/taler-wallet-webextension/src/cta/Withdraw/index.ts
@@ -17,25 +17,25 @@
import { AmountJson } from "@gnu-taler/taler-util";
import { Loading } from "../../components/Loading.js";
import { HookError } from "../../hooks/useAsyncAsHook.js";
+import {
+ State as SelectExchangeState
+} from "../../hooks/useSelectedExchange.js";
import { ButtonHandler, SelectFieldHandler } from "../../mui/handlers.js";
import { compose, StateViewMap } from "../../utils/index.js";
import * as wxApi from "../../wxApi.js";
import { Props as TermsOfServiceSectionProps } from "../TermsOfServiceSection.js";
import {
useComponentStateFromParams,
- useComponentStateFromURI,
+ useComponentStateFromURI
} from "./state.js";
-import {
- State as SelectExchangeState
-} from "../../hooks/useSelectedExchange.js";
+import { ExchangeSelectionPage } from "../../wallet/ExchangeSelection/index.js";
import {
- LoadingExchangeView,
LoadingInfoView,
LoadingUriView,
- SuccessView,
+ SuccessView
} from "./views.js";
-import { ExchangeSelectionPage } from "../../wallet/ExchangeSelection/index.js";
+import { NoExchangesView } from "../../wallet/ExchangeSelection/views.js";
export interface PropsFromURI {
talerWithdrawUri: string | undefined;
@@ -52,8 +52,8 @@ export interface PropsFromParams {
export type State =
| State.Loading
| State.LoadingUriError
- | State.LoadingExchangeError
| State.LoadingInfoError
+ | SelectExchangeState.NoExchange
| SelectExchangeState.Selecting
| State.Success;
@@ -66,10 +66,6 @@ export namespace State {
status: "loading-error";
error: HookError;
}
- export interface LoadingExchangeError {
- status: "no-exchange";
- error: undefined,
- }
export interface LoadingInfoError {
status: "loading-info";
error: HookError;
@@ -100,8 +96,8 @@ export namespace State {
const viewMapping: StateViewMap<State> = {
loading: Loading,
"loading-error": LoadingUriView,
- "no-exchange": LoadingExchangeView,
"loading-info": LoadingInfoView,
+ "no-exchange": NoExchangesView,
"selecting-exchange": ExchangeSelectionPage,
success: SuccessView,
};
diff --git a/packages/taler-wallet-webextension/src/cta/Withdraw/state.ts b/packages/taler-wallet-webextension/src/cta/Withdraw/state.ts
index 2e68d056e..5b5c11182 100644
--- a/packages/taler-wallet-webextension/src/cta/Withdraw/state.ts
+++ b/packages/taler-wallet-webextension/src/cta/Withdraw/state.ts
@@ -36,8 +36,6 @@ export function useComponentStateFromParams(
return { amount: Amounts.parseOrThrow(amount), exchanges };
});
- console.log("uri info", uriInfoHook)
-
if (!uriInfoHook) return { status: "loading", error: undefined };
if (uriInfoHook.hasError) {
@@ -80,7 +78,6 @@ export function useComponentStateFromURI(
return { talerWithdrawUri, amount: Amounts.parseOrThrow(amount), thisExchange: defaultExchangeBaseUrl, exchanges };
});
- console.log("uri info", uriInfoHook)
if (!uriInfoHook) return { status: "loading", error: undefined };
if (uriInfoHook.hasError) {
@@ -111,20 +108,11 @@ type ManualOrManagedWithdrawFunction = (exchange: string, ageRestricted: number
function exchangeSelectionState(doWithdraw: ManualOrManagedWithdrawFunction, cancel: () => Promise<void>, onSuccess: (txid: string) => Promise<void>, talerWithdrawUri: string | undefined, chosenAmount: AmountJson, exchangeList: ExchangeListItem[], defaultExchange: string | undefined, api: typeof wxApi,): RecursiveState<State> {
- //FIXME: use substates here
const selectedExchange = useSelectedExchange({ currency: chosenAmount.currency, defaultExchange, list: exchangeList })
- if (selectedExchange.status === 'no-exchange') {
- return {
- status: "no-exchange",
- error: undefined,
- }
- }
-
- if (selectedExchange.status === 'selecting-exchange') {
+ if (selectedExchange.status !== 'ready') {
return selectedExchange
}
- console.log("exchange selected", selectedExchange.selected)
return () => {
@@ -142,7 +130,7 @@ function exchangeSelectionState(doWithdraw: ManualOrManagedWithdrawFunction, can
return { state };
}, []);
- console.log("terms", terms)
+
/**
* With the exchange and amount, ask the wallet the information
* about the withdrawal
diff --git a/packages/taler-wallet-webextension/src/cta/Withdraw/views.tsx b/packages/taler-wallet-webextension/src/cta/Withdraw/views.tsx
index 82d6090e5..1e8284739 100644
--- a/packages/taler-wallet-webextension/src/cta/Withdraw/views.tsx
+++ b/packages/taler-wallet-webextension/src/cta/Withdraw/views.tsx
@@ -53,16 +53,6 @@ export function LoadingUriView({ error }: State.LoadingUriError): VNode {
);
}
-export function LoadingExchangeView(p: State.LoadingExchangeError): VNode {
- const { i18n } = useTranslationContext();
-
- return (
- <ErrorMessage
- title={<i18n.Translate>Could not get a default exchange, please check configuration</i18n.Translate>}
- />
- );
-}
-
export function LoadingInfoView({ error }: State.LoadingInfoError): VNode {
const { i18n } = useTranslationContext();