diff options
author | Sebastian <sebasjm@gmail.com> | 2023-01-03 01:57:39 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-01-03 01:58:18 -0300 |
commit | a2668c22f0d18386fc988f27299172145d9fa15d (patch) | |
tree | 38f06046ce4d71ee3af64ede931754bfae6dc954 /packages/merchant-backoffice-ui/src/hooks/templates.ts | |
parent | d1aa79eae817b1cf4c23f800308ecad101692ac7 (diff) | |
download | wallet-core-a2668c22f0d18386fc988f27299172145d9fa15d.tar.xz |
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
Diffstat (limited to 'packages/merchant-backoffice-ui/src/hooks/templates.ts')
-rw-r--r-- | packages/merchant-backoffice-ui/src/hooks/templates.ts | 92 |
1 files changed, 22 insertions, 70 deletions
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 <http://www.gnu.org/licenses/> */ 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<T>( - url: string, - token: string, - backend: string, - position?: string, - delta?: number, -): Promise<HttpResponseOk<T>> { - const params: any = {}; - if (delta !== undefined) { - params.limit = delta; - } - if (position !== undefined) params.offset = position; - - return request<T>(`${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<HttpResponseOk<void>> => { - const res = await request<void>(`${url}/private/templates`, { - method: "post", - token, + const res = await request<void>(`/private/templates`, { + method: "POST", data, }); await mutateAll(/.*private\/templates.*/); @@ -75,9 +44,8 @@ export function useTemplateAPI(): TemplateAPI { templateId: string, data: MerchantBackend.Template.TemplatePatchDetails, ): Promise<HttpResponseOk<void>> => { - const res = await request<void>(`${url}/private/templates/${templateId}`, { - method: "patch", - token, + const res = await request<void>(`/private/templates/${templateId}`, { + method: "PATCH", data, }); await mutateAll(/.*private\/templates.*/); @@ -87,9 +55,8 @@ export function useTemplateAPI(): TemplateAPI { const deleteTemplate = async ( templateId: string, ): Promise<HttpResponseOk<void>> => { - const res = await request<void>(`${url}/private/templates/${templateId}`, { - method: "delete", - token, + const res = await request<void>(`/private/templates/${templateId}`, { + method: "DELETE", }); await mutateAll(/.*private\/templates.*/); return res; @@ -102,10 +69,9 @@ export function useTemplateAPI(): TemplateAPI { HttpResponseOk<MerchantBackend.Template.UsingTemplateResponse> > => { const res = await request<MerchantBackend.Template.UsingTemplateResponse>( - `${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<MerchantBackend.Template.TemplateSummaryResponse> { - 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<MerchantBackend.Template.TemplateSummaryResponse>, 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<MerchantBackend.Template.TemplateDetails> { - 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<MerchantBackend.Template.TemplateDetails>, HttpError - >([`/private/templates/${templateId}`, token, url], templateFetcher, { + >([`/private/templates/${templateId}`], templateFetcher, { refreshInterval: 0, refreshWhenHidden: false, revalidateOnFocus: false, |