Skip to content

Commit

Permalink
♻️(frontend) useAddresses with new api
Browse files Browse the repository at this point in the history
  • Loading branch information
rlecellier committed Feb 28, 2024
1 parent 903881e commit eecbfe5
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/frontend/js/hooks/useAddresses.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -33,15 +34,35 @@ const messages = defineMessages({
},
});

export type AddressesMutateOptions = MutateOptions<Address, HttpError, AddressCreationPayload>;
export type AddressesMutateOptions = MutateOptions<Address, HttpError, Address>;

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<Address, ResourcesQuery, API['user']['addresses']> = {
// FIXME: now that we've both Address and PatchedAddress, UseResourcesProps cannot be build from a unique input/output type.
const props: UseResourcesProps<PatchedAddress, ResourcesQuery, typeof apiInterface> = {
queryKey: ['addresses'],
apiInterface: () => useJoanieApi().user.addresses,
apiInterface,
omniscient: true,
session: true,
messages,
Expand Down

0 comments on commit eecbfe5

Please sign in to comment.