diff options
Diffstat (limited to 'packages/merchant-backoffice-ui/src/paths/instance/products')
3 files changed, 37 insertions, 28 deletions
diff --git a/packages/merchant-backoffice-ui/src/paths/instance/products/create/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/products/create/index.tsx index 9935a9625..6cb083025 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/products/create/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/products/create/index.tsx @@ -20,13 +20,13 @@ */ import { TalerMerchantApi } from "@gnu-taler/taler-util"; -import { useTranslationContext } from "@gnu-taler/web-util/browser"; +import { useMerchantApiContext, useTranslationContext } from "@gnu-taler/web-util/browser"; import { Fragment, h, VNode } from "preact"; import { useState } from "preact/hooks"; import { NotificationCard } from "../../../../components/menu/index.js"; -import { useProductAPI } from "../../../../hooks/product.js"; import { Notification } from "../../../../utils/types.js"; import { CreatePage } from "./CreatePage.js"; +import { useSessionContext } from "../../../../context/session.js"; export type Entity = TalerMerchantApi.ProductAddDetail; interface Props { @@ -34,7 +34,8 @@ interface Props { onConfirm: () => void; } export default function CreateProduct({ onConfirm, onBack }: Props): VNode { - const { createProduct } = useProductAPI(); + const { lib } = useMerchantApiContext(); + const { state } = useSessionContext(); const [notif, setNotif] = useState<Notification | undefined>(undefined); const { i18n } = useTranslationContext(); @@ -44,7 +45,7 @@ export default function CreateProduct({ onConfirm, onBack }: Props): VNode { <CreatePage onBack={onBack} onCreate={(request: TalerMerchantApi.ProductAddDetail) => { - return createProduct(request) + return lib.management.addProduct(state.token, request) .then(() => onConfirm()) .catch((error) => { setNotif({ diff --git a/packages/merchant-backoffice-ui/src/paths/instance/products/list/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/products/list/index.tsx index 1017a9334..dc2e08d91 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/products/list/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/products/list/index.tsx @@ -23,6 +23,7 @@ import { HttpStatusCode, TalerErrorDetail, TalerMerchantApi } from "@gnu-taler/t import { ErrorType, HttpError, + useMerchantApiContext, useTranslationContext, } from "@gnu-taler/web-util/browser"; import { VNode, h } from "preact"; @@ -31,9 +32,9 @@ import { Loading } from "../../../../components/exception/loading.js"; import { JumpToElementById } from "../../../../components/form/JumpToElementById.js"; import { NotificationCard } from "../../../../components/menu/index.js"; import { ConfirmModal } from "../../../../components/modal/index.js"; +import { useSessionContext } from "../../../../context/session.js"; import { - useInstanceProducts, - useProductAPI, + useInstanceProducts } from "../../../../hooks/product.js"; import { Notification } from "../../../../utils/types.js"; import { CardTable } from "./Table.js"; @@ -53,7 +54,8 @@ export default function ProductList({ onNotFound, }: Props): VNode { const result = useInstanceProducts(); - const { deleteProduct, updateProduct, getProduct } = useProductAPI(); + const { lib } = useMerchantApiContext(); + const { state } = useSessionContext(); const [deleting, setDeleting] = useState<TalerMerchantApi.ProductDetail & WithId | null>(null); const [notif, setNotif] = useState<Notification | undefined>(undefined); @@ -80,7 +82,10 @@ export default function ProductList({ <NotificationCard notification={notif} /> <JumpToElementById - testIfExist={getProduct} + testIfExist={async (id) => { + const resp = await lib.management.getProduct(state.token, id); + return resp.type === "ok"; + }} onSelect={onSelect} description={i18n.str`jump to product with the given product ID`} placeholder={i18n.str`product id`} @@ -89,22 +94,22 @@ export default function ProductList({ <CardTable instances={result.data} onCreate={onCreate} - onUpdate={(id, prod) => - updateProduct(id, prod) - .then(() => - setNotif({ - message: i18n.str`product updated successfully`, - type: "SUCCESS", - }), - ) - .catch((error) => - setNotif({ - message: i18n.str`could not update the product`, - type: "ERROR", - description: error.message, - }), - ) - } + onUpdate={async (id, prod) => { + try { + await lib.management.updateProduct(state.token, id, prod); + setNotif({ + message: i18n.str`product updated successfully`, + type: "SUCCESS", + }); + } catch (error) { + setNotif({ + message: i18n.str`could not update the product`, + type: "ERROR", + description: error instanceof Error ? error.message : undefined, + }); + } + return + }} onSelect={(product) => onSelect(product.id)} onDelete={(prod: TalerMerchantApi.ProductDetail & WithId) => setDeleting(prod) @@ -120,7 +125,7 @@ export default function ProductList({ onCancel={() => setDeleting(null)} onConfirm={async (): Promise<void> => { try { - await deleteProduct(deleting.id); + await lib.management.deleteProduct(state.token, deleting.id); setNotif({ message: i18n.str`Product "${deleting.description}" (ID: ${deleting.id}) has been deleted`, type: "SUCCESS", diff --git a/packages/merchant-backoffice-ui/src/paths/instance/products/update/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/products/update/index.tsx index 842462c12..b9470ddac 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/products/update/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/products/update/index.tsx @@ -23,15 +23,17 @@ import { HttpStatusCode, TalerErrorDetail, TalerMerchantApi } from "@gnu-taler/t import { ErrorType, HttpError, + useMerchantApiContext, useTranslationContext, } from "@gnu-taler/web-util/browser"; import { Fragment, VNode, h } from "preact"; import { useState } from "preact/hooks"; import { Loading } from "../../../../components/exception/loading.js"; import { NotificationCard } from "../../../../components/menu/index.js"; -import { useProductAPI, useProductDetails } from "../../../../hooks/product.js"; +import { useProductDetails } from "../../../../hooks/product.js"; import { Notification } from "../../../../utils/types.js"; import { UpdatePage } from "./UpdatePage.js"; +import { useSessionContext } from "../../../../context/session.js"; export type Entity = TalerMerchantApi.ProductAddDetail; interface Props { @@ -50,9 +52,10 @@ export default function UpdateProduct({ onNotFound, onLoadError, }: Props): VNode { - const { updateProduct } = useProductAPI(); const result = useProductDetails(pid); const [notif, setNotif] = useState<Notification | undefined>(undefined); + const { lib } = useMerchantApiContext(); + const { state } = useSessionContext(); const { i18n } = useTranslationContext(); @@ -78,7 +81,7 @@ export default function UpdateProduct({ product={{ ...result.data, product_id: pid }} onBack={onBack} onUpdate={(data) => { - return updateProduct(pid, data) + return lib.management.updateProduct(state.token, pid, data) .then(onConfirm) .catch((error) => { setNotif({ |