wallet-core/packages/taler-wallet-webextension/src/cta/Withdraw/index.ts

106 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-07-21 15:36:15 +02:00
/*
This file is part of GNU Taler
(C) 2022 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
import { AmountJson, ExchangeListItem } from "@gnu-taler/taler-util";
2022-07-31 01:55:41 +02:00
import { Loading } from "../../components/Loading.js";
2022-07-21 15:36:15 +02:00
import { HookError } from "../../hooks/useAsyncAsHook.js";
import { State as SelectExchangeState } from "../../hooks/useSelectedExchange.js";
2022-07-21 15:36:15 +02:00
import { ButtonHandler, SelectFieldHandler } from "../../mui/handlers.js";
2022-07-31 01:55:41 +02:00
import { compose, StateViewMap } from "../../utils/index.js";
2022-07-21 15:36:15 +02:00
import {
2022-09-16 19:29:35 +02:00
useComponentStateFromParams,
2022-12-15 21:12:03 +01:00
useComponentStateFromURI,
2022-09-16 19:29:35 +02:00
} from "./state.js";
import { ExchangeSelectionPage } from "../../wallet/ExchangeSelection/index.js";
import { NoExchangesView } from "../../wallet/ExchangeSelection/views.js";
2022-10-25 17:23:08 +02:00
import { LoadingInfoView, LoadingUriView, SuccessView } from "./views.js";
2022-07-21 15:36:15 +02:00
2022-08-29 16:32:07 +02:00
export interface PropsFromURI {
2022-07-21 15:36:15 +02:00
talerWithdrawUri: string | undefined;
2022-08-10 16:50:46 +02:00
cancel: () => Promise<void>;
2022-09-16 21:03:58 +02:00
onSuccess: (txid: string) => Promise<void>;
2022-07-21 15:36:15 +02:00
}
2022-08-29 16:32:07 +02:00
export interface PropsFromParams {
amount: string;
cancel: () => Promise<void>;
2022-09-16 21:03:58 +02:00
onSuccess: (txid: string) => Promise<void>;
2022-08-29 16:32:07 +02:00
}
2022-07-21 15:36:15 +02:00
export type State =
2022-07-31 01:55:41 +02:00
| State.Loading
| State.LoadingUriError
2022-07-21 15:36:15 +02:00
| State.LoadingInfoError
| SelectExchangeState.NoExchange
| SelectExchangeState.Selecting
2022-09-16 21:03:58 +02:00
| State.Success;
2022-07-21 15:36:15 +02:00
export namespace State {
2022-07-31 01:55:41 +02:00
export interface Loading {
status: "loading";
error: undefined;
}
export interface LoadingUriError {
2022-10-17 15:18:17 +02:00
status: "uri-error";
2022-07-31 01:55:41 +02:00
error: HookError;
2022-07-21 15:36:15 +02:00
}
export interface LoadingInfoError {
2022-10-17 15:18:17 +02:00
status: "amount-error";
2022-07-31 01:55:41 +02:00
error: HookError;
2022-07-21 15:36:15 +02:00
}
export type Success = {
status: "success";
2022-07-31 01:55:41 +02:00
error: undefined;
2022-07-21 15:36:15 +02:00
currentExchange: ExchangeListItem;
2022-07-21 15:36:15 +02:00
chosenAmount: AmountJson;
withdrawalFee: AmountJson;
toBeReceived: AmountJson;
doWithdrawal: ButtonHandler;
doSelectExchange: ButtonHandler;
2022-07-21 15:36:15 +02:00
2022-08-10 16:50:46 +02:00
ageRestriction?: SelectFieldHandler;
talerWithdrawUri?: string;
2022-08-10 16:50:46 +02:00
cancel: () => Promise<void>;
onTosUpdate: () => void;
2022-07-21 15:36:15 +02:00
};
}
const viewMapping: StateViewMap<State> = {
2022-07-31 01:55:41 +02:00
loading: Loading,
2022-10-17 15:18:17 +02:00
"uri-error": LoadingUriView,
"amount-error": LoadingInfoView,
"no-exchange": NoExchangesView,
"selecting-exchange": ExchangeSelectionPage,
2022-07-21 15:36:15 +02:00
success: SuccessView,
};
2022-09-16 19:29:35 +02:00
export const WithdrawPageFromURI = compose(
"WithdrawPageFromURI",
(p: PropsFromURI) => useComponentStateFromURI(p),
2022-09-16 19:29:35 +02:00
viewMapping,
);
export const WithdrawPageFromParams = compose(
"WithdrawPageFromParams",
(p: PropsFromParams) => useComponentStateFromParams(p),
2022-09-16 19:29:35 +02:00
viewMapping,
);