From e1c53940c12dfa20fbf321d6e2b522aa319cd20a Mon Sep 17 00:00:00 2001 From: Romain Le Cellier Date: Wed, 6 Mar 2024 17:31:19 +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 now that we've generated types and api client, we need to use them into our api hooks --- src/frontend/js/hooks/useAddresses.ts | 37 ++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/frontend/js/hooks/useAddresses.ts b/src/frontend/js/hooks/useAddresses.ts index 672580402c..6f64da8f16 100644 --- a/src/frontend/js/hooks/useAddresses.ts +++ b/src/frontend/js/hooks/useAddresses.ts @@ -1,9 +1,13 @@ import { defineMessages } from 'react-intl'; import { MutateOptions } from '@tanstack/react-query'; -import { useJoanieApi } from 'contexts/JoanieApiContext'; -import { Address, AddressCreationPayload, API } from 'types/Joanie'; +import { joanieApi } from 'api/joanie/client'; +import type { Address } from 'api/joanie/gen'; +import { PatchedAddressRequest } from 'api/joanie/gen'; +import { ApiResourceInterface, PaginatedResourceQuery } from 'types/Joanie'; +import { Maybe } from 'types/utils'; import { HttpError } from 'utils/errors/HttpError'; -import { ResourcesQuery, useResource, useResources, UseResourcesProps } from './useResources'; + +import { useResource, useResources, UseResourcesProps } from './useResources'; const messages = defineMessages({ errorUpdate: { @@ -33,15 +37,36 @@ const messages = defineMessages({ }, }); -export type AddressesMutateOptions = MutateOptions; +export type AddressesMutateOptions = MutateOptions; + +const apiInterface: ApiResourceInterface = { + get: (resourceQuery?: Maybe) => { + const { id, ...filters } = resourceQuery || {}; + if (id) { + return joanieApi.addresses.addressesRetrieve(id); + } + return joanieApi.addresses.addressesList(filters.page, filters.page_size); + }, + create: (data: Address) => { + return joanieApi.addresses.addressesCreate(data); + }, + update: (data: PatchedAddressRequest & { id: string }) => { + const { id, ...updateData } = data; + return joanieApi.addresses.addressesPartialUpdate(id, updateData); + }, + delete: (id: string) => { + return joanieApi.addresses.addressesDestroy(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: () => apiInterface, omniscient: true, session: true, messages,