diff options
Diffstat (limited to 'packages/merchant-backoffice-ui/src/hooks/otp.ts')
-rw-r--r-- | packages/merchant-backoffice-ui/src/hooks/otp.ts | 231 |
1 files changed, 54 insertions, 177 deletions
diff --git a/packages/merchant-backoffice-ui/src/hooks/otp.ts b/packages/merchant-backoffice-ui/src/hooks/otp.ts index 36db2ea90..69e4a0f4f 100644 --- a/packages/merchant-backoffice-ui/src/hooks/otp.ts +++ b/packages/merchant-backoffice-ui/src/hooks/otp.ts @@ -14,195 +14,72 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ import { - HttpResponse, - HttpResponseOk, - HttpResponsePaginated, - RequestError, + useMerchantApiContext } from "@gnu-taler/web-util/browser"; -import { useEffect, useState } from "preact/hooks"; -import { MAX_RESULT_SIZE, PAGE_SIZE } from "../utils/constants.js"; -import { useBackendInstanceRequest, useMatchMutate } from "./backend.js"; +import { useState } from "preact/hooks"; +import { PAGE_SIZE } from "../utils/constants.js"; // FIX default import https://github.com/microsoft/TypeScript/issues/49189 -import { TalerErrorDetail, TalerMerchantApi } from "@gnu-taler/taler-util"; -import _useSWR, { SWRHook } from "swr"; +import { AccessToken, TalerHttpError, TalerMerchantManagementResultByMethod } from "@gnu-taler/taler-util"; +import _useSWR, { SWRHook, mutate } from "swr"; +import { useSessionContext } from "../context/session.js"; +import { buildPaginatedResult } from "./webhooks.js"; const useSWR = _useSWR as unknown as SWRHook; -// export function useOtpDeviceAPI(): OtpDeviceAPI { -// const mutateAll = useMatchMutate(); -// const { request } = useBackendInstanceRequest(); - -// const createOtpDevice = async ( -// data: TalerMerchantApi.OtpDeviceAddDetails, -// ): Promise<HttpResponseOk<void>> => { -// // MOCKED_DEVICES[data.otp_device_id] = data -// // return Promise.resolve({ ok: true, data: undefined }); -// const res = await request<void>(`/private/otp-devices`, { -// method: "POST", -// data, -// }); -// await mutateAll(/.*private\/otp-devices.*/); -// return res; -// }; - -// const updateOtpDevice = async ( -// deviceId: string, -// data: TalerMerchantApi.OtpDevicePatchDetails, -// ): Promise<HttpResponseOk<void>> => { -// // MOCKED_DEVICES[deviceId].otp_algorithm = data.otp_algorithm -// // MOCKED_DEVICES[deviceId].otp_ctr = data.otp_ctr -// // MOCKED_DEVICES[deviceId].otp_device_description = data.otp_device_description -// // MOCKED_DEVICES[deviceId].otp_key = data.otp_key -// // return Promise.resolve({ ok: true, data: undefined }); -// const res = await request<void>(`/private/otp-devices/${deviceId}`, { -// method: "PATCH", -// data, -// }); -// await mutateAll(/.*private\/otp-devices.*/); -// return res; -// }; - -// const deleteOtpDevice = async ( -// deviceId: string, -// ): Promise<HttpResponseOk<void>> => { -// // delete MOCKED_DEVICES[deviceId] -// // return Promise.resolve({ ok: true, data: undefined }); -// const res = await request<void>(`/private/otp-devices/${deviceId}`, { -// method: "DELETE", -// }); -// await mutateAll(/.*private\/otp-devices.*/); -// return res; -// }; - -// return { -// createOtpDevice, -// updateOtpDevice, -// deleteOtpDevice, -// }; -// } - -// export interface OtpDeviceAPI { -// createOtpDevice: ( -// data: TalerMerchantApi.OtpDeviceAddDetails, -// ) => Promise<HttpResponseOk<void>>; -// updateOtpDevice: ( -// id: string, -// data: TalerMerchantApi.OtpDevicePatchDetails, -// ) => Promise<HttpResponseOk<void>>; -// deleteOtpDevice: (id: string) => Promise<HttpResponseOk<void>>; -// } - -export interface InstanceOtpDeviceFilter { +export function revalidateInstanceOtpDevices() { + return mutate( + (key) => Array.isArray(key) && key[key.length - 1] === "listOtpDevices", + undefined, + { revalidate: true }, + ); } +export function useInstanceOtpDevices() { + const { state: session } = useSessionContext(); + const { lib: { management } } = useMerchantApiContext(); + + const [offset, setOffset] = useState<string | undefined>(); + + async function fetcher([token, bid]: [AccessToken, string]) { + return await management.listOtpDevices(token, { + limit: PAGE_SIZE, + offset: bid, + order: "dec", + }); + } -export function useInstanceOtpDevices( - args?: InstanceOtpDeviceFilter, - updatePosition?: (id: string) => void, -): HttpResponsePaginated< - TalerMerchantApi.OtpDeviceSummaryResponse, - TalerErrorDetail -> { - // return { - // ok: true, - // loadMore: () => { }, - // loadMorePrev: () => { }, - // data: { - // otp_devices: Object.values(MOCKED_DEVICES).map(d => ({ - // device_description: d.otp_device_description, - // otp_device_id: d.otp_device_id - // })) - // } - // } - - const { fetcher } = useBackendInstanceRequest(); - - const [pageAfter, setPageAfter] = useState(1); - - const totalAfter = pageAfter * PAGE_SIZE; - const { - data: afterData, - error: afterError, - isValidating: loadingAfter, - } = useSWR< - HttpResponseOk<TalerMerchantApi.OtpDeviceSummaryResponse>, - RequestError<TalerErrorDetail> - >([`/private/otp-devices`], fetcher); - - const [lastAfter, setLastAfter] = useState< - HttpResponse< - TalerMerchantApi.OtpDeviceSummaryResponse, - TalerErrorDetail - > - >({ loading: true }); - useEffect(() => { - if (afterData) setLastAfter(afterData); - }, [afterData /*, beforeData*/]); - - if (afterError) return afterError.cause; + const { data, error } = useSWR< + TalerMerchantManagementResultByMethod<"listOtpDevices">, + TalerHttpError + >([session.token, offset, "listOtpDevices"], fetcher); - // if the query returns less that we ask, then we have reach the end or beginning - const isReachingEnd = - afterData && afterData.data.otp_devices.length < totalAfter; - const isReachingStart = true; + if (error) return error; + if (data === undefined) return undefined; + if (data.type !== "ok") return data; - const pagination = { - isReachingEnd, - isReachingStart, - loadMore: () => { - if (!afterData || isReachingEnd) return; - if (afterData.data.otp_devices.length < MAX_RESULT_SIZE) { - setPageAfter(pageAfter + 1); - } else { - const from = `${afterData.data.otp_devices[afterData.data.otp_devices.length - 1] - .otp_device_id - }`; - if (from && updatePosition) updatePosition(from); - } - }, - loadMorePrev: () => { - }, - }; + return buildPaginatedResult(data.body.otp_devices, offset, setOffset, (d) => d.otp_device_id) +} - const otp_devices = !afterData ? [] : (afterData || lastAfter).data.otp_devices; - if (loadingAfter /* || loadingBefore */) - return { loading: true, data: { otp_devices } }; - if (/*beforeData &&*/ afterData) { - return { ok: true, data: { otp_devices }, ...pagination }; - } - return { loading: true }; +export function revalidateOtpDeviceDetails() { + return mutate( + (key) => Array.isArray(key) && key[key.length - 1] === "getOtpDeviceDetails", + undefined, + { revalidate: true }, + ); } +export function useOtpDeviceDetails(deviceId: string) { + const { state: session } = useSessionContext(); + const { lib: { management } } = useMerchantApiContext(); -export function useOtpDeviceDetails( - deviceId: string, -): HttpResponse< - TalerMerchantApi.OtpDeviceDetails, - TalerErrorDetail -> { - // return { - // ok: true, - // data: { - // device_description: MOCKED_DEVICES[deviceId].otp_device_description, - // otp_algorithm: MOCKED_DEVICES[deviceId].otp_algorithm, - // otp_ctr: MOCKED_DEVICES[deviceId].otp_ctr - // } - // } - const { fetcher } = useBackendInstanceRequest(); + async function fetcher([dId, token]: [string, AccessToken]) { + return await management.getOtpDeviceDetails(token, dId); + } - const { data, error, isValidating } = useSWR< - HttpResponseOk<TalerMerchantApi.OtpDeviceDetails>, - RequestError<TalerErrorDetail> - >([`/private/otp-devices/${deviceId}`], fetcher, { - refreshInterval: 0, - refreshWhenHidden: false, - revalidateOnFocus: false, - revalidateOnReconnect: false, - refreshWhenOffline: false, - }); + const { data, error } = useSWR< + TalerMerchantManagementResultByMethod<"getOtpDeviceDetails">, + TalerHttpError + >([deviceId, session.token, "getOtpDeviceDetails"], fetcher); - if (isValidating) return { loading: true, data: data?.data }; - if (data) { - return data; - } - if (error) return error.cause; - return { loading: true }; + if (data) return data; + if (error) return error; + return undefined; } |