Skip to content

Commit

Permalink
feat: finalise the migration to v5
Browse files Browse the repository at this point in the history
  • Loading branch information
ReidyT committed Aug 22, 2024
1 parent 927a979 commit c011a09
Show file tree
Hide file tree
Showing 23 changed files with 1,031 additions and 926 deletions.
13 changes: 13 additions & 0 deletions src/.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import '@tanstack/react-query';

interface MyMeta extends Record<string, unknown> {
// Your meta type definition.
typeError?: string;
}

declare module '@tanstack/react-query' {
interface Register {
queryMeta: MyMeta;
mutationMeta: MyMeta;
}
}
8 changes: 4 additions & 4 deletions src/hooks/invitation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getInvitationRoutine } from '../routines/invitation.js';
import { QueryClientConfig } from '../types.js';

export default (queryConfig: QueryClientConfig) => {
const { notifier, defaultQueryOptions } = queryConfig;
const { defaultQueryOptions } = queryConfig;

const useInvitation = (id?: UUID) =>
useQuery({
Expand All @@ -20,11 +20,11 @@ export default (queryConfig: QueryClientConfig) => {
}
return Api.getInvitation(queryConfig, id);
},
meta: {
typeError: getInvitationRoutine.FAILURE,
},
...defaultQueryOptions,
enabled: Boolean(id),
onError: (error) => {
notifier?.({ type: getInvitationRoutine.FAILURE, payload: { error } });
},
});

const useItemInvitations = (itemId?: UUID) =>
Expand Down
23 changes: 7 additions & 16 deletions src/hooks/itemGeolocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { QueryClientConfig } from '../types.js';
import useDebounce from './useDebounce.js';

export default (queryConfig: QueryClientConfig) => {
const { notifier, defaultQueryOptions } = queryConfig;
const { defaultQueryOptions } = queryConfig;

const useItemGeolocation = (id?: DiscriminatedItem['id']) =>
useQuery({
Expand All @@ -33,11 +33,8 @@ export default (queryConfig: QueryClientConfig) => {
},
...defaultQueryOptions,
enabled: Boolean(id),
onError: (error) => {
notifier?.({
type: getItemGeolocationRoutine.FAILURE,
payload: { error },
});
meta: {
typeError: getItemGeolocationRoutine.FAILURE,
},
});

Expand Down Expand Up @@ -115,11 +112,8 @@ export default (queryConfig: QueryClientConfig) => {
},
...defaultQueryOptions,
enabled: Boolean((lat || lat === 0) && (lng || lng === 0)) && enabled,
onError: (error) => {
notifier?.({
type: getAddressFromCoordinatesRoutine.FAILURE,
payload: { error },
});
meta: {
typeError: getAddressFromCoordinatesRoutine.FAILURE,
},
});
};
Expand Down Expand Up @@ -155,11 +149,8 @@ export default (queryConfig: QueryClientConfig) => {
},
...defaultQueryOptions,
enabled: Boolean(debouncedAddress) && enabled,
onError: (error) => {
notifier?.({
type: getSuggestionsForAddressRoutine.FAILURE,
payload: { error },
});
meta: {
typeError: getSuggestionsForAddressRoutine.FAILURE,
},
});
};
Expand Down
52 changes: 29 additions & 23 deletions src/hooks/itemPublish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as Api from '../api/itemPublish.js';
import { UndefinedArgument } from '../config/errors.js';
import { itemKeys } from '../keys.js';
import { QueryClientConfig } from '../types.js';
import { useOnDataChanged } from '../utils/useOnDataChanged.js';

export default (queryConfig: QueryClientConfig) => {
const { defaultQueryOptions } = queryConfig;
Expand Down Expand Up @@ -98,30 +99,35 @@ export default (queryConfig: QueryClientConfig) => {
const enabled =
(options?.enabled ?? true) && Boolean(args.itemIds.length);
const queryClient = useQueryClient();
return useQuery({
queryKey: itemKeys.many(args.itemIds).publishedInformation,
queryFn: () =>
splitRequestByIdsAndReturn<ItemPublished>(
args.itemIds,
MAX_TARGETS_FOR_READ_REQUEST,
(chunk) => Api.getManyItemPublishedInformations(chunk, queryConfig),
true,
),
onSuccess: async (publishedData) => {
// save items in their own key
if (publishedData?.data) {
Object.values(publishedData?.data)?.forEach(async (p) => {
const { id } = p.item;
queryClient.setQueryData(
itemKeys.single(id).publishedInformation,
p,
);
});
}
return useOnDataChanged(
useQuery({
queryKey: itemKeys.many(args.itemIds).publishedInformation,
queryFn: () =>
splitRequestByIdsAndReturn<ItemPublished>(
args.itemIds,
MAX_TARGETS_FOR_READ_REQUEST,
(chunk) =>
Api.getManyItemPublishedInformations(chunk, queryConfig),
true,
),
...defaultQueryOptions,
enabled,
}),
{
onSuccess: (publishedData) => {
// save items in their own key
if (publishedData?.data) {
Object.values(publishedData?.data)?.forEach((p) => {
const { id } = p.item;
queryClient.setQueryData(
itemKeys.single(id).publishedInformation,
p,
);
});
}
},
},
...defaultQueryOptions,
enabled,
});
);
},
};
};
51 changes: 28 additions & 23 deletions src/hooks/itemTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as Api from '../api/itemTag.js';
import { UndefinedArgument } from '../config/errors.js';
import { itemKeys } from '../keys.js';
import { QueryClientConfig } from '../types.js';
import { useOnDataChanged } from '../utils/useOnDataChanged.js';

