aboutsummaryrefslogtreecommitdiff
path: root/packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/list/index.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/list/index.tsx')
-rw-r--r--packages/merchant-backoffice-ui/src/paths/instance/tokenfamilies/list/index.tsx120
1 files changed, 74 insertions, 46 deletions
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>&quot;{deleting.name}&quot;</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>
);
}