From 7a201c3b885c5d23bf0fd0f3da32379a49b30c38 Mon Sep 17 00:00:00 2001 From: Nic Eigel Date: Sun, 14 Jan 2024 15:18:12 +0100 Subject: adding auditor-backoffice-ui --- .../instance/reserves/list/AutorizeRewardModal.tsx | 124 ++++++++ .../instance/reserves/list/CreatedSuccessfully.tsx | 102 +++++++ .../paths/instance/reserves/list/List.stories.tsx | 96 +++++++ .../src/paths/instance/reserves/list/Table.tsx | 320 +++++++++++++++++++++ .../src/paths/instance/reserves/list/index.tsx | 171 +++++++++++ 5 files changed, 813 insertions(+) create mode 100644 packages/auditor-backoffice-ui/src/paths/instance/reserves/list/AutorizeRewardModal.tsx create mode 100644 packages/auditor-backoffice-ui/src/paths/instance/reserves/list/CreatedSuccessfully.tsx create mode 100644 packages/auditor-backoffice-ui/src/paths/instance/reserves/list/List.stories.tsx create mode 100644 packages/auditor-backoffice-ui/src/paths/instance/reserves/list/Table.tsx create mode 100644 packages/auditor-backoffice-ui/src/paths/instance/reserves/list/index.tsx (limited to 'packages/auditor-backoffice-ui/src/paths/instance/reserves/list') diff --git a/packages/auditor-backoffice-ui/src/paths/instance/reserves/list/AutorizeRewardModal.tsx b/packages/auditor-backoffice-ui/src/paths/instance/reserves/list/AutorizeRewardModal.tsx new file mode 100644 index 000000000..e205ee621 --- /dev/null +++ b/packages/auditor-backoffice-ui/src/paths/instance/reserves/list/AutorizeRewardModal.tsx @@ -0,0 +1,124 @@ +/* + This file is part of GNU Taler + (C) 2021-2023 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 { useTranslationContext } from "@gnu-taler/web-util/browser"; +import { h, VNode } from "preact"; +import { useState } from "preact/hooks"; +import * as yup from "yup"; +import { + FormErrors, + FormProvider, +} from "../../../../components/form/FormProvider.js"; +import { Input } from "../../../../components/form/Input.js"; +import { InputCurrency } from "../../../../components/form/InputCurrency.js"; +import { + ConfirmModal, + ContinueModal, +} from "../../../../components/modal/index.js"; +import { MerchantBackend } from "../../../../declaration.js"; +import { AuthorizeRewardSchema } from "../../../../schemas/index.js"; +import { CreatedSuccessfully } from "./CreatedSuccessfully.js"; + +interface AuthorizeRewardModalProps { + onCancel: () => void; + onConfirm: (value: MerchantBackend.Rewards.RewardCreateRequest) => void; + rewardAuthorized?: { + response: MerchantBackend.Rewards.RewardCreateConfirmation; + request: MerchantBackend.Rewards.RewardCreateRequest; + }; +} + +export function AuthorizeRewardModal({ + onCancel, + onConfirm, + rewardAuthorized, +}: AuthorizeRewardModalProps): VNode { + // const result = useOrderDetails(id) + type State = MerchantBackend.Rewards.RewardCreateRequest; + const [form, setValue] = useState>({}); + const { i18n } = useTranslationContext(); + + // const [errors, setErrors] = useState>({}) + let errors: FormErrors = {}; + try { + AuthorizeRewardSchema.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 (rewardAuthorized) { + return ( + + + + ); + } + + return ( + + + errors={errors} + object={form} + valueHandler={setValue} + > + + name="amount" + label={i18n.str`Amount`} + tooltip={i18n.str`amount of reward`} + /> + + name="justification" + label={i18n.str`Justification`} + inputType="multiline" + tooltip={i18n.str`reason for the reward`} + /> + + name="next_url" + label={i18n.str`URL after reward`} + tooltip={i18n.str`URL to visit after reward payment`} + /> + + + ); +} diff --git a/packages/auditor-backoffice-ui/src/paths/instance/reserves/list/CreatedSuccessfully.tsx b/packages/auditor-backoffice-ui/src/paths/instance/reserves/list/CreatedSuccessfully.tsx new file mode 100644 index 000000000..b78236bc7 --- /dev/null +++ b/packages/auditor-backoffice-ui/src/paths/instance/reserves/list/CreatedSuccessfully.tsx @@ -0,0 +1,102 @@ +/* + This file is part of GNU Taler + (C) 2021-2023 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.js"; +import { MerchantBackend } from "../../../../declaration.js"; +import { datetimeFormatForSettings, useSettings } from "../../../../hooks/useSettings.js"; + +type Entity = MerchantBackend.Rewards.RewardCreateConfirmation; + +interface Props { + entity: Entity; + request: MerchantBackend.Rewards.RewardCreateRequest; + onConfirm: () => void; + onCreateAnother?: () => void; +} + +export function CreatedSuccessfully({ + request, + entity, + onConfirm, + onCreateAnother, +}: Props): VNode { + const [settings] = useSettings(); + return ( + +
+
+ +
+
+
+

+ +

+
+
+
+
+
+ +
+
+
+

+ +

+
+
+
+
+
+ +
+
+
+

+ +

+
+
+
+
+
+ +
+
+
+

+ +

+
+
+
+
+ ); +} diff --git a/packages/auditor-backoffice-ui/src/paths/instance/reserves/list/List.stories.tsx b/packages/auditor-backoffice-ui/src/paths/instance/reserves/list/List.stories.tsx new file mode 100644 index 000000000..b070bbde3 --- /dev/null +++ b/packages/auditor-backoffice-ui/src/paths/instance/reserves/list/List.stories.tsx @@ -0,0 +1,96 @@ +/* + This file is part of GNU Taler + (C) 2021-2023 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.js"; + +export default { + title: "Pages/Reserve/List", + component: TestedComponent, +}; + +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/auditor-backoffice-ui/src/paths/instance/reserves/list/Table.tsx b/packages/auditor-backoffice-ui/src/paths/instance/reserves/list/Table.tsx new file mode 100644 index 000000000..795e7ec82 --- /dev/null +++ b/packages/auditor-backoffice-ui/src/paths/instance/reserves/list/Table.tsx @@ -0,0 +1,320 @@ +/* + This file is part of GNU Taler + (C) 2021-2023 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 { useTranslationContext } from "@gnu-taler/web-util/browser"; +import { format } from "date-fns"; +import { Fragment, h, VNode } from "preact"; +import { MerchantBackend, WithId } from "../../../../declaration.js"; +import { datetimeFormatForSettings, useSettings } from "../../../../hooks/useSettings.js"; + +type Entity = MerchantBackend.Rewards.ReserveStatusEntry & WithId; + +interface Props { + instances: Entity[]; + onNewReward: (id: Entity) => void; + onSelect: (id: Entity) => void; + onDelete: (id: Entity) => void; + onCreate: () => void; +} + +export function CardTable({ + instances, + onCreate, + onSelect, + onNewReward, + 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 } = useTranslationContext(); + + return ( + + {withoutFunds.length > 0 && ( +
+
+

+ + + + Reserves not yet funded +

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

+ + + + Reserves ready +

+
+
+ + + +
+
+
+
+
+ {withFunds.length > 0 ? ( + + ) : ( + + )} + + + + + + ); +} +interface TableProps { + instances: Entity[]; + onNewReward: (id: Entity) => void; + onDelete: (id: Entity) => void; + onSelect: (id: Entity) => void; +} + +function Table({ instances, onNewReward, onSelect, onDelete }: TableProps): VNode { + const { i18n } = useTranslationContext(); + const [settings] = useSettings(); + 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, datetimeFormatForSettings(settings))} + onSelect(i)} + style={{ cursor: "pointer" }} + > + {i.expiration_time.t_s === "never" + ? "never" + : format( + i.expiration_time.t_s * 1000, + datetimeFormatForSettings(settings), + )} + 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 { + const { i18n } = useTranslationContext(); + return ( +
+

+ + + +

+

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

+
+ ); +} + +function TableWithoutFund({ + instances, + onSelect, + onDelete, +}: TableProps): VNode { + const { i18n } = useTranslationContext(); + const [settings] = useSettings(); + 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, datetimeFormatForSettings(settings))} + onSelect(i)} + style={{ cursor: "pointer" }} + > + {i.expiration_time.t_s === "never" + ? "never" + : format( + i.expiration_time.t_s * 1000, + datetimeFormatForSettings(settings), + )} + onSelect(i)} + style={{ cursor: "pointer" }} + > + {i.merchant_initial_amount} + +
+ +
+
+
+ ); +} diff --git a/packages/auditor-backoffice-ui/src/paths/instance/reserves/list/index.tsx b/packages/auditor-backoffice-ui/src/paths/instance/reserves/list/index.tsx new file mode 100644 index 000000000..b26ff0000 --- /dev/null +++ b/packages/auditor-backoffice-ui/src/paths/instance/reserves/list/index.tsx @@ -0,0 +1,171 @@ +/* + This file is part of GNU Taler + (C) 2021-2023 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 { + ErrorType, + HttpError, + useTranslationContext, +} from "@gnu-taler/web-util/browser"; +import { h, VNode } from "preact"; +import { useState } from "preact/hooks"; +import { Loading } from "../../../../components/exception/loading.js"; +import { NotificationCard } from "../../../../components/menu/index.js"; +import { MerchantBackend } from "../../../../declaration.js"; +import { + useInstanceReserves, + useReservesAPI, +} from "../../../../hooks/reserves.js"; +import { Notification } from "../../../../utils/types.js"; +import { AuthorizeRewardModal } from "./AutorizeRewardModal.js"; +import { CardTable } from "./Table.js"; +import { HttpStatusCode } from "@gnu-taler/taler-util"; +import { ConfirmModal } from "../../../../components/modal/index.js"; + +interface Props { + onUnauthorized: () => VNode; + onLoadError: (e: HttpError) => VNode; + onSelect: (id: string) => void; + onNotFound: () => VNode; + onCreate: () => void; +} + +interface RewardConfirmation { + response: MerchantBackend.Rewards.RewardCreateConfirmation; + request: MerchantBackend.Rewards.RewardCreateRequest; +} + +export default function ListRewards({ + onUnauthorized, + onLoadError, + onNotFound, + onSelect, + onCreate, +}: Props): VNode { + const result = useInstanceReserves(); + const { deleteReserve, authorizeRewardReserve } = useReservesAPI(); + const [notif, setNotif] = useState(undefined); + const { i18n } = useTranslationContext(); + const [reserveForReward, setReserveForReward] = useState( + undefined, + ); + const [deleting, setDeleting] = + useState(null); + const [rewardAuthorized, setRewardAuthorized] = useState< + RewardConfirmation | undefined + >(undefined); + + if (result.loading) return ; + if (!result.ok) { + if ( + result.type === ErrorType.CLIENT && + result.status === HttpStatusCode.Unauthorized + ) + return onUnauthorized(); + if ( + result.type === ErrorType.CLIENT && + result.status === HttpStatusCode.NotFound + ) + return onNotFound(); + return onLoadError(result); + } + + return ( +
+ + + {reserveForReward && ( + { + setReserveForReward(undefined); + setRewardAuthorized(undefined); + }} + rewardAuthorized={rewardAuthorized} + onConfirm={async (request) => { + try { + const response = await authorizeRewardReserve( + reserveForReward, + request, + ); + setRewardAuthorized({ + request, + response: response.data, + }); + } catch (error) { + setNotif({ + message: i18n.str`could not create the reward`, + type: "ERROR", + description: error instanceof Error ? error.message : undefined, + }); + setReserveForReward(undefined); + } + }} + /> + )} + + r.active) + .map((o) => ({ ...o, id: o.reserve_pub }))} + onCreate={onCreate} + onDelete={(reserve) => { + setDeleting(reserve) + }} + onSelect={(reserve) => onSelect(reserve.id)} + onNewReward={(reserve) => setReserveForReward(reserve.id)} + /> + + {deleting && ( + setDeleting(null)} + onConfirm={async (): Promise => { + try { + await deleteReserve(deleting.reserve_pub); + setNotif({ + message: i18n.str`Reserve for "${deleting.merchant_initial_amount}" (ID: ${deleting.reserve_pub}) has been deleted`, + type: "SUCCESS", + }); + } catch (error) { + setNotif({ + message: i18n.str`Failed to delete reserve`, + type: "ERROR", + description: error instanceof Error ? error.message : undefined, + }); + } + setDeleting(null); + }} + > +

+ If you delete the reserve for "{deleting.merchant_initial_amount}" you won't be able to create more rewards.
+ Reserve ID: {deleting.reserve_pub} +

+

+ Deleting an template cannot be undone. +

+
+ )} + +
+ ); +} -- cgit v1.2.3