diff options
Diffstat (limited to 'packages/merchant-backoffice-ui/src/paths')
21 files changed, 220 insertions, 114 deletions
diff --git a/packages/merchant-backoffice-ui/src/paths/instance/accounts/create/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/accounts/create/index.tsx index b12b95f2f..35c9e6624 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/accounts/create/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/accounts/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 { useBankAccountAPI } from "../../../../hooks/bank.js"; import { Notification } from "../../../../utils/types.js"; import { CreatePage } from "./CreatePage.js"; +import { useSessionContext } from "../../../../context/session.js"; export type Entity = TalerMerchantApi.AccountAddDetails; interface Props { @@ -35,7 +35,8 @@ interface Props { } export default function CreateValidator({ onConfirm, onBack }: Props): VNode { - const { createBankAccount } = useBankAccountAPI(); + const { lib: api } = useMerchantApiContext(); + const { state } = useSessionContext(); const [notif, setNotif] = useState<Notification | undefined>(undefined); const { i18n } = useTranslationContext(); @@ -45,7 +46,7 @@ export default function CreateValidator({ onConfirm, onBack }: Props): VNode { <CreatePage onBack={onBack} onCreate={(request: Entity) => { - return createBankAccount(request) + return api.management.addAccount(state.token, request) .then(() => { onConfirm() }) diff --git a/packages/merchant-backoffice-ui/src/paths/instance/accounts/list/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/accounts/list/index.tsx index 72efa08c9..8de6c763e 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/accounts/list/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/accounts/list/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 { useBankAccountAPI, useInstanceBankAccounts } from "../../../../hooks/bank.js"; +import { useInstanceBankAccounts } from "../../../../hooks/bank.js"; import { Notification } from "../../../../utils/types.js"; import { ListPage } from "./ListPage.js"; +import { useSessionContext } from "../../../../context/session.js"; interface Props { onUnauthorized: () => VNode; @@ -51,7 +53,8 @@ export default function ListOtpDevices({ const [position, setPosition] = useState<string | undefined>(undefined); const { i18n } = useTranslationContext(); const [notif, setNotif] = useState<Notification | undefined>(undefined); - const { deleteBankAccount } = useBankAccountAPI(); + const { lib: api } = useMerchantApiContext(); + const { state } = useSessionContext(); const result = useInstanceBankAccounts({ position }, (id) => setPosition(id)); if (result.loading) return <Loading />; @@ -89,8 +92,8 @@ export default function ListOtpDevices({ onSelect={(e) => { onSelect(e.h_wire); }} - onDelete={(e: TalerMerchantApi.BankAccountEntry) => - deleteBankAccount(e.h_wire) + onDelete={(e: TalerMerchantApi.BankAccountEntry) => { + return api.management.deleteAccount(state.token, e.h_wire) .then(() => setNotif({ message: i18n.str`bank account delete successfully`, @@ -105,6 +108,7 @@ export default function ListOtpDevices({ }), ) } + } /> </Fragment> ); diff --git a/packages/merchant-backoffice-ui/src/paths/instance/accounts/update/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/accounts/update/index.tsx index 742d13b67..f83020ed1 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/accounts/update/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/accounts/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 { useBankAccountAPI, useBankAccountDetails } from "../../../../hooks/bank.js"; +import { useBankAccountDetails } from "../../../../hooks/bank.js"; import { Notification } from "../../../../utils/types.js"; import { UpdatePage } from "./UpdatePage.js"; +import { useSessionContext } from "../../../../context/session.js"; export type Entity = TalerMerchantApi.AccountPatchDetails & WithId; @@ -51,7 +53,8 @@ export default function UpdateValidator({ onNotFound, onLoadError, }: Props): VNode { - const { updateBankAccount } = useBankAccountAPI(); + const { lib: api } = useMerchantApiContext(); + const { state } = useSessionContext(); const result = useBankAccountDetails(bid); const [notif, setNotif] = useState<Notification | undefined>(undefined); @@ -79,7 +82,7 @@ export default function UpdateValidator({ account={{ ...result.data, id: bid }} onBack={onBack} onUpdate={(data) => { - return updateBankAccount(bid, data) + return api.management.updateAccount(state.token, bid, data) .then(onConfirm) .catch((error) => { setNotif({ diff --git a/packages/merchant-backoffice-ui/src/paths/instance/orders/create/OrderCreatedSuccessfully.tsx b/packages/merchant-backoffice-ui/src/paths/instance/orders/create/OrderCreatedSuccessfully.tsx index 3f7b20f52..be631c1e0 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/orders/create/OrderCreatedSuccessfully.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/orders/create/OrderCreatedSuccessfully.tsx @@ -13,32 +13,52 @@ 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 { useTranslationContext } from "@gnu-taler/web-util/browser"; +import { ErrorType, HttpError, Loading, useTranslationContext } from "@gnu-taler/web-util/browser"; import { h, VNode } from "preact"; import { useEffect, useState } from "preact/hooks"; import { CreatedSuccessfully } from "../../../../components/notifications/CreatedSuccessfully.js"; -import { useOrderAPI } from "../../../../hooks/order.js"; import { Entity } from "./index.js"; +import { useOrderDetails } from "../../../../hooks/order.js"; +import { HttpStatusCode, TalerErrorDetail } from "@gnu-taler/taler-util"; interface Props { entity: Entity; onConfirm: () => void; onCreateAnother?: () => void; + onUnauthorized: () => VNode; + onNotFound: () => VNode; + onLoadError: (error: HttpError<TalerErrorDetail>) => VNode; } export function OrderCreatedSuccessfully({ entity, onConfirm, onCreateAnother, + onLoadError, + onNotFound, + onUnauthorized, }: Props): VNode { - const { getPaymentURL } = useOrderAPI(); - const [url, setURL] = useState<string | undefined>(undefined); + const result = useOrderDetails(entity.response.order_id) const { i18n } = useTranslationContext(); - useEffect(() => { - getPaymentURL(entity.response.order_id).then((response) => { - setURL(response.data); - }); - }, [getPaymentURL, entity.response.order_id]); + + 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); + } + + const url = result.data.order_status === "unpaid" ? + result.data.taler_pay_uri : + result.data.contract_terms.fulfillment_url return ( <CreatedSuccessfully diff --git a/packages/merchant-backoffice-ui/src/paths/instance/orders/create/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/orders/create/index.tsx index 0f8618435..a7165fa41 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/orders/create/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/orders/create/index.tsx @@ -20,16 +20,16 @@ */ import { HttpStatusCode, TalerErrorDetail, TalerMerchantApi } from "@gnu-taler/taler-util"; -import { ErrorType, HttpError } from "@gnu-taler/web-util/browser"; +import { ErrorType, HttpError, useMerchantApiContext } 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 { useInstanceDetails } from "../../../../hooks/instance.js"; -import { useOrderAPI } from "../../../../hooks/order.js"; import { useInstanceProducts } from "../../../../hooks/product.js"; import { Notification } from "../../../../utils/types.js"; import { CreatePage } from "./CreatePage.js"; +import { useSessionContext } from "../../../../context/session.js"; export type Entity = { request: TalerMerchantApi.PostOrderRequest; @@ -49,9 +49,9 @@ export default function OrderCreate({ onNotFound, onUnauthorized, }: Props): VNode { - const { createOrder } = useOrderAPI(); + const { lib } = useMerchantApiContext(); const [notif, setNotif] = useState<Notification | undefined>(undefined); - + const { state } = useSessionContext(); const detailsResult = useInstanceDetails(); const inventoryResult = useInstanceProducts(); @@ -93,9 +93,16 @@ export default function OrderCreate({ <CreatePage onBack={onBack} onCreate={(request: TalerMerchantApi.PostOrderRequest) => { - createOrder(request) + lib.management.createOrder(state.token, request) .then((r) => { - return onConfirm(r.data.order_id) + if (r.type === "ok") { + return onConfirm(r.body.order_id) + } else { + setNotif({ + message: "could not create order", + type: "ERROR", + }); + } }) .catch((error) => { setNotif({ diff --git a/packages/merchant-backoffice-ui/src/paths/instance/orders/details/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/orders/details/index.tsx index a7fe1801b..c0c4862a1 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/orders/details/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/orders/details/index.tsx @@ -17,15 +17,17 @@ import { HttpStatusCode, TalerErrorDetail } from "@gnu-taler/taler-util"; 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 { useOrderAPI, useOrderDetails } from "../../../../hooks/order.js"; +import { useOrderDetails } from "../../../../hooks/order.js"; import { Notification } from "../../../../utils/types.js"; import { DetailPage } from "./DetailPage.js"; +import { useSessionContext } from "../../../../context/session.js"; export interface Props { oid: string; @@ -43,9 +45,10 @@ export default function Update({ onNotFound, onUnauthorized, }: Props): VNode { - const { refundOrder } = useOrderAPI(); const result = useOrderDetails(oid); const [notif, setNotif] = useState<Notification | undefined>(undefined); + const { lib: api } = useMerchantApiContext() + const { state } = useSessionContext(); const { i18n } = useTranslationContext(); @@ -71,8 +74,11 @@ export default function Update({ <DetailPage onBack={onBack} id={oid} - onRefund={(id, value) => - refundOrder(id, value) + onRefund={(id, value) => { + if (state.status !== "loggedIn") { + return; + } + api.management.addRefund(state.token, id, value) .then(() => setNotif({ message: i18n.str`refund created successfully`, @@ -87,6 +93,7 @@ export default function Update({ }), ) } + } selected={result.data} /> </Fragment> diff --git a/packages/merchant-backoffice-ui/src/paths/instance/orders/list/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/orders/list/index.tsx index cd62685ca..a50f02a2d 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/orders/list/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/orders/list/index.tsx @@ -19,10 +19,11 @@ * @author Sebastian Javier Marchano (sebasjm) */ -import { HttpStatusCode, TalerErrorDetail, TalerMerchantApi } from "@gnu-taler/taler-util"; +import { HttpStatusCode, TalerErrorDetail, TalerMerchantApi, stringifyPayUri } from "@gnu-taler/taler-util"; import { ErrorType, HttpError, + useMerchantApiContext, useTranslationContext, } from "@gnu-taler/web-util/browser"; import { Fragment, VNode, h } from "preact"; @@ -33,12 +34,12 @@ import { NotificationCard } from "../../../../components/menu/index.js"; import { InstanceOrderFilter, useInstanceOrders, - useOrderAPI, useOrderDetails, } from "../../../../hooks/order.js"; import { Notification } from "../../../../utils/types.js"; import { ListPage } from "./ListPage.js"; import { RefundModal } from "./Table.js"; +import { useSessionContext } from "../../../../context/session.js"; interface Props { onUnauthorized: () => VNode; @@ -64,11 +65,12 @@ export default function OrderList({ setFilter((prev) => ({ ...prev, date })); const result = useInstanceOrders(filter, setNewDate); - const { refundOrder, getPaymentURL } = useOrderAPI(); + const { lib } = useMerchantApiContext(); const [notif, setNotif] = useState<Notification | undefined>(undefined); const { i18n } = useTranslationContext(); + const { state } = useSessionContext() if (result.loading) return <Loading />; if (!result.ok) { @@ -102,7 +104,13 @@ export default function OrderList({ <NotificationCard notification={notif} /> <JumpToElementById - testIfExist={getPaymentURL} + testIfExist={async (order) => { + if (state.status !== "loggedIn") { + return false; + } + const resp = await lib.management.getOrder(state.token, order) + return resp.type === "ok" + }} onSelect={onSelect} description={i18n.str`jump to order with the given product ID`} placeholder={i18n.str`order id`} @@ -123,9 +131,22 @@ export default function OrderList({ isNotPaidActive={isNotPaidActive} isRefundedActive={isRefundedActive} jumpToDate={filter.date} - onCopyURL={(id) => - getPaymentURL(id).then((resp) => copyToClipboard(resp.data)) - } + onCopyURL={async (id) => { + if (state.status !== "loggedIn") { + return false; + } + const resp = await lib.management.getOrder(state.token, id) + if (resp.type === "ok") { + if (resp.body.order_status === "unpaid") { + copyToClipboard(resp.body.taler_pay_uri) + } else { + if (resp.body.contract_terms.fulfillment_url) { + copyToClipboard(resp.body.contract_terms.fulfillment_url) + } + } + copyToClipboard(resp.body.order_status) + } + }} onCreate={onCreate} onSelectDate={setNewDate} onShowAll={() => setFilter({})} @@ -140,8 +161,8 @@ export default function OrderList({ <RefundModalForTable id={orderToBeRefunded.order_id} onCancel={() => setOrderToBeRefunded(undefined)} - onConfirm={(value) => - refundOrder(orderToBeRefunded.order_id, value) + onConfirm={(value) => { + lib.management.addRefund(state.token, orderToBeRefunded.order_id, value) .then(() => setNotif({ message: i18n.str`refund created successfully`, @@ -156,7 +177,8 @@ export default function OrderList({ }), ) .then(() => setOrderToBeRefunded(undefined)) - } + + }} onLoadError={(error) => { setNotif({ message: i18n.str`could not create the refund`, diff --git a/packages/merchant-backoffice-ui/src/paths/instance/otp_devices/create/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/otp_devices/create/index.tsx index e4501a053..6ad1295ed 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/otp_devices/create/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/otp_devices/create/index.tsx @@ -20,14 +20,14 @@ */ 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 { useOtpDeviceAPI } from "../../../../hooks/otp.js"; import { Notification } from "../../../../utils/types.js"; import { CreatedSuccessfully } from "./CreatedSuccessfully.js"; import { CreatePage } from "./CreatePage.js"; +import { useSessionContext } from "../../../../context/session.js"; export type Entity = TalerMerchantApi.OtpDeviceAddDetails; interface Props { @@ -36,7 +36,8 @@ interface Props { } export default function CreateValidator({ onConfirm, onBack }: Props): VNode { - const { createOtpDevice } = useOtpDeviceAPI(); + const { lib: api } = useMerchantApiContext(); + const { state } = useSessionContext(); const [notif, setNotif] = useState<Notification | undefined>(undefined); const { i18n } = useTranslationContext(); const [created, setCreated] = useState<TalerMerchantApi.OtpDeviceAddDetails | null>(null) @@ -51,7 +52,7 @@ export default function CreateValidator({ onConfirm, onBack }: Props): VNode { <CreatePage onBack={onBack} onCreate={(request: Entity) => { - return createOtpDevice(request) + return api.management.addOtpDevice(state.token, request) .then((d) => { setCreated(request) }) diff --git a/packages/merchant-backoffice-ui/src/paths/instance/otp_devices/list/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/otp_devices/list/index.tsx index 7fd827956..06a9f4a55 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/otp_devices/list/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/otp_devices/list/index.tsx @@ -23,13 +23,15 @@ 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 { useInstanceOtpDevices, useOtpDeviceAPI } from "../../../../hooks/otp.js"; +import { useSessionContext } from "../../../../context/session.js"; +import { useInstanceOtpDevices } from "../../../../hooks/otp.js"; import { Notification } from "../../../../utils/types.js"; import { ListPage } from "./ListPage.js"; @@ -51,7 +53,8 @@ export default function ListOtpDevices({ const [position, setPosition] = useState<string | undefined>(undefined); const { i18n } = useTranslationContext(); const [notif, setNotif] = useState<Notification | undefined>(undefined); - const { deleteOtpDevice } = useOtpDeviceAPI(); + const { lib } = useMerchantApiContext(); + const { state } = useSessionContext(); const result = useInstanceOtpDevices({ position }, (id) => setPosition(id)); if (result.loading) return <Loading />; @@ -83,8 +86,8 @@ export default function ListOtpDevices({ onSelect={(e) => { onSelect(e.otp_device_id); }} - onDelete={(e: TalerMerchantApi.OtpDeviceEntry) => - deleteOtpDevice(e.otp_device_id) + onDelete={(e: TalerMerchantApi.OtpDeviceEntry) => { + return lib.management.deleteOtpDevice(state.token, e.otp_device_id) .then(() => setNotif({ message: i18n.str`validator delete successfully`, @@ -98,7 +101,7 @@ export default function ListOtpDevices({ description: error.message, }), ) - } + }} /> </Fragment> ); diff --git a/packages/merchant-backoffice-ui/src/paths/instance/otp_devices/update/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/otp_devices/update/index.tsx index a824c6936..6b9970eab 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/otp_devices/update/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/otp_devices/update/index.tsx @@ -23,13 +23,15 @@ 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 { useOtpDeviceAPI, useOtpDeviceDetails } from "../../../../hooks/otp.js"; +import { useSessionContext } from "../../../../context/session.js"; +import { useOtpDeviceDetails } from "../../../../hooks/otp.js"; import { Notification } from "../../../../utils/types.js"; import { CreatedSuccessfully } from "../create/CreatedSuccessfully.js"; import { UpdatePage } from "./UpdatePage.js"; @@ -52,10 +54,11 @@ export default function UpdateValidator({ onNotFound, onLoadError, }: Props): VNode { - const { updateOtpDevice } = useOtpDeviceAPI(); const result = useOtpDeviceDetails(vid); const [notif, setNotif] = useState<Notification | undefined>(undefined); const [keyUpdated, setKeyUpdated] = useState<TalerMerchantApi.OtpDeviceAddDetails | null>(null) + const { lib } = useMerchantApiContext(); + const { state } = useSessionContext(); const { i18n } = useTranslationContext(); @@ -91,7 +94,7 @@ export default function UpdateValidator({ }} onBack={onBack} onUpdate={async (newInfo) => { - return updateOtpDevice(vid, newInfo) + return lib.management.updateOtpDevice(state.token, vid, newInfo) .then((d) => { if (newInfo.otp_key) { setKeyUpdated({ 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({ diff --git a/packages/merchant-backoffice-ui/src/paths/instance/templates/create/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/templates/create/index.tsx index 593850268..d23afb609 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/templates/create/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/templates/create/index.tsx @@ -20,11 +20,11 @@ */ import { TalerMerchantApi } from "@gnu-taler/taler-util"; -import { useTranslationContext } from "@gnu-taler/web-util/browser"; -import { Fragment, h, VNode } from "preact"; +import { useMerchantApiContext, useTranslationContext } from "@gnu-taler/web-util/browser"; +import { Fragment, VNode, h } from "preact"; import { useState } from "preact/hooks"; import { NotificationCard } from "../../../../components/menu/index.js"; -import { useTemplateAPI } from "../../../../hooks/templates.js"; +import { useSessionContext } from "../../../../context/session.js"; import { Notification } from "../../../../utils/types.js"; import { CreatePage } from "./CreatePage.js"; @@ -35,7 +35,8 @@ interface Props { } export default function CreateTransfer({ onConfirm, onBack }: Props): VNode { - const { createTemplate } = useTemplateAPI(); + const { lib } = useMerchantApiContext(); + const { state } = useSessionContext(); const [notif, setNotif] = useState<Notification | undefined>(undefined); const { i18n } = useTranslationContext(); @@ -45,7 +46,7 @@ export default function CreateTransfer({ onConfirm, onBack }: Props): VNode { <CreatePage onBack={onBack} onCreate={(request: TalerMerchantApi.TemplateAddDetails) => { - return createTemplate(request) + return lib.management.addTemplate(state.token, request) .then(() => onConfirm()) .catch((error) => { setNotif({ diff --git a/packages/merchant-backoffice-ui/src/paths/instance/templates/list/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/templates/list/index.tsx index 40ca6ac98..adbfb93cd 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/templates/list/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/templates/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 { Fragment, 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 { - useInstanceTemplates, - useTemplateAPI, + useInstanceTemplates } from "../../../../hooks/templates.js"; import { Notification } from "../../../../utils/types.js"; import { ListPage } from "./ListPage.js"; @@ -60,10 +61,11 @@ export default function ListTemplates({ const [position, setPosition] = useState<string | undefined>(undefined); const { i18n } = useTranslationContext(); const [notif, setNotif] = useState<Notification | undefined>(undefined); - const { deleteTemplate, testTemplateExist } = useTemplateAPI(); + const { lib } = useMerchantApiContext(); const result = useInstanceTemplates({ position }, (id) => setPosition(id)); const [deleting, setDeleting] = useState<TalerMerchantApi.TemplateEntry | null>(null); + const { state } = useSessionContext(); if (result.loading) return <Loading />; if (!result.ok) { @@ -85,7 +87,10 @@ export default function ListTemplates({ <NotificationCard notification={notif} /> <JumpToElementById - testIfExist={testTemplateExist} + testIfExist={async (id) => { + const resp = await lib.management.getTemplate(state.token, id) + return resp.type === "ok" + }} onSelect={onSelect} description={i18n.str`jump to template with the given template ID`} placeholder={i18n.str`template id`} @@ -122,7 +127,7 @@ export default function ListTemplates({ onCancel={() => setDeleting(null)} onConfirm={async (): Promise<void> => { try { - await deleteTemplate(deleting.template_id); + await lib.management.deleteTemplate(state.token, deleting.template_id); setNotif({ message: i18n.str`Template "${deleting.template_description}" (ID: ${deleting.template_id}) has been deleted`, type: "SUCCESS", diff --git a/packages/merchant-backoffice-ui/src/paths/instance/templates/update/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/templates/update/index.tsx index ba1939914..fc436056a 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/templates/update/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/templates/update/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 { Fragment, VNode, h } from "preact"; @@ -30,11 +31,11 @@ import { useState } from "preact/hooks"; import { Loading } from "../../../../components/exception/loading.js"; import { NotificationCard } from "../../../../components/menu/index.js"; import { - useTemplateAPI, useTemplateDetails, } from "../../../../hooks/templates.js"; import { Notification } from "../../../../utils/types.js"; import { UpdatePage } from "./UpdatePage.js"; +import { useSessionContext } from "../../../../context/session.js"; export type Entity = TalerMerchantApi.TemplatePatchDetails & WithId; @@ -54,7 +55,8 @@ export default function UpdateTemplate({ onNotFound, onLoadError, }: Props): VNode { - const { updateTemplate } = useTemplateAPI(); + const { lib } = useMerchantApiContext(); + const { state } = useSessionContext(); const result = useTemplateDetails(tid); const [notif, setNotif] = useState<Notification | undefined>(undefined); @@ -82,7 +84,7 @@ export default function UpdateTemplate({ template={{ ...result.data }} onBack={onBack} onUpdate={(data) => { - return updateTemplate(tid, data) + return lib.management.updateTemplate(state.token, tid, data) .then(onConfirm) .catch((error) => { setNotif({ diff --git a/packages/merchant-backoffice-ui/src/paths/instance/templates/use/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/templates/use/index.tsx index 64c38c86b..dd5d4aea3 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/templates/use/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/templates/use/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 { Fragment, VNode, h } from "preact"; @@ -30,11 +31,11 @@ import { useState } from "preact/hooks"; import { Loading } from "../../../../components/exception/loading.js"; import { NotificationCard } from "../../../../components/menu/index.js"; import { - useTemplateAPI, - useTemplateDetails, + useTemplateDetails } from "../../../../hooks/templates.js"; import { Notification } from "../../../../utils/types.js"; import { UsePage } from "./UsePage.js"; +import { useSessionContext } from "../../../../context/session.js"; export type Entity = TalerMerchantApi.TransferInformation; interface Props { @@ -54,7 +55,8 @@ export default function TemplateUsePage({ onNotFound, onUnauthorized, }: Props): VNode { - const { createOrderFromTemplate } = useTemplateAPI(); + const { lib } = useMerchantApiContext(); + const { state } = useSessionContext(); const result = useTemplateDetails(tid); const [notif, setNotif] = useState<Notification | undefined>(undefined); const { i18n } = useTranslationContext(); @@ -84,8 +86,18 @@ export default function TemplateUsePage({ onCreateOrder={( request: TalerMerchantApi.UsingTemplateDetails, ) => { - return createOrderFromTemplate(tid, request) - .then((res) => onOrderCreated(res.data.order_id)) + + return lib.management.useTemplateCreateOrder(tid, request) + .then((res) => { + if (res.type === "ok") { + onOrderCreated(res.body.order_id) + } else { + setNotif({ + message: i18n.str`could not create order from template`, + type: "ERROR", + }); + } + }) .catch((error) => { setNotif({ message: i18n.str`could not create order from template`, diff --git a/packages/merchant-backoffice-ui/src/paths/instance/transfers/create/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/transfers/create/index.tsx index e640e47f6..ac1d692a4 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/transfers/create/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/transfers/create/index.tsx @@ -20,14 +20,14 @@ */ 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 { useInstanceBankAccounts } from "../../../../hooks/bank.js"; -import { useTransferAPI } from "../../../../hooks/transfer.js"; import { Notification } from "../../../../utils/types.js"; import { CreatePage } from "./CreatePage.js"; +import { useSessionContext } from "../../../../context/session.js"; export type Entity = TalerMerchantApi.TransferInformation; interface Props { @@ -36,7 +36,8 @@ interface Props { } export default function CreateTransfer({ onConfirm, onBack }: Props): VNode { - const { informTransfer } = useTransferAPI(); + const { lib } = useMerchantApiContext(); + const { state } = useSessionContext(); const [notif, setNotif] = useState<Notification | undefined>(undefined); const { i18n } = useTranslationContext(); const instance = useInstanceBankAccounts(); @@ -51,7 +52,7 @@ export default function CreateTransfer({ onConfirm, onBack }: Props): VNode { onBack={onBack} accounts={accounts} onCreate={(request: TalerMerchantApi.TransferInformation) => { - return informTransfer(request) + return lib.management.informWireTransfer(state.token, request) .then(() => onConfirm()) .catch((error) => { setNotif({ diff --git a/packages/merchant-backoffice-ui/src/paths/instance/webhooks/create/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/webhooks/create/index.tsx index 42a432cf0..50c431079 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/webhooks/create/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/webhooks/create/index.tsx @@ -19,14 +19,14 @@ * @author Sebastian Javier Marchano (sebasjm) */ -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 { useWebhookAPI } from "../../../../hooks/webhooks.js"; import { Notification } from "../../../../utils/types.js"; import { CreatePage } from "./CreatePage.js"; import { TalerMerchantApi } from "@gnu-taler/taler-util"; +import { useSessionContext } from "../../../../context/session.js"; export type Entity = TalerMerchantApi.WebhookAddDetails; interface Props { @@ -35,9 +35,10 @@ interface Props { } export default function CreateWebhook({ onConfirm, onBack }: Props): VNode { - const { createWebhook } = useWebhookAPI(); const [notif, setNotif] = useState<Notification | undefined>(undefined); const { i18n } = useTranslationContext(); + const { lib } = useMerchantApiContext(); + const { state } = useSessionContext(); return ( <> @@ -45,7 +46,7 @@ export default function CreateWebhook({ onConfirm, onBack }: Props): VNode { <CreatePage onBack={onBack} onCreate={(request: TalerMerchantApi.WebhookAddDetails) => { - return createWebhook(request) + return lib.management.addWebhook(state.token, request) .then(() => onConfirm()) .catch((error) => { setNotif({ 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 17e767337..2923a8096 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 @@ -22,6 +22,7 @@ import { ErrorType, HttpError, + useMerchantApiContext, useTranslationContext, } from "@gnu-taler/web-util/browser"; import { Fragment, h, VNode } from "preact"; @@ -30,11 +31,11 @@ import { Loading } from "../../../../components/exception/loading.js"; import { NotificationCard } from "../../../../components/menu/index.js"; import { useInstanceWebhooks, - useWebhookAPI, } from "../../../../hooks/webhooks.js"; import { Notification } from "../../../../utils/types.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; @@ -54,7 +55,8 @@ export default function ListWebhooks({ const [position, setPosition] = useState<string | undefined>(undefined); const { i18n } = useTranslationContext(); const [notif, setNotif] = useState<Notification | undefined>(undefined); - const { deleteWebhook } = useWebhookAPI(); + const { lib } = useMerchantApiContext(); + const { state } = useSessionContext(); const result = useInstanceWebhooks({ position }, (id) => setPosition(id)); if (result.loading) return <Loading />; @@ -86,8 +88,8 @@ export default function ListWebhooks({ onSelect={(e) => { onSelect(e.webhook_id); }} - onDelete={(e: TalerMerchantApi.WebhookEntry) => - deleteWebhook(e.webhook_id) + onDelete={(e: TalerMerchantApi.WebhookEntry) => { + return lib.management.deleteWebhook(state.token, e.webhook_id) .then(() => setNotif({ message: i18n.str`webhook delete successfully`, @@ -101,7 +103,7 @@ export default function ListWebhooks({ description: error.message, }), ) - } + }} /> </Fragment> ); diff --git a/packages/merchant-backoffice-ui/src/paths/instance/webhooks/update/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/webhooks/update/index.tsx index 97b4f44ba..aecb4b947 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/webhooks/update/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/webhooks/update/index.tsx @@ -22,6 +22,7 @@ import { ErrorType, HttpError, + useMerchantApiContext, useTranslationContext, } from "@gnu-taler/web-util/browser"; import { Fragment, h, VNode } from "preact"; @@ -29,12 +30,12 @@ import { useState } from "preact/hooks"; import { Loading } from "../../../../components/exception/loading.js"; import { NotificationCard } from "../../../../components/menu/index.js"; import { - useWebhookAPI, useWebhookDetails, } from "../../../../hooks/webhooks.js"; import { Notification } from "../../../../utils/types.js"; import { UpdatePage } from "./UpdatePage.js"; import { HttpStatusCode, TalerErrorDetail, TalerMerchantApi } from "@gnu-taler/taler-util"; +import { useSessionContext } from "../../../../context/session.js"; export type Entity = TalerMerchantApi.WebhookPatchDetails & WithId; @@ -54,7 +55,8 @@ export default function UpdateWebhook({ onNotFound, onLoadError, }: Props): VNode { - const { updateWebhook } = useWebhookAPI(); + const { lib } = useMerchantApiContext(); + const { state } = useSessionContext(); const result = useWebhookDetails(tid); const [notif, setNotif] = useState<Notification | undefined>(undefined); @@ -82,7 +84,7 @@ export default function UpdateWebhook({ webhook={{ ...result.data, id: tid }} onBack={onBack} onUpdate={(data) => { - return updateWebhook(tid, data) + return lib.management.updateWebhook(state.token, tid, data) .then(onConfirm) .catch((error) => { setNotif({ |