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 | |
parent | 7b3157df9746d12f712e6a4604eccc8ed6b7d0c3 (diff) | |
download | wallet-core-04447178039569db4da0fd60c819e9fe1bc1df20.tar.gz wallet-core-04447178039569db4da0fd60c819e9fe1bc1df20.tar.bz2 wallet-core-04447178039569db4da0fd60c819e9fe1bc1df20.zip |
update token families code to allign with new standards
9 files changed, 214 insertions, 189 deletions
diff --git a/packages/merchant-backoffice-ui/src/Routing.tsx b/packages/merchant-backoffice-ui/src/Routing.tsx index 665137415..8ccd559b9 100644 --- a/packages/merchant-backoffice-ui/src/Routing.tsx +++ b/packages/merchant-backoffice-ui/src/Routing.tsx @@ -60,6 +60,9 @@ import TemplateQrPage from "./paths/instance/templates/qr/index.js"; import TemplateUpdatePage from "./paths/instance/templates/update/index.js"; import TemplateUsePage from "./paths/instance/templates/use/index.js"; import TokenPage from "./paths/instance/token/index.js"; +import TokenFamilyCreatePage from "./paths/instance/tokenfamilies/create/index.js"; +import TokenFamilyListPage from "./paths/instance/tokenfamilies/list/index.js"; +import TokenFamilyUpdatePage from "./paths/instance/tokenfamilies/update/index.js"; import TransferCreatePage from "./paths/instance/transfers/create/index.js"; import TransferListPage from "./paths/instance/transfers/list/index.js"; import InstanceUpdatePage, { @@ -106,6 +109,10 @@ export enum InstancePaths { templates_use = "/templates/:tid/use", templates_qr = "/templates/:tid/qr", + token_family_list = "/tokenfamilies", + token_family_update = "/tokenfamilies/:slug/update", + token_family_new = "/tokenfamilies/new", + webhooks_list = "/webhooks", webhooks_update = "/webhooks/:tid/update", webhooks_new = "/webhooks/new", @@ -472,6 +479,45 @@ export function Routing(_p: Props): VNode { route(InstancePaths.transfers_list); }} /> + {/* * + * Token family pages + */} + <Route + path={InstancePaths.token_family_list} + component={TokenFamilyListPage} + // onUnauthorized={LoginPageAccessDenied} + // onLoadError={ServerErrorRedirectTo(InstancePaths.settings)} + onCreate={() => { + route(InstancePaths.token_family_new); + }} + onSelect={(slug: string) => { + route(InstancePaths.token_family_update.replace(":slug", slug)); + }} + // onNotFound={IfAdminCreateDefaultOr(NotFoundPage)} + /> + <Route + path={InstancePaths.token_family_update} + component={TokenFamilyUpdatePage} + // onUnauthorized={LoginPageAccessDenied} + // onLoadError={ServerErrorRedirectTo(InstancePaths.token_family_list)} + onConfirm={() => { + route(InstancePaths.token_family_list); + }} + onBack={() => { + route(InstancePaths.token_family_list); + }} + // onNotFound={IfAdminCreateDefaultOr(NotFoundPage)} + /> + <Route + path={InstancePaths.token_family_new} + component={TokenFamilyCreatePage} + onConfirm={() => { + route(InstancePaths.token_family_list); + }} + onBack={() => { + route(InstancePaths.token_family_list); + }} + /> {/** * Webhooks pages */} diff --git a/packages/merchant-backoffice-ui/src/components/tokenfamily/TokenFamilyForm.tsx b/packages/merchant-backoffice-ui/src/components/tokenfamily/TokenFamilyForm.tsx index 95441b9fa..1492beb48 100644 --- a/packages/merchant-backoffice-ui/src/components/tokenfamily/TokenFamilyForm.tsx +++ b/packages/merchant-backoffice-ui/src/components/tokenfamily/TokenFamilyForm.tsx @@ -23,8 +23,6 @@ import { useTranslationContext } from "@gnu-taler/web-util/browser"; import { h } from "preact"; import { useCallback, useEffect, useState } from "preact/hooks"; import * as yup from "yup"; -import { useBackendContext } from "../../context/backend.js"; -import { MerchantBackend } from "../../declaration.js"; import { TokenFamilyCreateSchema } from "../../schemas/index.js"; import { FormErrors, FormProvider } from "../form/FormProvider.js"; import { Input } from "../form/Input.js"; @@ -32,8 +30,10 @@ import { InputWithAddon } from "../form/InputWithAddon.js"; import { InputDate } from "../form/InputDate.js"; import { InputDuration } from "../form/InputDuration.js"; import { InputSelector } from "../form/InputSelector.js"; +import { useSessionContext } from "../../context/session.js"; +import { TalerMerchantApi } from "@gnu-taler/taler-util"; -type Entity = MerchantBackend.TokenFamilies.TokenFamilyAddDetail; +type Entity = TalerMerchantApi.TokenFamilyCreateRequest; interface Props { onSubscribe: (c?: () => Entity | undefined) => void; @@ -47,7 +47,7 @@ export function TokenFamilyForm({ onSubscribe }: Props) { name: "", description: "", description_i18n: {}, - kind: "discount", + kind: TalerMerchantApi.TokenFamilyKind.Discount, duration: { d_us: "forever" }, valid_after: { t_s: "never" }, valid_before: { t_s: "never" }, @@ -81,7 +81,7 @@ export function TokenFamilyForm({ onSubscribe }: Props) { onSubscribe(hasErrors ? undefined : submit); }, [submit, hasErrors]); - const backend = useBackendContext(); + const { state } = useSessionContext(); const { i18n } = useTranslationContext(); return ( @@ -94,7 +94,7 @@ export function TokenFamilyForm({ onSubscribe }: Props) { > <InputWithAddon<Entity> name="slug" - addonBefore={`${backend.url}/tokenfamily/`} + addonBefore={new URL("tokenfamily/", state.backendUrl.href).href} label={i18n.str`Slug`} tooltip={i18n.str`token family slug to use in URLs (for internal use only)`} /> diff --git a/packages/merchant-backoffice-ui/src/hooks/tokenfamily.ts b/packages/merchant-backoffice-ui/src/hooks/tokenfamily.ts index 0266fe536..62f364972 100644 --- a/packages/merchant-backoffice-ui/src/hooks/tokenfamily.ts +++ b/packages/merchant-backoffice-ui/src/hooks/tokenfamily.ts @@ -13,114 +13,66 @@ 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 { - HttpResponse, - HttpResponseOk, - RequestError, -} from "@gnu-taler/web-util/browser"; import { MerchantBackend } from "../declaration.js"; -import { useBackendInstanceRequest, useMatchMutate } from "./backend.js"; +import { useSessionContext } from "../context/session.js"; // FIX default import https://github.com/microsoft/TypeScript/issues/49189 -import _useSWR, { SWRHook, useSWRConfig } from "swr"; +import _useSWR, { SWRHook } from "swr"; +import { AccessToken, TalerHttpError, TalerMerchantManagementResultByMethod } from "@gnu-taler/taler-util"; const useSWR = _useSWR as unknown as SWRHook; -export interface TokenFamilyAPI { - createTokenFamily: ( - data: MerchantBackend.TokenFamilies.TokenFamilyAddDetail, - ) => Promise<void>; - updateTokenFamily: ( - slug: string, - data: MerchantBackend.TokenFamilies.TokenFamilyPatchDetail, - ) => Promise<void>; - deleteTokenFamily: (slug: string) => Promise<void>; -} - -export function useTokenFamilyAPI(): TokenFamilyAPI { - const mutateAll = useMatchMutate(); - const { mutate } = useSWRConfig(); - - const { request } = useBackendInstanceRequest(); +export function useInstanceTokenFamilies() { + const { state: session, lib: { instance } } = useSessionContext(); - const createTokenFamily = async ( - data: MerchantBackend.TokenFamilies.TokenFamilyAddDetail, - ): Promise<void> => { - const res = await request(`/private/tokenfamilies`, { - method: "POST", - data, - }); + // const [offset, setOffset] = useState<number | undefined>(); - return await mutateAll(/.*"\/private\/tokenfamilies.*/); - }; - - const updateTokenFamily = async ( - tokenFamilySlug: string, - data: MerchantBackend.TokenFamilies.TokenFamilyPatchDetail, - ): Promise<void> => { - const r = await request(`/private/tokenfamilies/${tokenFamilySlug}`, { - method: "PATCH", - data, + async function fetcher([token, bid]: [AccessToken, number]) { + return await instance.listTokenFamilies(token, { + // limit: PAGINATED_LIST_REQUEST, + // offset: bid === undefined ? undefined: String(bid), + // order: "dec", }); + } - return await mutateAll(/.*"\/private\/tokenfamilies.*/); - }; + const { data, error } = useSWR< + TalerMerchantManagementResultByMethod<"listTokenFamilies">, + TalerHttpError + >([session.token, "offset", "listTokenFamilies"], fetcher); - const deleteTokenFamily = async (tokenFamilySlug: string): Promise<void> => { - await request(`/private/tokenfamilies/${tokenFamilySlug}`, { - method: "DELETE", - }); - await mutate([`/private/tokenfamilies`]); - }; + if (error) return error; + if (data === undefined) return undefined; + if (data.type !== "ok") return data; - return { createTokenFamily, updateTokenFamily, deleteTokenFamily }; + return data; } -export function useInstanceTokenFamilies(): HttpResponse< - (MerchantBackend.TokenFamilies.TokenFamilyEntry)[], - MerchantBackend.ErrorDetail -> { - const { fetcher, multiFetcher } = useBackendInstanceRequest(); - - const { data: list, error: listError } = useSWR< - HttpResponseOk<MerchantBackend.TokenFamilies.TokenFamilySummaryResponse>, - RequestError<MerchantBackend.ErrorDetail> - >([`/private/tokenfamilies`], fetcher, { - refreshInterval: 0, - refreshWhenHidden: false, - revalidateOnFocus: false, - revalidateOnReconnect: false, - refreshWhenOffline: false, - }); - - if (listError) return listError.cause; - - if (list) { - return { ok: true, data: list.data.token_families }; +export function useTokenFamilyDetails(tokenFamilySlug: string) { + const { state: session } = useSessionContext(); + const { lib: { instance } } = useSessionContext(); + + async function fetcher([slug, token]: [string, AccessToken]) { + return await instance.getTokenFamilyDetails(token, slug); } - return { loading: true }; + + const { data, error } = useSWR< + TalerMerchantManagementResultByMethod<"getTokenFamilyDetails">, + TalerHttpError + >([tokenFamilySlug, session.token, "getTokenFamilyDetails"], fetcher); + + if (error) return error; + if (data === undefined) return undefined; + if (data.type !== "ok") return data; + + return data; } -export function useTokenFamilyDetails( - tokenFamilySlug: string, -): HttpResponse< - MerchantBackend.TokenFamilies.TokenFamilyDetail, - MerchantBackend.ErrorDetail -> { - const { fetcher } = useBackendInstanceRequest(); - - const { data, error, isValidating } = useSWR< - HttpResponseOk<MerchantBackend.TokenFamilies.TokenFamilyDetail>, - RequestError<MerchantBackend.ErrorDetail> - >([`/private/tokenfamilies/${tokenFamilySlug}`], fetcher, { - refreshInterval: 0, - refreshWhenHidden: false, - revalidateOnFocus: false, - revalidateOnReconnect: false, - refreshWhenOffline: false, - }); - - if (isValidating) return { loading: true, data: data?.data }; - if (data) return data; - if (error) return error.cause; - return { loading: true }; +export interface TokenFamilyAPI { + createTokenFamily: ( + data: MerchantBackend.TokenFamilies.TokenFamilyAddDetail, + ) => Promise<void>; + updateTokenFamily: ( + slug: string, + data: MerchantBackend.TokenFamilies.TokenFamilyPatchDetail, + ) => Promise<void>; + deleteTokenFamily: (slug: string) => Promise<void>; } 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({ |