From 04447178039569db4da0fd60c819e9fe1bc1df20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ttler?= Date: Tue, 7 May 2024 18:12:20 +0200 Subject: update token families code to allign with new standards --- .../instance/tokenfamilies/create/CreatePage.tsx | 5 +- .../paths/instance/tokenfamilies/create/index.tsx | 7 +- .../paths/instance/tokenfamilies/list/Table.tsx | 3 +- .../paths/instance/tokenfamilies/list/index.tsx | 120 +++++++++++++-------- .../instance/tokenfamilies/update/UpdatePage.tsx | 7 +- .../paths/instance/tokenfamilies/update/index.tsx | 63 ++++++----- 6 files changed, 116 insertions(+), 89 deletions(-) (limited to 'packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies') 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; 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(undefined); const { i18n } = useTranslationContext(); + const { lib } = useSessionContext(); + const { state } = useSessionContext(); return ( @@ -44,7 +45,7 @@ export default function CreateTokenFamily({ onConfirm, onBack }: Props): VNode { { - 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(undefined); + const { lib, state } = useSessionContext(); + const [deleting, setDeleting] = + useState(null); const { i18n } = useTranslationContext(); - 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); + if (!result) return ; + if (result instanceof TalerError) { + return ; + } + if (result.type === "fail") { + switch (result.case) { + case HttpStatusCode.NotFound: { + return ; + } + case HttpStatusCode.Unauthorized: { + return + } + default: { + assertUnreachable(result); + } + } } return ( @@ -77,42 +86,61 @@ export default function TokenFamilyList({ - 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 && ( + setDeleting(null)} + onConfirm={async (): Promise => { + 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); + }} + > +

+ If you delete the "{deleting.name}" token family (Slug:{" "} + {deleting.slug}), all issued tokens will become invalid. +

+

+ Deleting a token family cannot be undone. +

+
+ )} ); } 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; @@ -71,7 +71,6 @@ export function UpdatePage({ onUpdate, onBack, tokenFamily }: Props) { } const { i18n } = useTranslationContext(); - const { url: backendURL } = useBackendContext() return (
@@ -82,7 +81,7 @@ export function UpdatePage({ onUpdate, onBack, tokenFamily }: Props) {
- {backendURL}/tokenfamilies/{tokenFamily.id} + Token Family: {tokenFamily.name}
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) => 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(undefined); + const { lib, state } = useSessionContext(); const { i18n } = useTranslationContext(); - 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); + if (!result) return ; + if (result instanceof TalerError) { + return ; + } + if (result.type === "fail") { + switch (result.case) { + case HttpStatusCode.NotFound: { + return ; + } + case HttpStatusCode.Unauthorized: { + return + } + 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({ -- cgit v1.2.3