From 7c6fe4afdc42c7d6b26ddc65fcf3bbcaa190cad3 Mon Sep 17 00:00:00 2001 From: Romain Le Cellier Date: Tue, 21 Nov 2023 14:39:51 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F(frontend)=20useAddresses=20w?= =?UTF-8?q?ith=20new=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/js/hooks/useAddresses.ts | 31 ++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/frontend/js/hooks/useAddresses.ts b/src/frontend/js/hooks/useAddresses.ts index 672580402c..934193b3b3 100644 --- a/src/frontend/js/hooks/useAddresses.ts +++ b/src/frontend/js/hooks/useAddresses.ts @@ -1,8 +1,9 @@ import { defineMessages } from 'react-intl'; import { MutateOptions } from '@tanstack/react-query'; -import { useJoanieApi } from 'contexts/JoanieApiContext'; -import { Address, AddressCreationPayload, API } from 'types/Joanie'; import { HttpError } from 'utils/errors/HttpError'; +import { PatchedAddress } from 'api/joanie/gen'; +import { joanieApi } from 'api/joanie'; +import type { Address } from 'api/joanie/gen'; import { ResourcesQuery, useResource, useResources, UseResourcesProps } from './useResources'; const messages = defineMessages({ @@ -33,15 +34,35 @@ const messages = defineMessages({ }, }); -export type AddressesMutateOptions = MutateOptions; +export type AddressesMutateOptions = MutateOptions; + +const apiInterface = { + get: ({ id, ...filters }: ResourcesQuery) => { + if (id) { + return joanieApi.api.apiV10AddressesRetrieve(id); + } + // FIXME: apiV10AddressesRetrieve do not accept filters object argument + return joanieApi.api.apiV10AddressesRetrieve({ id, filters }); + }, + create: (data: Address) => { + return joanieApi.api.apiV10AddressesCreate(data); + }, + update: (id: string, data: PatchedAddress) => { + return joanieApi.api.apiV10AddressesPartialUpdate(id, data); + }, + delete: (id: string) => { + return joanieApi.api.apiV10AddressesDestroy(id); + }, +}; /** * Joanie Api hook to retrieve/create/update/delete addresses * owned by the authenticated user. */ -const props: UseResourcesProps = { +// FIXME: now that we've both Address and PatchedAddress, UseResourcesProps cannot be build from a unique input/output type. +const props: UseResourcesProps = { queryKey: ['addresses'], - apiInterface: () => useJoanieApi().user.addresses, + apiInterface, omniscient: true, session: true, messages,