diff options
Diffstat (limited to 'packages/auditor-backoffice-ui/src/paths/instance/reserves/details')
4 files changed, 554 insertions, 0 deletions
diff --git a/packages/auditor-backoffice-ui/src/paths/instance/reserves/details/DetailPage.tsx b/packages/auditor-backoffice-ui/src/paths/instance/reserves/details/DetailPage.tsx new file mode 100644 index 000000000..d8840eeac --- /dev/null +++ b/packages/auditor-backoffice-ui/src/paths/instance/reserves/details/DetailPage.tsx @@ -0,0 +1,266 @@ +/* + 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 <http://www.gnu.org/licenses/> + */ + +/** + * + * @author Sebastian Javier Marchano (sebasjm) + */ + +import { + Amounts, + parsePaytoUri, + stringifyPaytoUri, +} from "@gnu-taler/taler-util"; +import { useTranslationContext } from "@gnu-taler/web-util/browser"; +import { format } from "date-fns"; +import { Fragment, h, VNode } from "preact"; +import { useState } from "preact/hooks"; +import { QR } from "../../../../components/exception/QR.js"; +import { FormProvider } from "../../../../components/form/FormProvider.js"; +import { Input } from "../../../../components/form/Input.js"; +import { InputCurrency } from "../../../../components/form/InputCurrency.js"; +import { InputDate } from "../../../../components/form/InputDate.js"; +import { TextField } from "../../../../components/form/TextField.js"; +import { SimpleModal } from "../../../../components/modal/index.js"; +import { MerchantBackend } from "../../../../declaration.js"; +import { useRewardDetails } from "../../../../hooks/reserves.js"; +import { RewardInfo } from "./RewardInfo.js"; +import { ShowAccountsOfReserveAsQRWithLink } from "../create/CreatedSuccessfully.js"; +import { datetimeFormatForSettings, useSettings } from "../../../../hooks/useSettings.js"; + +type Entity = MerchantBackend.Rewards.ReserveDetail; +type CT = MerchantBackend.ContractTerms; + +interface Props { + onBack: () => void; + selected: Entity; + id: string; +} + +export function DetailPage({ id, selected, onBack }: Props): VNode { + const { i18n } = useTranslationContext(); + const didExchangeAckTransfer = Amounts.isNonZero( + Amounts.parseOrThrow(selected.exchange_initial_amount), + ); + + return ( + <div class="columns"> + <div class="column" /> + <div class="column is-four-fifths"> + <div class="section main-section"> + <FormProvider object={{ ...selected, id }} valueHandler={null}> + <InputDate<Entity> + name="creation_time" + label={i18n.str`Created at`} + readonly + /> + <InputDate<Entity> + name="expiration_time" + label={i18n.str`Valid until`} + readonly + /> + <InputCurrency<Entity> + name="merchant_initial_amount" + label={i18n.str`Created balance`} + readonly + /> + <TextField<Entity> + name="exchange_url" + label={i18n.str`Exchange URL`} + readonly + > + <a target="_blank" rel="noreferrer" href={selected.exchange_url}> + {selected.exchange_url} + </a> + </TextField> + + {didExchangeAckTransfer && ( + <Fragment> + <InputCurrency<Entity> + name="exchange_initial_amount" + label={i18n.str`Exchange balance`} + readonly + /> + <InputCurrency<Entity> + name="pickup_amount" + label={i18n.str`Picked up`} + readonly + /> + <InputCurrency<Entity> + name="committed_amount" + label={i18n.str`Committed`} + readonly + /> + </Fragment> + )} + <Input name="id" label={i18n.str`Subject`} readonly /> + </FormProvider> + + {didExchangeAckTransfer ? ( + <Fragment> + <div class="card has-table"> + <header class="card-header"> + <p class="card-header-title"> + <span class="icon"> + <i class="mdi mdi-cash-register" /> + </span> + <i18n.Translate>Rewards</i18n.Translate> + </p> + </header> + <div class="card-content"> + <div class="b-table has-pagination"> + <div class="table-wrapper has-mobile-cards"> + {selected.rewards && selected.rewards.length > 0 ? ( + <Table rewards={selected.rewards} /> + ) : ( + <EmptyTable /> + )} + </div> + </div> + </div> + </div> + </Fragment> + ) : selected.accounts ? ( + <ShowAccountsOfReserveAsQRWithLink + accounts={selected.accounts} + amount={selected.merchant_initial_amount} + message={id} + /> + ) : undefined} + + <div class="buttons is-right mt-5"> + <button class="button" onClick={onBack}> + <i18n.Translate>Back</i18n.Translate> + </button> + </div> + </div> + </div> + <div class="column" /> + </div> + ); +} + +function EmptyTable(): VNode { + const { i18n } = useTranslationContext(); + return ( + <div class="content has-text-grey has-text-centered"> + <p> + <span class="icon is-large"> + <i class="mdi mdi-emoticon-sad mdi-48px" /> + </span> + </p> + <p> + <i18n.Translate> + No reward has been authorized from this reserve + </i18n.Translate> + </p> + </div> + ); +} + +interface TableProps { + rewards: MerchantBackend.Rewards.RewardStatusEntry[]; +} + +function Table({ rewards }: TableProps): VNode { + const { i18n } = useTranslationContext(); + return ( + <div class="table-container"> + <table class="table is-fullwidth is-striped is-hoverable is-fullwidth"> + <thead> + <tr> + <th> + <i18n.Translate>Authorized</i18n.Translate> + </th> + <th> + <i18n.Translate>Picked up</i18n.Translate> + </th> + <th> + <i18n.Translate>Reason</i18n.Translate> + </th> + <th> + <i18n.Translate>Expiration</i18n.Translate> + </th> + </tr> + </thead> + <tbody> + {rewards.map((t, i) => { + return <RewardRow id={t.reward_id} key={i} entry={t} />; + })} + </tbody> + </table> + </div> + ); +} + +function RewardRow({ + id, + entry, +}: { + id: string; + entry: MerchantBackend.Rewards.RewardStatusEntry; +}) { + const [selected, setSelected] = useState(false); + const result = useRewardDetails(id); + const [settings] = useSettings(); + if (result.loading) { + return ( + <tr> + <td>...</td> + <td>...</td> + <td>...</td> + <td>...</td> + </tr> + ); + } + if (!result.ok) { + return ( + <tr> + <td>...</td> {/* authorized */} + <td>{entry.total_amount}</td> + <td>{entry.reason}</td> + <td>...</td> {/* expired */} + </tr> + ); + } + const info = result.data; + function onSelect() { + setSelected(true); + } + return ( + <Fragment> + {selected && ( + <SimpleModal + description="reward" + active + onCancel={() => setSelected(false)} + > + <RewardInfo id={id} amount={info.total_authorized} entity={info} /> + </SimpleModal> + )} + <tr> + <td onClick={onSelect}>{info.total_authorized}</td> + <td onClick={onSelect}>{info.total_picked_up}</td> + <td onClick={onSelect}>{info.reason}</td> + <td onClick={onSelect}> + {info.expiration.t_s === "never" + ? "never" + : format(info.expiration.t_s * 1000, datetimeFormatForSettings(settings))} + </td> + </tr> + </Fragment> + ); +} diff --git a/packages/auditor-backoffice-ui/src/paths/instance/reserves/details/Details.stories.tsx b/packages/auditor-backoffice-ui/src/paths/instance/reserves/details/Details.stories.tsx new file mode 100644 index 000000000..41c715f20 --- /dev/null +++ b/packages/auditor-backoffice-ui/src/paths/instance/reserves/details/Details.stories.tsx @@ -0,0 +1,126 @@ +/* + 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 <http://www.gnu.org/licenses/> + */ + +/** + * + * @author Sebastian Javier Marchano (sebasjm) + */ + +import { h, VNode, FunctionalComponent } from "preact"; +import { DetailPage as TestedComponent } from "./DetailPage.js"; + +export default { + title: "Pages/Reserve/Detail", + component: TestedComponent, + argTypes: { + onUpdate: { action: "onUpdate" }, + onBack: { action: "onBack" }, + }, +}; + +function createExample<Props>( + Component: FunctionalComponent<Props>, + props: Partial<Props>, +) { + const r = (args: any) => <Component {...args} />; + 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", + accounts: [ + { + payto_uri: "payto://x-taler-bank/bank.taler:8080/account", + credit_restrictions: [], + debit_restrictions: [], + master_sig: "", + }, + ], + 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", + accounts: [ + { + payto_uri: "payto://x-taler-bank/bank.taler:8080/account", + credit_restrictions: [], + debit_restrictions: [], + master_sig: "", + }, + ], + exchange_url: "http://exchange.taler/", + }, +}); + +export const FundedWithEmptyRewards = 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", + accounts: [ + { + payto_uri: "payto://x-taler-bank/bank.taler:8080/account", + credit_restrictions: [], + debit_restrictions: [], + master_sig: "", + }, + ], + exchange_url: "http://exchange.taler/", + rewards: [ + { + reason: "asdasd", + reward_id: "123", + total_amount: "TESTKUDOS:1", + }, + ], + }, +}); diff --git a/packages/auditor-backoffice-ui/src/paths/instance/reserves/details/RewardInfo.tsx b/packages/auditor-backoffice-ui/src/paths/instance/reserves/details/RewardInfo.tsx new file mode 100644 index 000000000..491028695 --- /dev/null +++ b/packages/auditor-backoffice-ui/src/paths/instance/reserves/details/RewardInfo.tsx @@ -0,0 +1,94 @@ +/* + 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 <http://www.gnu.org/licenses/> + */ +import { format } from "date-fns"; +import { Fragment, h, VNode } from "preact"; +import { useBackendContext } from "../../../../context/backend.js"; +import { MerchantBackend } from "../../../../declaration.js"; +import { + datetimeFormatForSettings, + useSettings, +} from "../../../../hooks/useSettings.js"; + +type Entity = MerchantBackend.Rewards.RewardDetails; + +interface Props { + id: string; + entity: Entity; + amount: string; +} + +export function RewardInfo({ + id: merchantRewardId, + amount, + entity, +}: Props): VNode { + const { url: backendURL } = useBackendContext(); + const [settings] = useSettings(); + const rewardURL = "not-supported"; + return ( + <Fragment> + <div class="field is-horizontal"> + <div class="field-label is-normal"> + <label class="label">Amount</label> + </div> + <div class="field-body is-flex-grow-3"> + <div class="field"> + <p class="control"> + <input readonly class="input" value={amount} /> + </p> + </div> + </div> + </div> + <div class="field is-horizontal"> + <div class="field-label is-normal"> + <label class="label">URL</label> + </div> + <div class="field-body is-flex-grow-3"> + <div class="field" style={{ overflowWrap: "anywhere" }}> + <p class="control"> + <a target="_blank" rel="noreferrer" href={rewardURL}> + {rewardURL} + </a> + </p> + </div> + </div> + </div> + <div class="field is-horizontal"> + <div class="field-label is-normal"> + <label class="label">Valid until</label> + </div> + <div class="field-body is-flex-grow-3"> + <div class="field"> + <p class="control"> + <input + class="input" + readonly + value={ + !entity.expiration || entity.expiration.t_s === "never" + ? "never" + : format( + entity.expiration.t_s * 1000, + datetimeFormatForSettings(settings), + ) + } + /> + </p> + </div> + </div> + </div> + </Fragment> + ); +} diff --git a/packages/auditor-backoffice-ui/src/paths/instance/reserves/details/index.tsx b/packages/auditor-backoffice-ui/src/paths/instance/reserves/details/index.tsx new file mode 100644 index 000000000..8e2a74529 --- /dev/null +++ b/packages/auditor-backoffice-ui/src/paths/instance/reserves/details/index.tsx @@ -0,0 +1,68 @@ +/* + 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 <http://www.gnu.org/licenses/> + */ + +/** + * + * @author Sebastian Javier Marchano (sebasjm) + */ + +import { ErrorType, HttpError } from "@gnu-taler/web-util/browser"; +import { Fragment, h, VNode } from "preact"; +import { Loading } from "../../../../components/exception/loading.js"; +import { MerchantBackend } from "../../../../declaration.js"; +import { useReserveDetails } from "../../../../hooks/reserves.js"; +import { DetailPage } from "./DetailPage.js"; +import { HttpStatusCode } from "@gnu-taler/taler-util"; + +interface Props { + rid: string; + + onUnauthorized: () => VNode; + onLoadError: (error: HttpError<MerchantBackend.ErrorDetail>) => 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.loading) return <Loading />; + 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 ( + <Fragment> + <DetailPage selected={result.data} onBack={onBack} id={rid} /> + </Fragment> + ); +} |