diff options
Diffstat (limited to 'packages/merchant-backoffice-ui/src/paths/instance/orders')
4 files changed, 85 insertions, 29 deletions
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`, |