From 3e060b80428943c6562250a6ff77eff10a0259b7 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 24 Oct 2022 10:46:14 +0200 Subject: repo: integrate packages from former merchant-backoffice.git --- .../instance/reserves/create/Create.stories.tsx | 42 +++ .../paths/instance/reserves/create/CreatePage.tsx | 168 +++++++++++ .../create/CreatedSuccessfully.stories.tsx | 53 ++++ .../reserves/create/CreatedSuccessfully.tsx | 79 ++++++ .../src/paths/instance/reserves/create/index.tsx | 71 +++++ .../paths/instance/reserves/details/DetailPage.tsx | 278 ++++++++++++++++++ .../instance/reserves/details/Details.stories.tsx | 105 +++++++ .../paths/instance/reserves/details/TipInfo.tsx | 87 ++++++ .../src/paths/instance/reserves/details/index.tsx | 56 ++++ .../instance/reserves/list/AutorizeTipModal.tsx | 85 ++++++ .../instance/reserves/list/CreatedSuccessfully.tsx | 100 +++++++ .../paths/instance/reserves/list/List.stories.tsx | 102 +++++++ .../src/paths/instance/reserves/list/Table.tsx | 313 +++++++++++++++++++++ .../src/paths/instance/reserves/list/index.tsx | 117 ++++++++ 14 files changed, 1656 insertions(+) create mode 100644 packages/merchant-backoffice-ui/src/paths/instance/reserves/create/Create.stories.tsx create mode 100644 packages/merchant-backoffice-ui/src/paths/instance/reserves/create/CreatePage.tsx create mode 100644 packages/merchant-backoffice-ui/src/paths/instance/reserves/create/CreatedSuccessfully.stories.tsx create mode 100644 packages/merchant-backoffice-ui/src/paths/instance/reserves/create/CreatedSuccessfully.tsx create mode 100644 packages/merchant-backoffice-ui/src/paths/instance/reserves/create/index.tsx create mode 100644 packages/merchant-backoffice-ui/src/paths/instance/reserves/details/DetailPage.tsx create mode 100644 packages/merchant-backoffice-ui/src/paths/instance/reserves/details/Details.stories.tsx create mode 100644 packages/merchant-backoffice-ui/src/paths/instance/reserves/details/TipInfo.tsx create mode 100644 packages/merchant-backoffice-ui/src/paths/instance/reserves/details/index.tsx create mode 100644 packages/merchant-backoffice-ui/src/paths/instance/reserves/list/AutorizeTipModal.tsx create mode 100644 packages/merchant-backoffice-ui/src/paths/instance/reserves/list/CreatedSuccessfully.tsx create mode 100644 packages/merchant-backoffice-ui/src/paths/instance/reserves/list/List.stories.tsx create mode 100644 packages/merchant-backoffice-ui/src/paths/instance/reserves/list/Table.tsx create mode 100644 packages/merchant-backoffice-ui/src/paths/instance/reserves/list/index.tsx (limited to 'packages/merchant-backoffice-ui/src/paths/instance/reserves') diff --git a/packages/merchant-backoffice-ui/src/paths/instance/reserves/create/Create.stories.tsx b/packages/merchant-backoffice-ui/src/paths/instance/reserves/create/Create.stories.tsx new file mode 100644 index 000000000..e138770a8 --- /dev/null +++ b/packages/merchant-backoffice-ui/src/paths/instance/reserves/create/Create.stories.tsx @@ -0,0 +1,42 @@ +/* + This file is part of GNU Taler + (C) 2021 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 + */ + +/** +* +* @author Sebastian Javier Marchano (sebasjm) +*/ + +import { h, VNode, FunctionalComponent } from 'preact'; +import { CreatePage as TestedComponent } from './CreatePage'; + + +export default { + title: 'Pages/Reserve/Create', + component: TestedComponent, + argTypes: { + onCreate: { action: 'onCreate' }, + onBack: { action: 'onBack' }, + }, +}; + +function createExample(Component: FunctionalComponent, props: Partial) { + const r = (args: any) => + r.args = props + return r +} + +export const Example = createExample(TestedComponent, { +}); diff --git a/packages/merchant-backoffice-ui/src/paths/instance/reserves/create/CreatePage.tsx b/packages/merchant-backoffice-ui/src/paths/instance/reserves/create/CreatePage.tsx new file mode 100644 index 000000000..2e85cf9c8 --- /dev/null +++ b/packages/merchant-backoffice-ui/src/paths/instance/reserves/create/CreatePage.tsx @@ -0,0 +1,168 @@ +/* + This file is part of GNU Taler + (C) 2021 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 + */ + +/** +* +* @author Sebastian Javier Marchano (sebasjm) +*/ + +import { Fragment, h, VNode } from "preact"; +import { StateUpdater, useEffect, useState } from "preact/hooks"; +import { FormErrors, FormProvider } from "../../../../components/form/FormProvider"; +import { Input } from "../../../../components/form/Input"; +import { InputCurrency } from "../../../../components/form/InputCurrency"; +import { ExchangeBackend, MerchantBackend } from "../../../../declaration"; +import { Translate, useTranslator } from "../../../../i18n"; +import { AsyncButton } from "../../../../components/exception/AsyncButton"; +import { canonicalizeBaseUrl, ExchangeKeysJson } from "@gnu-taler/taler-util" +import { PAYTO_WIRE_METHOD_LOOKUP, URL_REGEX } from "../../../../utils/constants"; +import { request } from "../../../../hooks/backend"; +import { InputSelector } from "../../../../components/form/InputSelector"; + +type Entity = MerchantBackend.Tips.ReserveCreateRequest + +interface Props { + onCreate: (d: Entity) => Promise; + onBack?: () => void; +} + + +enum Steps { + EXCHANGE, + WIRE_METHOD, +} + +interface ViewProps { + step: Steps, + setCurrentStep: (s: Steps) => void; + reserve: Partial; + onBack?: () => void; + submitForm: () => Promise; + setReserve: StateUpdater>; +} +function ViewStep({ step, setCurrentStep, reserve, onBack, submitForm, setReserve }: ViewProps): VNode { + const i18n = useTranslator() + const [wireMethods, setWireMethods] = useState>([]) + const [exchangeQueryError, setExchangeQueryError] = useState(undefined) + + useEffect(() => { + setExchangeQueryError(undefined) + }, [reserve.exchange_url]) + + switch (step) { + case Steps.EXCHANGE: { + const errors: FormErrors = { + initial_balance: !reserve.initial_balance ? 'cannot be empty' : !(parseInt(reserve.initial_balance.split(':')[1], 10) > 0) ? i18n`it should be greater than 0` : undefined, + exchange_url: !reserve.exchange_url ? i18n`cannot be empty` : !URL_REGEX.test(reserve.exchange_url) ? i18n`must be a valid URL` : !exchangeQueryError ? undefined : exchangeQueryError, + } + + const hasErrors = Object.keys(errors).some(k => (errors as any)[k] !== undefined) + + return + object={reserve} errors={errors} valueHandler={setReserve}> + name="initial_balance" label={i18n`Initial balance`} tooltip={i18n`balance prior to deposit`} /> + name="exchange_url" label={i18n`Exchange URL`} tooltip={i18n`URL of exchange`} /> + + +
+ {onBack && } + { + return request(`${reserve.exchange_url}wire`).then(r => { + const wireMethods = r.data.accounts.map(a => { + const match = PAYTO_WIRE_METHOD_LOOKUP.exec(a.payto_uri) + return match && match[1] || '' + }) + setWireMethods(wireMethods) + setCurrentStep(Steps.WIRE_METHOD) + return + }).catch((r: any) => { + setExchangeQueryError(r.message) + }) + }} data-tooltip={ + hasErrors ? i18n`Need to complete marked fields` : 'confirm operation' + } disabled={hasErrors} >Next +
+
+ } + + case Steps.WIRE_METHOD: { + const errors: FormErrors = { + wire_method: !reserve.wire_method ? i18n`cannot be empty` : undefined, + } + + const hasErrors = Object.keys(errors).some(k => (errors as any)[k] !== undefined) + return + object={reserve} errors={errors} valueHandler={setReserve}> + name="initial_balance" label={i18n`Initial balance`} tooltip={i18n`balance prior to deposit`} readonly /> + name="exchange_url" label={i18n`Exchange URL`} tooltip={i18n`URL of exchange`} readonly /> + name="wire_method" label={i18n`Wire method`} tooltip={i18n`method to use for wire transfer`} values={wireMethods} placeholder={i18n`Select one wire method`} /> + +
+ {onBack && } + Confirm +
+
+ + } + } +} + +export function CreatePage({ onCreate, onBack }: Props): VNode { + const [reserve, setReserve] = useState>({}) + + + const submitForm = () => { + return onCreate(reserve as Entity) + } + + const [currentStep, setCurrentStep] = useState(Steps.EXCHANGE) + + + return
+
+
+
+} diff --git a/packages/merchant-backoffice-ui/src/paths/instance/reserves/create/CreatedSuccessfully.stories.tsx b/packages/merchant-backoffice-ui/src/paths/instance/reserves/create/CreatedSuccessfully.stories.tsx new file mode 100644 index 000000000..f013040ff --- /dev/null +++ b/packages/merchant-backoffice-ui/src/paths/instance/reserves/create/CreatedSuccessfully.stories.tsx @@ -0,0 +1,53 @@ +/* + This file is part of GNU Taler + (C) 2021 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 + */ + +/** +* +* @author Sebastian Javier Marchano (sebasjm) +*/ + +import { h, VNode, FunctionalComponent } from 'preact'; +import { CreatedSuccessfully as TestedComponent } from './CreatedSuccessfully'; + + +export default { + title: 'Pages/Reserve/CreatedSuccessfully', + component: TestedComponent, + argTypes: { + onCreate: { action: 'onCreate' }, + onBack: { action: 'onBack' }, + }, +}; + +function createExample(Component: FunctionalComponent, props: Partial) { + const r = (args: any) => + r.args = props + return r +} + +export const Example = createExample(TestedComponent, { + entity: { + request: { + exchange_url: 'http://exchange.taler/', + initial_balance: 'TESTKUDOS:1', + wire_method: 'x-taler-bank', + }, + response: { + payto_uri: 'payto://x-taler-bank/bank.taler:8080/exchange_account', + reserve_pub: 'WEQWDASDQWEASDADASDQWEQWEASDAS' + } + } +}); diff --git a/packages/merchant-backoffice-ui/src/paths/instance/reserves/create/CreatedSuccessfully.tsx b/packages/merchant-backoffice-ui/src/paths/instance/reserves/create/CreatedSuccessfully.tsx new file mode 100644 index 000000000..255486d22 --- /dev/null +++ b/packages/merchant-backoffice-ui/src/paths/instance/reserves/create/CreatedSuccessfully.tsx @@ -0,0 +1,79 @@ +/* + This file is part of GNU Taler + (C) 2021 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 + */ + +import { h, VNode } from "preact"; +import { CreatedSuccessfully as Template } from "../../../../components/notifications/CreatedSuccessfully"; +import { MerchantBackend } from "../../../../declaration"; +import { Translate } from "../../../../i18n"; +import { QR } from "../../../../components/exception/QR"; + +type Entity = { request: MerchantBackend.Tips.ReserveCreateRequest, response: MerchantBackend.Tips.ReserveCreateConfirmation }; + +interface Props { + entity: Entity; + onConfirm: () => void; + onCreateAnother?: () => void; +} + +export function CreatedSuccessfully({ entity, onConfirm, onCreateAnother }: Props): VNode { + const link = `${entity.response.payto_uri}?message=${entity.response.reserve_pub}&amount=${entity.request.initial_balance}` + + return ; +} + diff --git a/packages/merchant-backoffice-ui/src/paths/instance/reserves/create/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/reserves/create/index.tsx new file mode 100644 index 000000000..5c2fdaf4b --- /dev/null +++ b/packages/merchant-backoffice-ui/src/paths/instance/reserves/create/index.tsx @@ -0,0 +1,71 @@ +/* + This file is part of GNU Taler + (C) 2021 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 + */ + +/** + * + * @author Sebastian Javier Marchano (sebasjm) + */ + +import { Fragment, h, VNode } from "preact"; +import { useState } from "preact/hooks"; +import { NotificationCard } from "../../../../components/menu"; +import { MerchantBackend } from "../../../../declaration"; +import { useReservesAPI } from "../../../../hooks/reserves"; +import { useTranslator } from "../../../../i18n"; +import { Notification } from "../../../../utils/types"; +import { CreatedSuccessfully } from "./CreatedSuccessfully"; +import { CreatePage } from "./CreatePage"; +interface Props { + onBack: () => void; + onConfirm: () => void; +} +export default function CreateReserve({ onBack, onConfirm }: Props): VNode { + const { createReserve } = useReservesAPI(); + const [notif, setNotif] = useState(undefined); + const i18n = useTranslator(); + + const [createdOk, setCreatedOk] = useState< + | { + request: MerchantBackend.Tips.ReserveCreateRequest; + response: MerchantBackend.Tips.ReserveCreateConfirmation; + } + | undefined + >(undefined); + + if (createdOk) { + return ; + } + + return ( + + + { + return createReserve(request) + .then((r) => setCreatedOk({ request, response: r.data })) + .catch((error) => { + setNotif({ + message: i18n`could not create reserve`, + type: "ERROR", + description: error.message, + }); + }); + }} + /> + + ); +} diff --git a/packages/merchant-backoffice-ui/src/paths/instance/reserves/details/DetailPage.tsx b/packages/merchant-backoffice-ui/src/paths/instance/reserves/details/DetailPage.tsx new file mode 100644 index 000000000..cbc70179b --- /dev/null +++ b/packages/merchant-backoffice-ui/src/paths/instance/reserves/details/DetailPage.tsx @@ -0,0 +1,278 @@ +/* + This file is part of GNU Taler + (C) 2021 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 + */ + +/** + * + * @author Sebastian Javier Marchano (sebasjm) + */ + +import { Amounts } from "@gnu-taler/taler-util"; +import { format } from "date-fns"; +import { Fragment, h, VNode } from "preact"; +import { useState } from "preact/hooks"; +import { QR } from "../../../../components/exception/QR"; +import { FormProvider } from "../../../../components/form/FormProvider"; +import { Input } from "../../../../components/form/Input"; +import { InputCurrency } from "../../../../components/form/InputCurrency"; +import { InputDate } from "../../../../components/form/InputDate"; +import { TextField } from "../../../../components/form/TextField"; +import { ContinueModal, SimpleModal } from "../../../../components/modal"; +import { MerchantBackend } from "../../../../declaration"; +import { useTipDetails } from "../../../../hooks/reserves"; +import { Translate, useTranslator } from "../../../../i18n"; +import { TipInfo } from "./TipInfo"; + +type Entity = MerchantBackend.Tips.ReserveDetail; +type CT = MerchantBackend.ContractTerms; + +interface Props { + onBack: () => void; + selected: Entity; + id: string; +} + +export function DetailPage({ id, selected, onBack }: Props): VNode { + const i18n = useTranslator(); + const didExchangeAckTransfer = Amounts.isNonZero( + Amounts.parseOrThrow(selected.exchange_initial_amount) + ); + const link = `${selected.payto_uri}?message=${id}&amount=${selected.merchant_initial_amount}`; + + return ( +
+
+
+
+ + + name="creation_time" + label={i18n`Created at`} + readonly + /> + + name="expiration_time" + label={i18n`Valid until`} + readonly + /> + + name="merchant_initial_amount" + label={i18n`Created balance`} + readonly + /> + + name="exchange_url" + label={i18n`Exchange URL`} + readonly + > + + {selected.exchange_url} + + + + {didExchangeAckTransfer && ( + + + name="exchange_initial_amount" + label={i18n`Exchange balance`} + readonly + /> + + name="pickup_amount" + label={i18n`Picked up`} + readonly + /> + + name="committed_amount" + label={i18n`Committed`} + readonly + /> + + )} + + name="payto_uri" + label={i18n`Account address`} + readonly + /> + + + + {didExchangeAckTransfer ? ( + +
+
+

+ + + + Tips +

+
+
+
+
+ {selected.tips && selected.tips.length > 0 ? ( + + ) : ( + + )} + + + + + + ) : ( + +

+ + To complete the setup of the reserve, you must now initiate a + wire transfer using the given wire transfer subject and + crediting the specified amount to the indicated account of the + exchange. + +

+

+ + If your system supports RFC 8905, you can do this by opening + this URI: + +

+
+                
+                  {link}
+                
+              
+ +
+ )} + +
+ +
+ + +
+
+ ); +} + +function EmptyTable(): VNode { + return ( +
+

+ + + +

+

+ No tips has been authorized from this reserve +

+
+ ); +} + +interface TableProps { + tips: MerchantBackend.Tips.TipStatusEntry[]; +} + +function Table({ tips }: TableProps): VNode { + return ( +
+
+ + + + + + + + + + {tips.map((t, i) => { + return ; + })} + +
+ Authorized + + Picked up + + Reason + + Expiration +
+
+ ); +} + +function TipRow({ + id, + entry, +}: { + id: string; + entry: MerchantBackend.Tips.TipStatusEntry; +}) { + const [selected, setSelected] = useState(false); + const result = useTipDetails(id); + if (result.loading) { + return ( + + ... + ... + ... + ... + + ); + } + if (!result.ok) { + return ( + + ... {/* authorized */} + {entry.total_amount} + {entry.reason} + ... {/* expired */} + + ); + } + const info = result.data; + function onSelect() { + setSelected(true); + } + return ( + + {selected && ( + setSelected(false)} + > + + + )} + + {info.total_authorized} + {info.total_picked_up} + {info.reason} + + {info.expiration.t_s === "never" + ? "never" + : format(info.expiration.t_s * 1000, "yyyy/MM/dd HH:mm:ss")} + + + + ); +} diff --git a/packages/merchant-backoffice-ui/src/paths/instance/reserves/details/Details.stories.tsx b/packages/merchant-backoffice-ui/src/paths/instance/reserves/details/Details.stories.tsx new file mode 100644 index 000000000..98c1fa72e --- /dev/null +++ b/packages/merchant-backoffice-ui/src/paths/instance/reserves/details/Details.stories.tsx @@ -0,0 +1,105 @@ +/* + This file is part of GNU Taler + (C) 2021 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 + */ + +/** + * + * @author Sebastian Javier Marchano (sebasjm) + */ + +import { h, VNode, FunctionalComponent } from "preact"; +import { DetailPage as TestedComponent } from "./DetailPage"; + +export default { + title: "Pages/Reserve/Detail", + component: TestedComponent, + argTypes: { + onUpdate: { action: "onUpdate" }, + onBack: { action: "onBack" }, + }, +}; + +function createExample( + Component: FunctionalComponent, + props: Partial +) { + const r = (args: any) => ; + r.args = props; + return r; +} + +export const Funded = createExample(TestedComponent, { + id: "THISISTHERESERVEID", + selected: { + active: true, + committed_amount: "TESTKUDOS:10", + creation_time: { + t_s: new Date().getTime() / 1000, + }, + exchange_initial_amount: "TESTKUDOS:10", + expiration_time: { + t_s: new Date().getTime() / 1000, + }, + merchant_initial_amount: "TESTKUDOS:10", + pickup_amount: "TESTKUDOS:10", + payto_uri: "payto://x-taler-bank/bank.taler:8080/account", + exchange_url: "http://exchange.taler/", + }, +}); + +export const NotYetFunded = createExample(TestedComponent, { + id: "THISISTHERESERVEID", + selected: { + active: true, + committed_amount: "TESTKUDOS:10", + creation_time: { + t_s: new Date().getTime() / 1000, + }, + exchange_initial_amount: "TESTKUDOS:0", + expiration_time: { + t_s: new Date().getTime() / 1000, + }, + merchant_initial_amount: "TESTKUDOS:10", + pickup_amount: "TESTKUDOS:10", + payto_uri: "payto://x-taler-bank/bank.taler:8080/account", + exchange_url: "http://exchange.taler/", + }, +}); + +export const FundedWithEmptyTips = createExample(TestedComponent, { + id: "THISISTHERESERVEID", + selected: { + active: true, + committed_amount: "TESTKUDOS:10", + creation_time: { + t_s: new Date().getTime() / 1000, + }, + exchange_initial_amount: "TESTKUDOS:10", + expiration_time: { + t_s: new Date().getTime() / 1000, + }, + merchant_initial_amount: "TESTKUDOS:10", + pickup_amount: "TESTKUDOS:10", + payto_uri: "payto://x-taler-bank/bank.taler:8080/account", + exchange_url: "http://exchange.taler/", + tips: [ + { + reason: "asdasd", + tip_id: "123", + total_amount: "TESTKUDOS:1", + }, + ], + }, +}); diff --git a/packages/merchant-backoffice-ui/src/paths/instance/reserves/details/TipInfo.tsx b/packages/merchant-backoffice-ui/src/paths/instance/reserves/details/TipInfo.tsx new file mode 100644 index 000000000..3f384966b --- /dev/null +++ b/packages/merchant-backoffice-ui/src/paths/instance/reserves/details/TipInfo.tsx @@ -0,0 +1,87 @@ +/* + This file is part of GNU Taler + (C) 2021 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 + */ +import { format } from "date-fns"; +import { Fragment, h, VNode } from "preact"; +import { useBackendContext } from "../../../../context/backend"; +import { MerchantBackend } from "../../../../declaration"; + +type Entity = MerchantBackend.Tips.TipDetails; + +interface Props { + id: string; + entity: Entity; + amount: string; +} + +export function TipInfo({ id, amount, entity }: Props): VNode { + const { url } = useBackendContext(); + const tipHost = url.replace(/.*:\/\//, ""); // remove protocol part + const proto = url.startsWith("http://") ? "taler+http" : "taler"; + const tipURL = `${proto}://tip/${tipHost}/${id}`; + return ( + +
+
+ +
+
+
+

+ +

+
+
+
+
+
+ +
+
+ +
+
+
+
+ +
+
+
+

+ +

+
+
+
+
+ ); +} diff --git a/packages/merchant-backoffice-ui/src/paths/instance/reserves/details/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/reserves/details/index.tsx new file mode 100644 index 000000000..c2483f053 --- /dev/null +++ b/packages/merchant-backoffice-ui/src/paths/instance/reserves/details/index.tsx @@ -0,0 +1,56 @@ +/* + This file is part of GNU Taler + (C) 2021 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 + */ + +/** + * + * @author Sebastian Javier Marchano (sebasjm) + */ + +import { Fragment, h, VNode } from "preact"; +import { Loading } from "../../../../components/exception/loading"; +import { HttpError } from "../../../../hooks/backend"; +import { useReserveDetails } from "../../../../hooks/reserves"; +import { DetailPage } from "./DetailPage"; + +interface Props { + rid: string; + + onUnauthorized: () => VNode; + onLoadError: (error: HttpError) => VNode; + onNotFound: () => VNode; + onDelete: () => void; + onBack: () => void; +} +export default function DetailReserve({ + rid, + onUnauthorized, + onLoadError, + onNotFound, + onBack, + onDelete, +}: Props): VNode { + const result = useReserveDetails(rid); + + if (result.clientError && result.isUnauthorized) return onUnauthorized(); + if (result.clientError && result.isNotfound) return onNotFound(); + if (result.loading) return ; + if (!result.ok) return onLoadError(result); + return ( + + + + ); +} diff --git a/packages/merchant-backoffice-ui/src/paths/instance/reserves/list/AutorizeTipModal.tsx b/packages/merchant-backoffice-ui/src/paths/instance/reserves/list/AutorizeTipModal.tsx new file mode 100644 index 000000000..ec468b2e9 --- /dev/null +++ b/packages/merchant-backoffice-ui/src/paths/instance/reserves/list/AutorizeTipModal.tsx @@ -0,0 +1,85 @@ +/* + This file is part of GNU Taler + (C) 2021 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 + */ + +/** +* +* @author Sebastian Javier Marchano (sebasjm) +*/ + +import { h, VNode } from "preact"; +import { useState } from "preact/hooks"; +import { FormErrors, FormProvider } from "../../../../components/form/FormProvider"; +import { Input } from "../../../../components/form/Input"; +import { InputCurrency } from "../../../../components/form/InputCurrency"; +import { ConfirmModal, ContinueModal } from "../../../../components/modal"; +import { MerchantBackend } from "../../../../declaration"; +import { useTranslator } from "../../../../i18n"; +import { AuthorizeTipSchema } from "../../../../schemas"; +import { CreatedSuccessfully } from "./CreatedSuccessfully"; +import * as yup from 'yup'; + +interface AuthorizeTipModalProps { + onCancel: () => void; + onConfirm: (value: MerchantBackend.Tips.TipCreateRequest) => void; + tipAuthorized?: { + response: MerchantBackend.Tips.TipCreateConfirmation; + request: MerchantBackend.Tips.TipCreateRequest; + }; +} + +export function AuthorizeTipModal({ onCancel, onConfirm, tipAuthorized }: AuthorizeTipModalProps): VNode { + // const result = useOrderDetails(id) + type State = MerchantBackend.Tips.TipCreateRequest + const [form, setValue] = useState>({}) + const i18n = useTranslator(); + + // const [errors, setErrors] = useState>({}) + let errors: FormErrors = {} + try { + AuthorizeTipSchema.validateSync(form, { abortEarly: false }) + } catch (err) { + if (err instanceof yup.ValidationError) { + const yupErrors = err.inner as any[] + errors = yupErrors.reduce((prev, cur) => !cur.path ? prev : ({ ...prev, [cur.path]: cur.message }), {}) + } + } + const hasErrors = Object.keys(errors).some(k => (errors as any)[k] !== undefined) + + const validateAndConfirm = () => { + onConfirm(form as State) + } + if (tipAuthorized) { + return + + + } + + return + + errors={errors} object={form} valueHandler={setValue} > + name="amount" label={i18n`Amount`} tooltip={i18n`amount of tip`} /> + name="justification" label={i18n`Justification`} inputType="multiline" tooltip={i18n`reason for the tip`} /> + name="next_url" label={i18n`URL after tip`} tooltip={i18n`URL to visit after tip payment`} /> + + + +} + + diff --git a/packages/merchant-backoffice-ui/src/paths/instance/reserves/list/CreatedSuccessfully.tsx b/packages/merchant-backoffice-ui/src/paths/instance/reserves/list/CreatedSuccessfully.tsx new file mode 100644 index 000000000..1e5f0758f --- /dev/null +++ b/packages/merchant-backoffice-ui/src/paths/instance/reserves/list/CreatedSuccessfully.tsx @@ -0,0 +1,100 @@ +/* + This file is part of GNU Taler + (C) 2021 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 + */ +import { format } from "date-fns"; +import { Fragment, h, VNode } from "preact"; +import { CreatedSuccessfully as Template } from "../../../../components/notifications/CreatedSuccessfully"; +import { MerchantBackend } from "../../../../declaration"; + +type Entity = MerchantBackend.Tips.TipCreateConfirmation; + +interface Props { + entity: Entity; + request: MerchantBackend.Tips.TipCreateRequest; + onConfirm: () => void; + onCreateAnother?: () => void; +} + +export function CreatedSuccessfully({ + request, + entity, + onConfirm, + onCreateAnother, +}: Props): VNode { + return ( + +
+
+ +
+
+
+

+ +

+
+
+
+
+
+ +
+
+
+

+ +

+
+
+
+
+
+ +
+
+
+

+ +

+
+
+
+
+
+ +
+
+
+

+ +

+
+
+
+
+ ); +} diff --git a/packages/merchant-backoffice-ui/src/paths/instance/reserves/list/List.stories.tsx b/packages/merchant-backoffice-ui/src/paths/instance/reserves/list/List.stories.tsx new file mode 100644 index 000000000..1cb9e748c --- /dev/null +++ b/packages/merchant-backoffice-ui/src/paths/instance/reserves/list/List.stories.tsx @@ -0,0 +1,102 @@ +/* + This file is part of GNU Taler + (C) 2021 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 + */ + +/** + * + * @author Sebastian Javier Marchano (sebasjm) + */ + +import { h, VNode, FunctionalComponent } from "preact"; +import { CardTable as TestedComponent } from "./Table"; + +export default { + title: "Pages/Reserve/List", + component: TestedComponent, + argTypes: { + onCreate: { action: "onCreate" }, + onDelete: { action: "onDelete" }, + onNewTip: { action: "onNewTip" }, + onSelect: { action: "onSelect" }, + }, +}; + +function createExample( + Component: FunctionalComponent, + props: Partial +) { + const r = (args: any) => ; + r.args = props; + return r; +} + +export const AllFunded = createExample(TestedComponent, { + instances: [ + { + id: "reseverId", + active: true, + committed_amount: "TESTKUDOS:10", + creation_time: { + t_s: new Date().getTime() / 1000, + }, + exchange_initial_amount: "TESTKUDOS:10", + expiration_time: { + t_s: new Date().getTime() / 1000, + }, + merchant_initial_amount: "TESTKUDOS:10", + pickup_amount: "TESTKUDOS:10", + reserve_pub: "WEQWDASDQWEASDADASDQWEQWEASDAS", + }, + { + id: "reseverId2", + active: true, + committed_amount: "TESTKUDOS:13", + creation_time: { + t_s: new Date().getTime() / 1000, + }, + exchange_initial_amount: "TESTKUDOS:10", + expiration_time: { + t_s: new Date().getTime() / 1000, + }, + merchant_initial_amount: "TESTKUDOS:10", + pickup_amount: "TESTKUDOS:10", + reserve_pub: "WEQWDASDQWEASDADASDQWEQWEASDAS", + }, + ], +}); + +export const Empty = createExample(TestedComponent, { + instances: [], +}); + +export const OneNotYetFunded = createExample(TestedComponent, { + instances: [ + { + id: "reseverId", + active: true, + committed_amount: "TESTKUDOS:0", + creation_time: { + t_s: new Date().getTime() / 1000, + }, + exchange_initial_amount: "TESTKUDOS:0", + expiration_time: { + t_s: new Date().getTime() / 1000, + }, + merchant_initial_amount: "TESTKUDOS:10", + pickup_amount: "TESTKUDOS:10", + reserve_pub: "WEQWDASDQWEASDADASDQWEQWEASDAS", + }, + ], +}); diff --git a/packages/merchant-backoffice-ui/src/paths/instance/reserves/list/Table.tsx b/packages/merchant-backoffice-ui/src/paths/instance/reserves/list/Table.tsx new file mode 100644 index 000000000..b3bb7b020 --- /dev/null +++ b/packages/merchant-backoffice-ui/src/paths/instance/reserves/list/Table.tsx @@ -0,0 +1,313 @@ +/* + This file is part of GNU Taler + (C) 2021 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 + */ + +/** + * + * @author Sebastian Javier Marchano (sebasjm) + */ + +import { format } from "date-fns"; +import { Fragment, h, VNode } from "preact"; +import { MerchantBackend, WithId } from "../../../../declaration"; +import { Translate, useTranslator } from "../../../../i18n"; + +type Entity = MerchantBackend.Tips.ReserveStatusEntry & WithId; + +interface Props { + instances: Entity[]; + onNewTip: (id: Entity) => void; + onSelect: (id: Entity) => void; + onDelete: (id: Entity) => void; + onCreate: () => void; +} + +export function CardTable({ + instances, + onCreate, + onSelect, + onNewTip, + onDelete, +}: Props): VNode { + const [withoutFunds, withFunds] = instances.reduce((prev, current) => { + const amount = current.exchange_initial_amount; + if (amount.endsWith(":0")) { + prev[0] = prev[0].concat(current); + } else { + prev[1] = prev[1].concat(current); + } + return prev; + }, new Array>([], [])); + + const i18n = useTranslator(); + + return ( + + {withoutFunds.length > 0 && ( +
+
+

+ + + + Reserves not yet funded +

+
+
+
+
+ +
+
+
+
+ )} + +
+
+

+ + + + Reserves ready +

+
+
+ + + +
+
+
+
+
+ {withFunds.length > 0 ? ( + + ) : ( + + )} + + + + + + ); +} +interface TableProps { + instances: Entity[]; + onNewTip: (id: Entity) => void; + onDelete: (id: Entity) => void; + onSelect: (id: Entity) => void; +} + +function Table({ instances, onNewTip, onSelect, onDelete }: TableProps): VNode { + const i18n = useTranslator(); + return ( +
+
+ + + + + + + + + + + {instances.map((i) => { + return ( + + + + + + + + + ); + })} + +
+ Created at + + Expires at + + Initial + + Picked up + + Committed + +
onSelect(i)} + style={{ cursor: "pointer" }} + > + {i.creation_time.t_s === "never" + ? "never" + : format(i.creation_time.t_s * 1000, "yyyy/MM/dd HH:mm:ss")} + onSelect(i)} + style={{ cursor: "pointer" }} + > + {i.expiration_time.t_s === "never" + ? "never" + : format( + i.expiration_time.t_s * 1000, + "yyyy/MM/dd HH:mm:ss" + )} + onSelect(i)} + style={{ cursor: "pointer" }} + > + {i.exchange_initial_amount} + onSelect(i)} + style={{ cursor: "pointer" }} + > + {i.pickup_amount} + onSelect(i)} + style={{ cursor: "pointer" }} + > + {i.committed_amount} + +
+ + +
+
+
+ ); +} + +function EmptyTable(): VNode { + return ( +
+

+ + + +

+

+ + There is no ready reserves yet, add more pressing the + sign or fund + them + +

+
+ ); +} + +function TableWithoutFund({ + instances, + onSelect, + onDelete, +}: TableProps): VNode { + const i18n = useTranslator(); + return ( +
+ + + + + + + + + + {instances.map((i) => { + return ( + + + + + + + ); + })} + +
+ Created at + + Expires at + + Expected Balance + +
onSelect(i)} + style={{ cursor: "pointer" }} + > + {i.creation_time.t_s === "never" + ? "never" + : format(i.creation_time.t_s * 1000, "yyyy/MM/dd HH:mm:ss")} + onSelect(i)} + style={{ cursor: "pointer" }} + > + {i.expiration_time.t_s === "never" + ? "never" + : format( + i.expiration_time.t_s * 1000, + "yyyy/MM/dd HH:mm:ss" + )} + onSelect(i)} + style={{ cursor: "pointer" }} + > + {i.merchant_initial_amount} + +
+ +
+
+
+ ); +} diff --git a/packages/merchant-backoffice-ui/src/paths/instance/reserves/list/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/reserves/list/index.tsx new file mode 100644 index 000000000..f071b5635 --- /dev/null +++ b/packages/merchant-backoffice-ui/src/paths/instance/reserves/list/index.tsx @@ -0,0 +1,117 @@ +/* + This file is part of GNU Taler + (C) 2021 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 + */ + +/** + * + * @author Sebastian Javier Marchano (sebasjm) + */ + +import { h, VNode } from "preact"; +import { useState } from "preact/hooks"; +import { Loading } from "../../../../components/exception/loading"; +import { NotificationCard } from "../../../../components/menu"; +import { MerchantBackend } from "../../../../declaration"; +import { HttpError } from "../../../../hooks/backend"; +import { + useInstanceReserves, + useReservesAPI, +} from "../../../../hooks/reserves"; +import { useTranslator } from "../../../../i18n"; +import { Notification } from "../../../../utils/types"; +import { CardTable } from "./Table"; +import { AuthorizeTipModal } from "./AutorizeTipModal"; + +interface Props { + onUnauthorized: () => VNode; + onLoadError: (e: HttpError) => VNode; + onSelect: (id: string) => void; + onNotFound: () => VNode; + onCreate: () => void; +} + +interface TipConfirmation { + response: MerchantBackend.Tips.TipCreateConfirmation; + request: MerchantBackend.Tips.TipCreateRequest; +} + +export default function ListTips({ + onUnauthorized, + onLoadError, + onNotFound, + onSelect, + onCreate, +}: Props): VNode { + const result = useInstanceReserves(); + const { deleteReserve, authorizeTipReserve } = useReservesAPI(); + const [notif, setNotif] = useState(undefined); + const i18n = useTranslator(); + const [reserveForTip, setReserveForTip] = useState( + undefined + ); + const [tipAuthorized, setTipAuthorized] = useState< + TipConfirmation | undefined + >(undefined); + + if (result.clientError && result.isUnauthorized) return onUnauthorized(); + if (result.clientError && result.isNotfound) return onNotFound(); + if (result.loading) return ; + if (!result.ok) return onLoadError(result); + + return ( +
+ + + {reserveForTip && ( + { + setReserveForTip(undefined); + setTipAuthorized(undefined); + }} + tipAuthorized={tipAuthorized} + onConfirm={async (request) => { + try { + const response = await authorizeTipReserve( + reserveForTip, + request + ); + setTipAuthorized({ + request, + response: response.data, + }); + } catch (error) { + setNotif({ + message: i18n`could not create the tip`, + type: "ERROR", + description: error instanceof Error ? error.message : undefined, + }); + setReserveForTip(undefined); + } + }} + /> + )} + + r.active) + .map((o) => ({ ...o, id: o.reserve_pub }))} + onCreate={onCreate} + onDelete={(reserve) => deleteReserve(reserve.reserve_pub)} + onSelect={(reserve) => onSelect(reserve.id)} + onNewTip={(reserve) => setReserveForTip(reserve.id)} + /> +
+ ); +} -- cgit v1.2.3