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 --- .../merchant-backoffice-ui/src/hooks/templates.ts | 92 ++++++---------------- 1 file changed, 22 insertions(+), 70 deletions(-) (limited to 'packages/merchant-backoffice-ui/src/hooks/templates.ts') diff --git a/packages/merchant-backoffice-ui/src/hooks/templates.ts b/packages/merchant-backoffice-ui/src/hooks/templates.ts index 3e69d78d0..55c3875b5 100644 --- a/packages/merchant-backoffice-ui/src/hooks/templates.ts +++ b/packages/merchant-backoffice-ui/src/hooks/templates.ts @@ -14,57 +14,26 @@ GNU Taler; see the file COPYING. If not, see */ import { MerchantBackend } from "../declaration.js"; -import { useBackendContext } from "../context/backend.js"; +import { useMatchMutate, useBackendInstanceRequest } from "./backend.js"; +import useSWR from "swr"; +import { MAX_RESULT_SIZE, PAGE_SIZE } from "../utils/constants.js"; +import { useEffect, useState } from "preact/hooks"; import { - request, - HttpResponse, HttpError, + HttpResponse, HttpResponseOk, HttpResponsePaginated, - useMatchMutate, -} from "./backend.js"; -import useSWR from "swr"; -import { useInstanceContext } from "../context/instance.js"; -import { MAX_RESULT_SIZE, PAGE_SIZE } from "../utils/constants.js"; -import { useEffect, useState } from "preact/hooks"; - -async function templateFetcher( - url: string, - token: string, - backend: string, - position?: string, - delta?: number, -): Promise> { - const params: any = {}; - if (delta !== undefined) { - params.limit = delta; - } - if (position !== undefined) params.offset = position; - - return request(`${backend}${url}`, { token, params }); -} +} from "../utils/request.js"; export function useTemplateAPI(): TemplateAPI { 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 createTemplate = async ( data: MerchantBackend.Template.TemplateAddDetails, ): Promise> => { - const res = await request(`${url}/private/templates`, { - method: "post", - token, + const res = await request(`/private/templates`, { + method: "POST", data, }); await mutateAll(/.*private\/templates.*/); @@ -75,9 +44,8 @@ export function useTemplateAPI(): TemplateAPI { templateId: string, data: MerchantBackend.Template.TemplatePatchDetails, ): Promise> => { - const res = await request(`${url}/private/templates/${templateId}`, { - method: "patch", - token, + const res = await request(`/private/templates/${templateId}`, { + method: "PATCH", data, }); await mutateAll(/.*private\/templates.*/); @@ -87,9 +55,8 @@ export function useTemplateAPI(): TemplateAPI { const deleteTemplate = async ( templateId: string, ): Promise> => { - const res = await request(`${url}/private/templates/${templateId}`, { - method: "delete", - token, + const res = await request(`/private/templates/${templateId}`, { + method: "DELETE", }); await mutateAll(/.*private\/templates.*/); return res; @@ -102,10 +69,9 @@ export function useTemplateAPI(): TemplateAPI { HttpResponseOk > => { const res = await request( - `${url}/private/templates/${templateId}`, + `/private/templates/${templateId}`, { - method: "post", - token, + method: "POST", data, }, ); @@ -140,12 +106,7 @@ export function useInstanceTemplates( args?: InstanceTemplateFilter, updatePosition?: (id: string) => 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 { templateFetcher } = useBackendInstanceRequest(); // const [pageBefore, setPageBefore] = useState(1); const [pageAfter, setPageAfter] = useState(1); @@ -180,10 +141,7 @@ export function useInstanceTemplates( } = useSWR< HttpResponseOk, HttpError - >( - [`/private/templates`, token, url, args?.position, -totalAfter], - templateFetcher, - ); + >([`/private/templates`, args?.position, -totalAfter], templateFetcher); //this will save last result // const [lastBefore, setLastBefore] = useState< @@ -216,10 +174,9 @@ export function useInstanceTemplates( if (afterData.data.templates.length < MAX_RESULT_SIZE) { setPageAfter(pageAfter + 1); } else { - const from = `${ - afterData.data.templates[afterData.data.templates.length - 1] - .template_id - }`; + const from = `${afterData.data.templates[afterData.data.templates.length - 1] + .template_id + }`; if (from && updatePosition) updatePosition(from); } }, @@ -255,17 +212,12 @@ export function useInstanceTemplates( export function useTemplateDetails( templateId: 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 { templateFetcher } = useBackendInstanceRequest(); const { data, error, isValidating } = useSWR< HttpResponseOk, HttpError - >([`/private/templates/${templateId}`, token, url], templateFetcher, { + >([`/private/templates/${templateId}`], templateFetcher, { refreshInterval: 0, refreshWhenHidden: false, revalidateOnFocus: false, -- cgit v1.2.3