Skip to content

Commit

Permalink
fix: split get many publish info, no settled for many calls (#278)
Browse files Browse the repository at this point in the history
* fix: split get many publish info, no settled for many calls

* refactor: remove settled callback for async calls

* refactor: fix delete mutation onMutation
  • Loading branch information
pyphilia authored Jun 13, 2023
1 parent 64242b7 commit adc09eb
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 878 deletions.
4 changes: 2 additions & 2 deletions src/api/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ export const splitRequestByIds = <T>(
).then((responses) => {
// only get request returns
// todo: not ideal..
if (!responses[0] || !responses[0]?.data) {
if (responses.every((r: any) => !r?.data)) {
return null;
}

const result = responses.reduce(
(prev, d) => ({
data: { ...prev.data, ...d.data },
data: { ...prev.data, ...(d.data ?? {}) },
errors: prev.errors.concat(d.errors),
}),
{ data: {}, errors: [] },
Expand Down
4 changes: 2 additions & 2 deletions src/api/itemPublish.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Item, UUID } from '@graasp/sdk';
import { Item, ItemPublished, ResultOf, UUID } from '@graasp/sdk';

import { QueryClientConfig } from '../types';
import configureAxios, { verifyAuthentication } from './axios';
Expand Down Expand Up @@ -40,7 +40,7 @@ export const getItemPublishedInformation = async (
export const getManyItemPublishedInformations = async (
ids: UUID[],
{ API_HOST }: QueryClientConfig,
) =>
): Promise<ResultOf<ItemPublished>> =>
axios
.get(`${API_HOST}/${buildManyGetItemPublishedInformationsRoute(ids)}`)
.then(({ data }) => data);
Expand Down
15 changes: 12 additions & 3 deletions src/hooks/itemPublish.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { List } from 'immutable';
import { useQuery, useQueryClient } from 'react-query';

import { ItemPublished, UUID, convertJs } from '@graasp/sdk';
import {
ItemPublished,
MAX_TARGETS_FOR_READ_REQUEST,
UUID,
convertJs,
} from '@graasp/sdk';
import {
ItemPublishedRecord,
ItemRecord,
ResultOfRecord,
} from '@graasp/sdk/frontend';

import * as Api from '../api';
import { splitRequestByIds } from '../api/axios';
import { UndefinedArgument } from '../config/errors';
import {
buildItemPublishedInformationKey,
Expand Down Expand Up @@ -85,8 +91,11 @@ export default (queryConfig: QueryClientConfig) => {
return useQuery({
queryKey: buildManyItemPublishedInformationsKey(args.itemIds),
queryFn: (): Promise<ResultOfRecord<ItemPublished>> =>
Api.getManyItemPublishedInformations(args.itemIds, queryConfig).then(
(data) => convertJs(data),
splitRequestByIds<ItemPublished>(
args.itemIds,
MAX_TARGETS_FOR_READ_REQUEST,
(chunk) => Api.getManyItemPublishedInformations(chunk, queryConfig),
true,
),
onSuccess: async (publishedData) => {
// save items in their own key
Expand Down
Loading

0 comments on commit adc09eb

Please sign in to comment.