diff options
author | Christian Blättler <blatc2@bfh.ch> | 2024-05-07 18:12:20 +0200 |
---|---|---|
committer | Christian Blättler <blatc2@bfh.ch> | 2024-05-07 18:12:20 +0200 |
commit | 04447178039569db4da0fd60c819e9fe1bc1df20 (patch) | |
tree | 2a93ecf076ddc63218cb676fb7cea1dd9ad31881 /packages/merchant-backoffice-ui/src/paths | |
parent | 7b3157df9746d12f712e6a4604eccc8ed6b7d0c3 (diff) | |
download | wallet-core-04447178039569db4da0fd60c819e9fe1bc1df20.tar.xz |
update token families code to allign with new standards
Diffstat (limited to 'packages/merchant-backoffice-ui/src/paths')
6 files changed, 116 insertions, 89 deletions
diff --git a/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/create/CreatePage.tsx b/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/create/CreatePage.tsx index 7ae9bebe6..cec1f3426 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/create/CreatePage.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/create/CreatePage.tsx @@ -22,12 +22,11 @@ import { useTranslationContext } from "@gnu-taler/web-util/browser"; import { h, VNode } from "preact"; import { AsyncButton } from "../../../../components/exception/AsyncButton.js"; -import { MerchantBackend } from "../../../../declaration.js"; import { useListener } from "../../../../hooks/listener.js"; import { TokenFamilyForm } from "../../../../components/tokenfamily/TokenFamilyForm.js"; -// import { Test } from "../../../../components/tokenfamily/Test.js"; +import { TalerMerchantApi } from "@gnu-taler/taler-util"; -type Entity = MerchantBackend.TokenFamilies.TokenFamilyAddDetail; +type Entity = TalerMerchantApi.TokenFamilyCreateRequest; interface Props { onCreate: (d: Entity) => Promise<void>; diff --git a/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/create/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/create/index.tsx index 0beef4c45..deee7d0d5 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/create/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/create/index.tsx @@ -25,8 +25,8 @@ import { useState } from "preact/hooks"; import { NotificationCard } from "../../../../components/menu/index.js"; import { MerchantBackend } from "../../../../declaration.js"; import { Notification } from "../../../../utils/types.js"; +import { useSessionContext } from "../../../../context/session.js"; import { CreatePage } from "./CreatePage.js"; -import { useTokenFamilyAPI } from "../../../../hooks/tokenfamily.js"; export type Entity = MerchantBackend.TokenFamilies.TokenFamilyAddDetail; interface Props { @@ -34,9 +34,10 @@ interface Props { onConfirm: () => void; } export default function CreateTokenFamily({ onConfirm, onBack }: Props): VNode { - const { createTokenFamily } = useTokenFamilyAPI(); const [notif, setNotif] = useState<Notification | undefined>(undefined); const { i18n } = useTranslationContext(); + const { lib } = useSessionContext(); + const { state } = useSessionContext(); return ( <Fragment> @@ -44,7 +45,7 @@ export default function CreateTokenFamily({ onConfirm, onBack }: Props): VNode { <CreatePage onBack={onBack} onCreate={(request) => { - return createTokenFamily(request) + return lib.instance.createTokenFamily(state.token, request) .then(() => onConfirm()) .catch((error) => { setNotif({ diff --git a/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/list/Table.tsx b/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/list/Table.tsx index 3d9bf9018..b5ca03cfd 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/list/Table.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/list/Table.tsx @@ -24,8 +24,9 @@ import { Fragment, h, VNode } from "preact"; import { StateUpdater, useState } from "preact/hooks"; import { format } from "date-fns"; import { MerchantBackend } from "../../../../declaration.js"; +import { TalerMerchantApi } from "@gnu-taler/taler-util"; -type Entity = MerchantBackend.TokenFamilies.TokenFamilyEntry; +type Entity = TalerMerchantApi.TokenFamilySummary; interface Props { instances: Entity[]; diff --git a/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/list/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/list/index.tsx index a5d7413c0..5531174e0 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/list/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/list/index.tsx @@ -31,11 +31,15 @@ import { NotificationCard } from "../../../../components/menu/index.js"; import { MerchantBackend } from "../../../../declaration.js"; import { useInstanceTokenFamilies, - useTokenFamilyAPI, } from "../../../../hooks/tokenfamily.js"; import { Notification } from "../../../../utils/types.js"; import { CardTable } from "./Table.js"; -import { HttpStatusCode } from "@gnu-taler/taler-util"; +import { HttpStatusCode, TalerError, TalerMerchantApi, assertUnreachable } from "@gnu-taler/taler-util"; +import { useSessionContext } from "../../../../context/session.js"; +import { ErrorLoadingMerchant } from "../../../../components/ErrorLoadingMerchant.js"; +import { ConfirmModal } from "../../../../components/modal/index.js"; +import { LoginPage } from "../../../login/index.js"; +import { NotFoundPageOrAdminCreate } from "../../../notfound/index.js"; interface Props { onUnauthorized: () => VNode; @@ -52,24 +56,29 @@ export default function TokenFamilyList({ onNotFound, }: Props): VNode { const result = useInstanceTokenFamilies(); - const { deleteTokenFamily, updateTokenFamily } = useTokenFamilyAPI(); const [notif, setNotif] = useState<Notification | undefined>(undefined); + const { lib, state } = useSessionContext(); + const [deleting, setDeleting] = + useState<TalerMerchantApi.TokenFamilySummary | null>(null); const { i18n } = useTranslationContext(); - 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); + if (!result) return <Loading />; + if (result instanceof TalerError) { + return <ErrorLoadingMerchant error={result} />; + } + if (result.type === "fail") { + switch (result.case) { + case HttpStatusCode.NotFound: { + return <NotFoundPageOrAdminCreate />; + } + case HttpStatusCode.Unauthorized: { + return <LoginPage /> + } + default: { + assertUnreachable(result); + } + } } return ( @@ -77,42 +86,61 @@ export default function TokenFamilyList({ <NotificationCard notification={notif} /> <CardTable - instances={result.data} + instances={result.body.token_families} onCreate={onCreate} - onUpdate={(slug, fam) => - updateTokenFamily(slug, fam) - .then(() => - setNotif({ - message: i18n.str`token family updated successfully`, - type: "SUCCESS", - }), - ) - .catch((error) => - setNotif({ - message: i18n.str`could not update the token family`, - type: "ERROR", - description: error.message, - }), - ) - } + onUpdate={async (slug, fam) => { + try { + await lib.instance.updateTokenFamily(state.token, slug, fam); + setNotif({ + message: i18n.str`token family updated successfully`, + type: "SUCCESS", + }); + } catch (error) { + setNotif({ + message: i18n.str`could not update the token family`, + type: "ERROR", + description: error instanceof Error ? error.message : undefined, + }); + } + return; + }} onSelect={(tokenFamily) => onSelect(tokenFamily.slug)} - onDelete={(fam) => - deleteTokenFamily(fam.slug) - .then(() => + onDelete={(fam) => setDeleting(fam)} + /> + + {deleting && ( + <ConfirmModal + label={`Delete token family`} + description={`Delete the token family "${deleting.name}"`} + danger + active + onCancel={() => setDeleting(null)} + onConfirm={async (): Promise<void> => { + try { + await lib.instance.deleteTokenFamily(state.token, deleting.slug); setNotif({ - message: i18n.str`token family delete successfully`, + message: i18n.str`Token family "${deleting.name}" (SLUG: ${deleting.slug}) has been deleted`, type: "SUCCESS", - }), - ) - .catch((error) => + }); + } catch (error) { setNotif({ - message: i18n.str`could not delete the token family`, + message: i18n.str`Failed to delete token family`, type: "ERROR", - description: error.message, - }), - ) - } - /> + description: error instanceof Error ? error.message : undefined, + }); + } + setDeleting(null); + }} + > + <p> + If you delete the <b>"{deleting.name}"</b> token family (Slug:{" "} + <b>{deleting.slug}</b>), all issued tokens will become invalid. + </p> + <p class="warning"> + Deleting a token family <b>cannot be undone</b>. + </p> + </ConfirmModal> + )} </section> ); } diff --git a/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/update/UpdatePage.tsx b/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/update/UpdatePage.tsx index 3735645bd..7ad18c01b 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/update/UpdatePage.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/update/UpdatePage.tsx @@ -25,14 +25,14 @@ import { useState } from "preact/hooks"; import * as yup from "yup"; import { MerchantBackend, WithId } from "../../../../declaration.js"; import { TokenFamilyUpdateSchema } from "../../../../schemas/index.js"; -import { useBackendContext } from "../../../../context/backend.js"; import { FormErrors, FormProvider } from "../../../../components/form/FormProvider.js"; import { Input } from "../../../../components/form/Input.js"; import { InputDate } from "../../../../components/form/InputDate.js"; import { InputDuration } from "../../../../components/form/InputDuration.js"; import { AsyncButton } from "../../../../components/exception/AsyncButton.js"; +import { TalerMerchantApi } from "@gnu-taler/taler-util"; -type Entity = MerchantBackend.TokenFamilies.TokenFamilyPatchDetail & WithId; +type Entity = TalerMerchantApi.TokenFamilyUpdateRequest; interface Props { onUpdate: (d: Entity) => Promise<void>; @@ -71,7 +71,6 @@ export function UpdatePage({ onUpdate, onBack, tokenFamily }: Props) { } const { i18n } = useTranslationContext(); - const { url: backendURL } = useBackendContext() return ( <div> @@ -82,7 +81,7 @@ export function UpdatePage({ onUpdate, onBack, tokenFamily }: Props) { <div class="level-left"> <div class="level-item"> <span class="is-size-4"> - {backendURL}/tokenfamilies/{tokenFamily.id} + Token Family: <b>{tokenFamily.name}</b> </span> </div> </div> diff --git a/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/update/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/update/index.tsx index 4f582d7f3..068235e14 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/update/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/update/index.tsx @@ -28,59 +28,58 @@ import { Fragment, 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, WithId } from "../../../../declaration.js"; import { Notification } from "../../../../utils/types.js"; import { UpdatePage } from "./UpdatePage.js"; -import { HttpStatusCode } from "@gnu-taler/taler-util"; -import { useTokenFamilyAPI, useTokenFamilyDetails } from "../../../../hooks/tokenfamily.js"; +import { HttpStatusCode, TalerError, TalerMerchantApi, assertUnreachable } from "@gnu-taler/taler-util"; +import { useTokenFamilyDetails } from "../../../../hooks/tokenfamily.js"; +import { useSessionContext } from "../../../../context/session.js"; +import { ErrorLoadingMerchant } from "../../../../components/ErrorLoadingMerchant.js"; +import { LoginPage } from "../../../login/index.js"; +import { NotFoundPageOrAdminCreate } from "../../../notfound/index.js"; -export type Entity = MerchantBackend.TokenFamilies.TokenFamilyPatchDetail & WithId; +type Entity = TalerMerchantApi.TokenFamilyUpdateRequest; interface Props { onBack?: () => void; onConfirm: () => void; - onUnauthorized: () => VNode; - onNotFound: () => VNode; - onLoadError: (e: HttpError<MerchantBackend.ErrorDetail>) => VNode; slug: string; } export default function UpdateTokenFamily({ slug, onConfirm, onBack, - onUnauthorized, - onNotFound, - onLoadError, }: Props): VNode { - const { updateTokenFamily } = useTokenFamilyAPI(); const result = useTokenFamilyDetails(slug); const [notif, setNotif] = useState<Notification | undefined>(undefined); + const { lib, state } = useSessionContext(); const { i18n } = useTranslationContext(); - 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); + if (!result) return <Loading />; + if (result instanceof TalerError) { + return <ErrorLoadingMerchant error={result} />; + } + if (result.type === "fail") { + switch (result.case) { + case HttpStatusCode.NotFound: { + return <NotFoundPageOrAdminCreate />; + } + case HttpStatusCode.Unauthorized: { + return <LoginPage /> + } + default: { + assertUnreachable(result); + } + } } const family: Entity = { - id: slug, - name: result.data.name, - description: result.data.description, - description_i18n: result.data.description_i18n || {}, - duration: result.data.duration, - valid_after: result.data.valid_after, - valid_before: result.data.valid_before, + name: result.body.name, + description: result.body.description, + description_i18n: result.body.description_i18n || {}, + duration: result.body.duration, + valid_after: result.body.valid_after, + valid_before: result.body.valid_before, }; return ( @@ -90,7 +89,7 @@ export default function UpdateTokenFamily({ tokenFamily={family} onBack={onBack} onUpdate={(data) => { - return updateTokenFamily(slug, data) + return lib.instance.updateTokenFamily(state.token, slug, data) .then(onConfirm) .catch((error) => { setNotif({ |