wallet-core/packages/taler-wallet-webextension/src/wallet/DestinationSelection.tsx

564 lines
14 KiB
TypeScript
Raw Normal View History

2022-08-14 15:10:37 +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/>
*/
2022-08-16 02:18:39 +02:00
import { Amounts } from "@gnu-taler/taler-util";
2022-10-25 17:23:08 +02:00
import { WalletApiOperation } from "@gnu-taler/taler-wallet-core";
2022-08-14 15:10:37 +02:00
import { styled } from "@linaria/react";
import { Fragment, h, VNode } from "preact";
2022-08-16 02:18:39 +02:00
import { useState } from "preact/hooks";
2022-08-16 04:01:05 +02:00
import { Loading } from "../components/Loading.js";
import { LoadingError } from "../components/LoadingError.js";
import { SelectList } from "../components/SelectList.js";
2022-08-16 02:18:39 +02:00
import {
2022-08-16 04:01:05 +02:00
Input,
2022-08-16 02:18:39 +02:00
LightText,
2022-08-16 04:01:05 +02:00
LinkPrimary,
2022-08-16 02:18:39 +02:00
SvgIcon,
} from "../components/styled/index.js";
import { useTranslationContext } from "../context/translation.js";
2022-08-16 04:01:05 +02:00
import { useAsyncAsHook } from "../hooks/useAsyncAsHook.js";
2022-08-16 02:18:39 +02:00
import { Button } from "../mui/Button.js";
import { Grid } from "../mui/Grid.js";
2022-08-14 15:10:37 +02:00
import { Paper } from "../mui/Paper.js";
2022-08-16 02:18:39 +02:00
import { TextField } from "../mui/TextField.js";
2022-08-16 04:01:05 +02:00
import { Pages } from "../NavigationBar.js";
2022-08-16 02:18:39 +02:00
import arrowIcon from "../svg/chevron-down.svg";
import bankIcon from "../svg/ri-bank-line.svg";
2022-10-25 17:23:08 +02:00
import { wxApi } from "../wxApi.js";
2022-08-14 15:10:37 +02:00
const Container = styled.div`
display: flex;
flex-direction: column;
& > * {
margin: 8px;
}
`;
interface PropsGet {
2022-08-16 02:18:39 +02:00
amount?: string;
goToWalletManualWithdraw: (amount: string) => void;
2022-08-29 16:32:07 +02:00
goToWalletWalletInvoice: (amount: string) => void;
2022-08-14 15:10:37 +02:00
}
interface PropsSend {
amount?: string;
goToWalletBankDeposit: (amount: string) => void;
goToWalletWalletSend: (amount: string) => void;
}
2022-08-14 15:10:37 +02:00
2022-08-16 02:18:39 +02:00
type Contact = {
icon: string;
name: string;
description: string;
};
const ContactTable = styled.table`
width: 100%;
& > tr > td {
padding: 8px;
& > div:not([data-disabled]):hover {
background-color: lightblue;
}
color: black;
div[data-disabled] > * {
color: gray;
}
}
& > tr:nth-child(2n) {
background: #ebebeb;
}
`;
const MediaExample = styled.div`
text-size-adjust: 100%;
color: inherit;
font-family: inherit;
font-size: inherit;
line-height: inherit;
text-transform: none;
text-align: left;
box-sizing: border-box;
align-items: center;
display: flex;
padding: 8px 8px;
&[data-disabled]:hover {
cursor: inherit;
}
cursor: pointer;
`;
const MediaLeft = styled.div`
text-size-adjust: 100%;
color: inherit;
font-family: inherit;
font-size: inherit;
line-height: inherit;
text-transform: none;
text-align: left;
box-sizing: border-box;
padding-right: 8px;
display: block;
`;
const MediaBody = styled.div`
text-size-adjust: 100%;
font-family: inherit;
text-transform: none;
text-align: left;
box-sizing: border-box;
flex: 1 1;
font-size: 14px;
font-weight: 500;
line-height: 1.42857;
`;
const MediaRight = styled.div`
text-size-adjust: 100%;
color: inherit;
font-family: inherit;
font-size: inherit;
line-height: inherit;
text-transform: none;
text-align: left;
box-sizing: border-box;
padding-left: 8px;
`;
const CircleDiv = styled.div`
box-sizing: border-box;
align-items: center;
background-position: 50%;
background-repeat: no-repeat;
background-size: cover;
border-radius: 50%;
display: flex;
justify-content: center;
margin-left: auto;
margin-right: auto;
overflow: hidden;
text-align: center;
text-decoration: none;
text-transform: uppercase;
transition: background-color 0.15s ease, border-color 0.15s ease,
color 0.15s ease;
font-size: 16px;
background-color: #86a7bd1a;
height: 40px;
line-height: 40px;
width: 40px;
border: none;
`;
2022-08-16 04:01:05 +02:00
export function SelectCurrency({
onChange,
}: {
onChange: (s: string) => void;
}): VNode {
const { i18n } = useTranslationContext();
2022-10-25 17:23:08 +02:00
const hook = useAsyncAsHook(() =>
wxApi.wallet.call(WalletApiOperation.ListExchanges, {}),
);
2022-08-16 04:01:05 +02:00
if (!hook) {
return <Loading />;
}
if (hook.hasError) {
return (
<LoadingError
error={hook}
title={<i18n.Translate>Could not load list of exchange</i18n.Translate>}
/>
);
}
const list: Record<string, string> = {};
hook.response.exchanges.forEach((e) => {
if (e.currency) {
list[e.currency] = e.currency;
}
});
2022-08-16 04:01:05 +02:00
list[""] = "Select a currency";
2022-08-17 21:12:21 +02:00
return <SelectCurrencyView onChange={onChange} list={list} />;
}
export function SelectCurrencyView({
onChange,
list,
}: {
onChange: (s: string) => void;
list: Record<string, string>;
}): VNode {
const { i18n } = useTranslationContext();
2022-08-16 04:01:05 +02:00
return (
<Fragment>
2022-08-17 21:12:21 +02:00
<h2>
<i18n.Translate>
Choose a currency to proceed or add another exchange
</i18n.Translate>
</h2>
2022-08-16 04:01:05 +02:00
2022-08-17 21:12:21 +02:00
<p>
<Input>
<SelectList
label={<i18n.Translate>Known currencies</i18n.Translate>}
list={list}
name="lang"
value={""}
onChange={(v) => onChange(v)}
/>
</Input>
</p>
2022-08-16 04:01:05 +02:00
<div style={{ display: "flex", justifyContent: "space-between" }}>
<div />
<LinkPrimary href={Pages.settingsExchangeAdd({})}>
<i18n.Translate>Add an exchange</i18n.Translate>
</LinkPrimary>
</div>
</Fragment>
);
}
2022-08-16 02:18:39 +02:00
function RowExample({
info,
disabled,
}: {
info: Contact;
disabled?: boolean;
}): VNode {
return (
<MediaExample data-disabled={disabled}>
<MediaLeft>
<CircleDiv>
<SvgIcon
title={info.name}
dangerouslySetInnerHTML={{ __html: info.icon }}
color="currentColor"
/>
</CircleDiv>
</MediaLeft>
<MediaBody>
<span>{info.name}</span>
<LightText>{info.description}</LightText>
</MediaBody>
<MediaRight>
<SvgIcon
title="Select this contact"
dangerouslySetInnerHTML={{ __html: arrowIcon }}
color="currentColor"
transform="rotate(-90deg)"
/>
</MediaRight>
</MediaExample>
);
}
export function DestinationSelectionGetCash({
amount: initialAmount,
goToWalletManualWithdraw,
2022-08-29 16:32:07 +02:00
goToWalletWalletInvoice,
}: PropsGet): VNode {
2022-08-16 02:18:39 +02:00
const parsedInitialAmount = !initialAmount
? undefined
: Amounts.parse(initialAmount);
const parsedInitialAmountValue = !parsedInitialAmount
2022-08-16 04:01:05 +02:00
? "0"
2022-08-16 02:18:39 +02:00
: Amounts.stringifyValue(parsedInitialAmount);
2022-08-16 04:01:05 +02:00
const [currency, setCurrency] = useState(parsedInitialAmount?.currency);
2022-08-16 02:18:39 +02:00
const [amount, setAmount] = useState(parsedInitialAmountValue);
2022-11-04 19:38:58 +01:00
function positiveSetAmount(e: string):void {
const value = Number.parseInt(e, 10);
if (value < 0) return
setAmount(String(value))
}
2022-08-16 02:18:39 +02:00
const { i18n } = useTranslationContext();
const previous1: Contact[] = [];
const previous2: Contact[] = [
{
name: "International Bank",
icon: bankIcon,
description: "account ending with 3454",
},
{
name: "Max",
icon: bankIcon,
description: "account ending with 3454",
},
{
name: "Alex",
icon: bankIcon,
description: "account ending with 3454",
},
];
2022-08-29 16:32:07 +02:00
const previous = previous1;
2022-08-16 02:18:39 +02:00
if (!currency) {
2022-08-16 04:01:05 +02:00
return (
<div>
<SelectCurrency onChange={(c) => setCurrency(c)} />
</div>
);
2022-08-16 02:18:39 +02:00
}
const currencyAndAmount = `${currency}:${amount}`;
const parsedAmount = Amounts.parse(currencyAndAmount);
// const dirty = parsedInitialAmountValue !== amount;
const invalid = !parsedAmount || Amounts.isZero(parsedAmount);
2022-08-14 15:10:37 +02:00
return (
<Container>
2022-08-16 02:18:39 +02:00
<h1>
<i18n.Translate>Specify the amount and the origin</i18n.Translate>
</h1>
2022-08-16 04:01:05 +02:00
<Grid container columns={2} justifyContent="space-between">
<TextField
label="Amount"
type="number"
2022-11-04 19:38:58 +01:00
min="0"
2022-08-16 04:01:05 +02:00
variant="filled"
error={invalid}
required
startAdornment={
<div style={{ padding: "25px 12px 8px 12px" }}>{currency}</div>
}
value={amount}
onChange={(e) => {
setAmount(e);
}}
/>
<Button onClick={async () => setCurrency(undefined)}>
2022-09-13 16:07:39 +02:00
<i18n.Translate>Change currency</i18n.Translate>
2022-08-16 04:01:05 +02:00
</Button>
</Grid>
2022-08-16 02:18:39 +02:00
<Grid container spacing={1} columns={1}>
2022-08-29 16:32:07 +02:00
{previous.length > 0 ? (
2022-08-16 02:18:39 +02:00
<Fragment>
2022-09-13 16:07:39 +02:00
<p>
<i18n.Translate>Use previous origins:</i18n.Translate>
</p>
2022-08-16 02:18:39 +02:00
<Grid item xs={1}>
<Paper style={{ padding: 8 }}>
<ContactTable>
2022-08-29 16:32:07 +02:00
{previous.map((info, i) => (
2022-08-16 02:18:39 +02:00
<tr key={i}>
<td>
<RowExample info={info} disabled={invalid} />
</td>
</tr>
))}
</ContactTable>
</Paper>
</Grid>
</Fragment>
) : undefined}
2022-08-29 16:32:07 +02:00
{previous.length > 0 ? (
<Grid item>
2022-09-13 16:07:39 +02:00
<p>
<i18n.Translate>
Or specify the origin of the money
</i18n.Translate>
</p>
2022-08-29 16:32:07 +02:00
</Grid>
) : (
<Grid item>
2022-09-13 16:07:39 +02:00
<p>
<i18n.Translate>Specify the origin of the money</i18n.Translate>
</p>
2022-08-29 16:32:07 +02:00
</Grid>
)}
2022-08-17 21:12:21 +02:00
<Grid item container columns={2} spacing={1}>
2022-08-16 02:18:39 +02:00
<Grid item xs={1}>
<Paper style={{ padding: 8 }}>
2022-09-13 16:07:39 +02:00
<p>
<i18n.Translate>From my bank account</i18n.Translate>
</p>
2022-08-16 02:18:39 +02:00
<Button
disabled={invalid}
onClick={async () =>
goToWalletManualWithdraw(currencyAndAmount)
}
>
2022-09-13 16:07:39 +02:00
<i18n.Translate>Withdraw</i18n.Translate>
2022-08-16 02:18:39 +02:00
</Button>
</Paper>
</Grid>
<Grid item xs={1}>
<Paper style={{ padding: 8 }}>
2022-09-13 16:07:39 +02:00
<p>
<i18n.Translate>From another wallet</i18n.Translate>
</p>
2022-08-29 16:32:07 +02:00
<Button
disabled={invalid}
onClick={async () => goToWalletWalletInvoice(currencyAndAmount)}
>
2022-09-13 16:07:39 +02:00
<i18n.Translate>Invoice</i18n.Translate>
2022-08-29 16:32:07 +02:00
</Button>
2022-08-16 02:18:39 +02:00
</Paper>
</Grid>
</Grid>
</Grid>
2022-08-14 15:10:37 +02:00
</Container>
);
}
2022-08-16 02:18:39 +02:00
export function DestinationSelectionSendCash({
amount: initialAmount,
goToWalletBankDeposit,
goToWalletWalletSend,
}: PropsSend): VNode {
2022-08-16 02:18:39 +02:00
const parsedInitialAmount = !initialAmount
? undefined
: Amounts.parse(initialAmount);
const parsedInitialAmountValue = !parsedInitialAmount
? ""
: Amounts.stringifyValue(parsedInitialAmount);
const currency = parsedInitialAmount?.currency;
const [amount, setAmount] = useState(parsedInitialAmountValue);
2022-11-04 19:38:58 +01:00
function positiveSetAmount(e: string):void {
const value = Number.parseInt(e, 10);
if (value < 0) return
setAmount(String(value))
}
2022-08-16 02:18:39 +02:00
const { i18n } = useTranslationContext();
const previous1: Contact[] = [];
const previous2: Contact[] = [
{
name: "International Bank",
icon: bankIcon,
description: "account ending with 3454",
},
{
name: "Max",
icon: bankIcon,
description: "account ending with 3454",
},
{
name: "Alex",
icon: bankIcon,
description: "account ending with 3454",
},
];
2022-08-29 16:32:07 +02:00
const previous = previous1;
2022-08-16 02:18:39 +02:00
if (!currency) {
2022-09-13 16:07:39 +02:00
return (
<div>
<i18n.Translate>currency not provided</i18n.Translate>
</div>
);
2022-08-16 02:18:39 +02:00
}
const currencyAndAmount = `${currency}:${amount}`;
const parsedAmount = Amounts.parse(currencyAndAmount);
const invalid = !parsedAmount || Amounts.isZero(parsedAmount);
2022-08-14 15:10:37 +02:00
return (
<Container>
2022-08-16 02:18:39 +02:00
<h1>
<i18n.Translate>Specify the amount and the destination</i18n.Translate>
</h1>
2022-08-16 04:01:05 +02:00
<div>
<TextField
label="Amount"
type="number"
2022-11-04 19:38:58 +01:00
min="0"
2022-08-16 04:01:05 +02:00
variant="filled"
required
error={invalid}
startAdornment={
<div style={{ padding: "25px 12px 8px 12px" }}>{currency}</div>
}
value={amount}
onChange={(e) => {
2022-11-04 19:38:58 +01:00
positiveSetAmount(e);
2022-08-16 04:01:05 +02:00
}}
/>
</div>
2022-08-16 02:18:39 +02:00
<Grid container spacing={1} columns={1}>
2022-08-29 16:32:07 +02:00
{previous.length > 0 ? (
2022-08-16 02:18:39 +02:00
<Fragment>
2022-09-13 16:07:39 +02:00
<p>
<i18n.Translate>Use previous destinations:</i18n.Translate>
</p>
2022-08-16 02:18:39 +02:00
<Grid item xs={1}>
<Paper style={{ padding: 8 }}>
<ContactTable>
2022-08-29 16:32:07 +02:00
{previous.map((info, i) => (
2022-08-16 02:18:39 +02:00
<tr key={i}>
<td>
<RowExample info={info} disabled={invalid} />
</td>
</tr>
))}
</ContactTable>
</Paper>
</Grid>
</Fragment>
) : undefined}
2022-08-29 16:32:07 +02:00
{previous.length > 0 ? (
<Grid item>
2022-09-13 16:07:39 +02:00
<p>
<i18n.Translate>
Or specify the destination of the money
</i18n.Translate>
</p>
2022-08-29 16:32:07 +02:00
</Grid>
) : (
<Grid item>
2022-09-13 16:07:39 +02:00
<p>
<i18n.Translate>
Specify the destination of the money
</i18n.Translate>
</p>
2022-08-29 16:32:07 +02:00
</Grid>
)}
2022-08-17 21:12:21 +02:00
<Grid item container columns={2} spacing={1}>
2022-08-16 02:18:39 +02:00
<Grid item xs={1}>
<Paper style={{ padding: 8 }}>
2022-09-13 16:07:39 +02:00
<p>
<i18n.Translate>To my bank account</i18n.Translate>
</p>
<Button
disabled={invalid}
onClick={async () => goToWalletBankDeposit(currencyAndAmount)}
>
2022-09-13 16:07:39 +02:00
<i18n.Translate>Deposit</i18n.Translate>
</Button>
2022-08-16 02:18:39 +02:00
</Paper>
</Grid>
<Grid item xs={1}>
<Paper style={{ padding: 8 }}>
2022-09-13 16:07:39 +02:00
<p>
<i18n.Translate>To another wallet</i18n.Translate>
</p>
<Button
disabled={invalid}
onClick={async () => goToWalletWalletSend(currencyAndAmount)}
>
2022-09-13 16:07:39 +02:00
<i18n.Translate>Send</i18n.Translate>
</Button>
2022-08-16 02:18:39 +02:00
</Paper>
</Grid>
</Grid>
</Grid>
2022-08-14 15:10:37 +02:00
</Container>
);
}