From a2668c22f0d18386fc988f27299172145d9fa15d Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 3 Jan 2023 01:57:39 -0300 Subject: refactor better QA removed axios, use fetch removed jest, added mocha and chai moved the default request handler to runtime dependency (so it can be replaced for testing) refactored ALL the test to the standard web-utils all hooks now use ONE request handler moved the tests from test folder to src --- packages/merchant-backoffice-ui/src/hooks/order.ts | 97 +++++----------------- 1 file changed, 20 insertions(+), 77 deletions(-) (limited to 'packages/merchant-backoffice-ui/src/hooks/order.ts') diff --git a/packages/merchant-backoffice-ui/src/hooks/order.ts b/packages/merchant-backoffice-ui/src/hooks/order.ts index d1e26b671..0bea6b963 100644 --- a/packages/merchant-backoffice-ui/src/hooks/order.ts +++ b/packages/merchant-backoffice-ui/src/hooks/order.ts @@ -14,20 +14,16 @@ GNU Taler; see the file COPYING. If not, see */ import { useEffect, useState } from "preact/hooks"; -import useSWR, { useSWRConfig } from "swr"; -import { useBackendContext } from "../context/backend.js"; -import { useInstanceContext } from "../context/instance.js"; +import useSWR from "swr"; import { MerchantBackend } from "../declaration.js"; import { MAX_RESULT_SIZE, PAGE_SIZE } from "../utils/constants.js"; import { - fetcher, HttpError, HttpResponse, HttpResponseOk, HttpResponsePaginated, - request, - useMatchMutate, -} from "./backend.js"; +} from "../utils/request.js"; +import { useBackendInstanceRequest, useMatchMutate } from "./backend.js"; export interface OrderAPI { //FIXME: add OutOfStockResponse on 410 @@ -48,52 +44,17 @@ export interface OrderAPI { type YesOrNo = "yes" | "no"; -export function orderFetcher( - url: string, - token: string, - backend: string, - paid?: YesOrNo, - refunded?: YesOrNo, - wired?: YesOrNo, - searchDate?: Date, - delta?: number, -): Promise> { - const date_ms = - delta && delta < 0 && searchDate - ? searchDate.getTime() + 1 - : searchDate?.getTime(); - const params: any = {}; - if (paid !== undefined) params.paid = paid; - if (delta !== undefined) params.delta = delta; - if (refunded !== undefined) params.refunded = refunded; - if (wired !== undefined) params.wired = wired; - if (date_ms !== undefined) params.date_ms = date_ms; - return request(`${backend}${url}`, { token, params }); -} - export function useOrderAPI(): OrderAPI { const mutateAll = useMatchMutate(); - const { url: baseUrl, token: adminToken } = useBackendContext(); - const { token: instanceToken, id, admin } = useInstanceContext(); - - const { url, token } = !admin - ? { - url: baseUrl, - token: adminToken, - } - : { - url: `${baseUrl}/instances/${id}`, - token: instanceToken, - }; + const { request } = useBackendInstanceRequest(); const createOrder = async ( data: MerchantBackend.Orders.PostOrderRequest, ): Promise> => { const res = await request( - `${url}/private/orders`, + `/private/orders`, { - method: "post", - token, + method: "POST", data, }, ); @@ -107,10 +68,9 @@ export function useOrderAPI(): OrderAPI { ): Promise> => { mutateAll(/@"\/private\/orders"@/); const res = request( - `${url}/private/orders/${orderId}/refund`, + `/private/orders/${orderId}/refund`, { - method: "post", - token, + method: "POST", data, }, ); @@ -125,9 +85,8 @@ export function useOrderAPI(): OrderAPI { data: MerchantBackend.Orders.ForgetRequest, ): Promise> => { mutateAll(/@"\/private\/orders"@/); - const res = request(`${url}/private/orders/${orderId}/forget`, { - method: "patch", - token, + const res = request(`/private/orders/${orderId}/forget`, { + method: "PATCH", data, }); // we may be forgetting some fields that are pare of the listing, so we must evict everything @@ -138,9 +97,8 @@ export function useOrderAPI(): OrderAPI { orderId: string, ): Promise> => { mutateAll(/@"\/private\/orders"@/); - const res = request(`${url}/private/orders/${orderId}`, { - method: "delete", - token, + const res = request(`/private/orders/${orderId}`, { + method: "DELETE", }); await mutateAll(/.*private\/orders.*/); return res; @@ -150,10 +108,9 @@ export function useOrderAPI(): OrderAPI { orderId: string, ): Promise> => { return request( - `${url}/private/orders/${orderId}`, + `/private/orders/${orderId}`, { - method: "get", - token, + method: "GET", }, ).then((res) => { const url = @@ -172,17 +129,12 @@ export function useOrderAPI(): OrderAPI { export function useOrderDetails( oderId: string, ): HttpResponse { - const { url: baseUrl, token: baseToken } = useBackendContext(); - const { token: instanceToken, id, admin } = useInstanceContext(); - - const { url, token } = !admin - ? { url: baseUrl, token: baseToken } - : { url: `${baseUrl}/instances/${id}`, token: instanceToken }; + const { fetcher } = useBackendInstanceRequest(); const { data, error, isValidating } = useSWR< HttpResponseOk, HttpError - >([`/private/orders/${oderId}`, token, url], fetcher, { + >([`/private/orders/${oderId}`], fetcher, { refreshInterval: 0, refreshWhenHidden: false, revalidateOnFocus: false, @@ -207,12 +159,7 @@ export function useInstanceOrders( args?: InstanceOrderFilter, updateFilter?: (d: Date) => void, ): HttpResponsePaginated { - const { url: baseUrl, token: baseToken } = useBackendContext(); - const { token: instanceToken, id, admin } = useInstanceContext(); - - const { url, token } = !admin - ? { url: baseUrl, token: baseToken } - : { url: `${baseUrl}/instances/${id}`, token: instanceToken }; + const { orderFetcher } = useBackendInstanceRequest(); const [pageBefore, setPageBefore] = useState(1); const [pageAfter, setPageAfter] = useState(1); @@ -233,8 +180,6 @@ export function useInstanceOrders( } = useSWR, HttpError>( [ `/private/orders`, - token, - url, args?.paid, args?.refunded, args?.wired, @@ -250,8 +195,6 @@ export function useInstanceOrders( } = useSWR, HttpError>( [ `/private/orders`, - token, - url, args?.paid, args?.refunded, args?.wired, @@ -314,9 +257,9 @@ export function useInstanceOrders( !beforeData || !afterData ? [] : (beforeData || lastBefore).data.orders - .slice() - .reverse() - .concat((afterData || lastAfter).data.orders); + .slice() + .reverse() + .concat((afterData || lastAfter).data.orders); if (loadingAfter || loadingBefore) return { loading: true, data: { orders } }; if (beforeData && afterData) { return { ok: true, data: { orders }, ...pagination }; -- cgit v1.2.3