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

117 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 } 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 { ButtonHandler, SelectFieldHandler } from "../../mui/handlers.js";
2022-07-31 01:55:41 +02:00
import { compose, StateViewMap } from "../../utils/index.js";
import * as wxApi from "../../wxApi.js";
2022-09-16 19:29:35 +02:00
import { Props as TermsOfServiceSectionProps } from "../TermsOfServiceSection.js";
2022-07-21 15:36:15 +02:00
import {
2022-09-16 19:29:35 +02:00
useComponentStateFromParams,
useComponentStateFromURI,
} from "./state.js";
import {
CompletedView,
LoadingExchangeView,
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-07-21 15:36:15 +02:00
}
2022-08-29 16:32:07 +02:00
export interface PropsFromParams {
amount: string;
cancel: () => Promise<void>;
}
2022-07-21 15:36:15 +02:00
export type State =
2022-07-31 01:55:41 +02:00
| State.Loading
| State.LoadingUriError
| State.LoadingExchangeError
2022-07-21 15:36:15 +02:00
| State.LoadingInfoError
| State.Success
| State.Completed;
export namespace State {
2022-07-31 01:55:41 +02:00
export interface Loading {
status: "loading";
error: undefined;
}
export interface LoadingUriError {
2022-07-21 15:36:15 +02:00
status: "loading-uri";
2022-07-31 01:55:41 +02:00
error: HookError;
2022-07-21 15:36:15 +02:00
}
2022-07-31 01:55:41 +02:00
export interface LoadingExchangeError {
2022-07-21 15:36:15 +02:00
status: "loading-exchange";
2022-07-31 01:55:41 +02:00
error: HookError;
2022-07-21 15:36:15 +02:00
}
export interface LoadingInfoError {
status: "loading-info";
2022-07-31 01:55:41 +02:00
error: HookError;
2022-07-21 15:36:15 +02:00
}
export type Completed = {
status: "completed";
2022-07-31 01:55:41 +02:00
error: undefined;
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
2022-08-10 16:50:46 +02:00
exchangeUrl: string;
2022-07-21 15:36:15 +02:00
chosenAmount: AmountJson;
withdrawalFee: AmountJson;
toBeReceived: AmountJson;
doWithdrawal: ButtonHandler;
tosProps?: TermsOfServiceSectionProps;
mustAcceptFirst: boolean;
2022-08-10 16:50:46 +02:00
ageRestriction?: SelectFieldHandler;
talerWithdrawUri?: string;
2022-08-10 16:50:46 +02:00
cancel: () => Promise<void>;
2022-07-21 15:36:15 +02:00
};
}
const viewMapping: StateViewMap<State> = {
2022-07-31 01:55:41 +02:00
loading: Loading,
2022-07-21 15:36:15 +02:00
"loading-uri": LoadingUriView,
"loading-exchange": LoadingExchangeView,
"loading-info": LoadingInfoView,
completed: CompletedView,
success: SuccessView,
};
2022-09-16 19:29:35 +02:00
export const WithdrawPageFromURI = compose(
"WithdrawPageFromURI",
(p: PropsFromURI) => useComponentStateFromURI(p, wxApi),
viewMapping,
);
export const WithdrawPageFromParams = compose(
"WithdrawPageFromParams",
(p: PropsFromParams) => useComponentStateFromParams(p, wxApi),
viewMapping,
);