export default (queryConfig: QueryClientConfig) => {
const { defaultQueryOptions } = queryConfig;
Expand All @@ -26,31 +27,35 @@ export default (queryConfig: QueryClientConfig) => {

const useItemsTags = (ids?: UUID[]) => {
const queryClient = useQueryClient();
return useQuery({
queryKey: itemKeys.many(ids).tags,
queryFn: () => {
if (!ids || ids?.length === 0) {
throw new UndefinedArgument();
}
return splitRequestByIdsAndReturn(
ids,
MAX_TARGETS_FOR_READ_REQUEST,
(chunk) => Api.getItemsTags(chunk, queryConfig),
true,
);
},
onSuccess: async (tags) => {
// save tags in their own key
ids?.forEach(async (id) => {
const itemTags = tags?.data?.[id];
if (itemTags?.length) {
queryClient.setQueryData(itemKeys.single(id).tags, itemTags);
return useOnDataChanged(
useQuery({
queryKey: itemKeys.many(ids).tags,
queryFn: () => {
if (!ids || ids?.length === 0) {
throw new UndefinedArgument();
}
});
return splitRequestByIdsAndReturn(
ids,
MAX_TARGETS_FOR_READ_REQUEST,
(chunk) => Api.getItemsTags(chunk, queryConfig),
true,
);
},
enabled: Boolean(ids && ids.length),
...defaultQueryOptions,
}),
{
onSuccess: (tags) => {
// save tags in their own key
ids?.forEach((id) => {
const itemTags = tags?.data?.[id];
if (itemTags?.length) {
queryClient.setQueryData(itemKeys.single(id).tags, itemTags);
}
});
},
},
enabled: Boolean(ids && ids.length),
...defaultQueryOptions,
});
);
};

return { useItemTags, useItemsTags };
Expand Down
55 changes: 30 additions & 25 deletions src/hooks/membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as Api from '../api/membership.js';
import { UndefinedArgument } from '../config/errors.js';
import { buildManyItemMembershipsKey, itemKeys } from '../keys.js';
import { QueryClientConfig } from '../types.js';
import { useOnDataChanged } from '../utils/useOnDataChanged.js';
import { configureWsMembershipHooks } from '../ws/index.js';

export default (
Expand Down Expand Up @@ -57,33 +58,37 @@ export default (

membershipWsHooks?.useItemsMembershipsUpdates(getUpdates ? ids : null);

return useQuery({
queryKey: buildManyItemMembershipsKey(ids),
queryFn: () => {
if (!ids) {
throw new UndefinedArgument();
}
return useOnDataChanged(
useQuery({
queryKey: buildManyItemMembershipsKey(ids),
queryFn: () => {
if (!ids) {
throw new UndefinedArgument();
}

return splitRequestByIdsAndReturn(
ids,
MAX_TARGETS_FOR_READ_REQUEST,
(chunk) => Api.getMembershipsForItems(chunk, queryConfig),
);
return splitRequestByIdsAndReturn(
ids,
MAX_TARGETS_FOR_READ_REQUEST,
(chunk) => Api.getMembershipsForItems(chunk, queryConfig),
);
},
enabled: Boolean(ids?.length) && ids?.every((id) => Boolean(id)),
...defaultQueryOptions,
}),
{
onSuccess: (memberships) => {
// save memberships in their own key
if (memberships) {
ids?.forEach((id) => {
queryClient.setQueryData(
itemKeys.single(id).memberships,
memberships.data[id],
);
});
}
},
},
onSuccess: async (memberships) => {
// save memberships in their own key
if (memberships) {
ids?.forEach(async (id) => {
queryClient.setQueryData(
itemKeys.single(id).memberships,
memberships.data[id],
);
});
}
},
enabled: Boolean(ids?.length) && ids?.every((id) => Boolean(id)),
...defaultQueryOptions,
});
);
},
};
};
Loading

0 comments on commit c011a09

Please sign in to comment.