wallet-core/packages/taler-wallet-webextension/src/cta/InvoiceCreate/state.ts

188 lines
5.1 KiB
TypeScript
Raw Normal View History

2022-08-31 05:20:35 +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/>
*/
/* eslint-disable react-hooks/rules-of-hooks */
import {
Amounts,
TalerErrorDetail,
TalerProtocolTimestamp,
} from "@gnu-taler/taler-util";
2022-10-25 17:23:08 +02:00
import { TalerError, WalletApiOperation } from "@gnu-taler/taler-wallet-core";
2022-11-08 17:00:34 +01:00
import { isFuture, parse } from "date-fns";
2022-08-31 05:20:35 +02:00
import { useState } from "preact/hooks";
import { useAsyncAsHook } from "../../hooks/useAsyncAsHook.js";
import { useSelectedExchange } from "../../hooks/useSelectedExchange.js";
2022-11-28 13:29:14 +01:00
import { RecursiveState } from "../../utils/index.js";
2022-10-25 17:23:08 +02:00
import { wxApi } from "../../wxApi.js";
2022-08-31 05:20:35 +02:00
import { Props, State } from "./index.js";
export function useComponentState(
2022-09-16 21:03:58 +02:00
{ amount: amountStr, onClose, onSuccess }: Props,
2022-08-31 05:20:35 +02:00
api: typeof wxApi,
): RecursiveState<State> {
2022-09-16 19:29:35 +02:00
const amount = Amounts.parseOrThrow(amountStr);
2022-08-31 05:20:35 +02:00
const hook = useAsyncAsHook(() =>
api.wallet.call(WalletApiOperation.ListExchanges, {}),
);
2022-08-31 05:20:35 +02:00
if (!hook) {
return {
status: "loading",
error: undefined,
2022-09-16 19:29:35 +02:00
};
2022-08-31 05:20:35 +02:00
}
if (hook.hasError) {
return {
status: "loading-uri",
error: hook,
};
}
const exchangeList = hook.response.exchanges;
return () => {
2022-11-08 17:00:34 +01:00
const [subject, setSubject] = useState<string | undefined>();
const [timestamp, setTimestamp] = useState<string | undefined>();
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;
2022-11-08 17:00:34 +01:00
const hook = useAsyncAsHook(async () => {
const resp = await api.wallet.call(
WalletApiOperation.PreparePeerPullPayment,
{
amount: amountStr,
exchangeBaseUrl: exchange.exchangeBaseUrl,
},
);
return resp;
});
2022-11-08 17:00:34 +01:00
if (!hook) {
return {
status: "loading",
error: undefined,
};
2022-11-08 17:00:34 +01:00
}
if (hook.hasError) {
return {
status: "loading-uri",
error: hook,
};
2022-11-08 17:00:34 +01:00
}
const { amountEffective, amountRaw } = hook.response;
const requestAmount = Amounts.parseOrThrow(amountRaw);
const toBeReceived = Amounts.parseOrThrow(amountEffective);
2022-11-08 17:00:34 +01:00
let purse_expiration: TalerProtocolTimestamp | undefined = undefined;
2022-11-08 17:00:34 +01:00
let timestampError: string | undefined = undefined;
const t =
timestamp === undefined
? undefined
: parse(timestamp, "dd/MM/yyyy", new Date());
2022-11-08 17:00:34 +01:00
if (t !== undefined) {
if (Number.isNaN(t.getTime())) {
timestampError = 'Should have the format "dd/MM/yyyy"';
2022-11-08 17:00:34 +01:00
} else {
if (!isFuture(t)) {
timestampError = "Should be in the future";
2022-11-08 17:00:34 +01:00
} else {
purse_expiration = {
t_s: t.getTime() / 1000,
};
2022-11-08 17:00:34 +01:00
}
}
}
async function accept(): Promise<void> {
2022-11-08 17:00:34 +01:00
if (!subject || !purse_expiration) return;
try {
const resp = await api.wallet.call(
WalletApiOperation.InitiatePeerPullPayment,
{
exchangeBaseUrl: exchange.exchangeBaseUrl,
partialContractTerms: {
amount: Amounts.stringify(amount),
summary: subject,
purse_expiration,
},
},
);
onSuccess(resp.transactionId);
} catch (e) {
if (e instanceof TalerError) {
setOperationError(e.errorDetail);
}
console.error(e);
throw Error("error trying to accept");
2022-08-31 05:20:35 +02:00
}
}
const unableToCreate =
!subject || Amounts.isZero(amount) || !purse_expiration;
return {
status: "ready",
subject: {
error:
subject === undefined
? undefined
: !subject
? "Can't be empty"
: undefined,
2022-11-08 17:00:34 +01:00
value: subject ?? "",
onInput: async (e) => setSubject(e),
},
2022-11-08 17:00:34 +01:00
expiration: {
error: timestampError,
value: timestamp === undefined ? "" : timestamp,
onInput: async (e) => {
setTimestamp(e);
},
2022-11-08 17:00:34 +01:00
},
doSelectExchange: selectedExchange.doSelect,
exchangeUrl: exchange.exchangeBaseUrl,
create: {
2022-11-08 17:00:34 +01:00
onClick: unableToCreate ? undefined : accept,
},
cancel: {
onClick: onClose,
},
2022-11-08 17:00:34 +01:00
requestAmount,
toBeReceived,
error: undefined,
operationError,
};
};
2022-08-31 05:20:35 +02:00
}