diff options
Diffstat (limited to 'packages/merchant-backoffice-ui/src/paths/instance/webhooks/list/index.tsx')
-rw-r--r-- | packages/merchant-backoffice-ui/src/paths/instance/webhooks/list/index.tsx | 72 |
1 files changed, 32 insertions, 40 deletions
diff --git a/packages/merchant-backoffice-ui/src/paths/instance/webhooks/list/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/webhooks/list/index.tsx index 2923a8096..7c24a7228 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/webhooks/list/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/webhooks/list/index.tsx @@ -20,58 +20,51 @@ */ import { - ErrorType, - HttpError, + HttpStatusCode, + TalerError, + TalerMerchantApi, + assertUnreachable, +} from "@gnu-taler/taler-util"; +import { useMerchantApiContext, useTranslationContext, } from "@gnu-taler/web-util/browser"; -import { Fragment, h, VNode } from "preact"; +import { Fragment, VNode, h } from "preact"; import { useState } from "preact/hooks"; +import { ErrorLoadingMerchant } from "../../../../components/ErrorLoadingMerchant.js"; import { Loading } from "../../../../components/exception/loading.js"; import { NotificationCard } from "../../../../components/menu/index.js"; -import { - useInstanceWebhooks, -} from "../../../../hooks/webhooks.js"; +import { useSessionContext } from "../../../../context/session.js"; +import { useInstanceWebhooks } from "../../../../hooks/webhooks.js"; import { Notification } from "../../../../utils/types.js"; +import { NotFoundPageOrAdminCreate } from "../../../notfound/index.js"; import { ListPage } from "./ListPage.js"; -import { HttpStatusCode, TalerErrorDetail, TalerMerchantApi } from "@gnu-taler/taler-util"; -import { useSessionContext } from "../../../../context/session.js"; interface Props { - onUnauthorized: () => VNode; - onLoadError: (error: HttpError<TalerErrorDetail>) => VNode; - onNotFound: () => VNode; onCreate: () => void; onSelect: (id: string) => void; } -export default function ListWebhooks({ - onUnauthorized, - onLoadError, - onCreate, - onSelect, - onNotFound, -}: Props): VNode { - const [position, setPosition] = useState<string | undefined>(undefined); +export default function ListWebhooks({ onCreate, onSelect }: Props): VNode { const { i18n } = useTranslationContext(); const [notif, setNotif] = useState<Notification | undefined>(undefined); const { lib } = useMerchantApiContext(); const { state } = useSessionContext(); - const result = useInstanceWebhooks({ position }, (id) => setPosition(id)); + const result = useInstanceWebhooks(); - 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 />; + } + default: { + assertUnreachable(result.case); + } + } } return ( @@ -79,17 +72,16 @@ export default function ListWebhooks({ <NotificationCard notification={notif} /> <ListPage - webhooks={result.data.webhooks} - onLoadMoreBefore={ - result.isReachingStart ? result.loadMorePrev : undefined - } - onLoadMoreAfter={result.isReachingEnd ? result.loadMore : undefined} + webhooks={result.body} + onLoadMoreBefore={result.isFirstPage ? undefined : result.loadFirst} + onLoadMoreAfter={result.isLastPage ? undefined : result.loadNext} onCreate={onCreate} onSelect={(e) => { onSelect(e.webhook_id); }} onDelete={(e: TalerMerchantApi.WebhookEntry) => { - return lib.management.deleteWebhook(state.token, e.webhook_id) + return lib.management + .deleteWebhook(state.token, e.webhook_id) .then(() => setNotif({ message: i18n.str`webhook delete successfully`, @@ -102,7 +94,7 @@ export default function ListWebhooks({ type: "ERROR", description: error.message, }), - ) + ); }} /> </Fragment> |