This commit is contained in:
Lucas Bordeau 2024-07-05 16:55:10 +02:00
parent aa2218900c
commit 2e2c607ad4
3 changed files with 65 additions and 31 deletions

View File

@ -0,0 +1 @@
export const DEFAULT_MUTATION_BATCH_SIZE = 30;

View File

@ -1,9 +1,12 @@
import { useApolloClient } from '@apollo/client'; import { useApolloClient } from '@apollo/client';
import { useRecoilValue } from 'recoil';
import { triggerDeleteRecordsOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerDeleteRecordsOptimisticEffect'; import { triggerDeleteRecordsOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerDeleteRecordsOptimisticEffect';
import { apiConfigState } from '@/client-config/states/apiConfigState';
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems'; import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems';
import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordFromCache'; import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordFromCache';
import { DEFAULT_MUTATION_BATCH_SIZE } from '@/object-record/constants/DefaultMutationBatchSize';
import { useDeleteManyRecordsMutation } from '@/object-record/hooks/useDeleteManyRecordsMutation'; import { useDeleteManyRecordsMutation } from '@/object-record/hooks/useDeleteManyRecordsMutation';
import { getDeleteManyRecordsMutationResponseField } from '@/object-record/utils/getDeleteManyRecordsMutationResponseField'; import { getDeleteManyRecordsMutationResponseField } from '@/object-record/utils/getDeleteManyRecordsMutationResponseField';
import { isDefined } from '~/utils/isDefined'; import { isDefined } from '~/utils/isDefined';
@ -41,44 +44,69 @@ export const useDeleteManyRecords = ({
objectMetadataItem.namePlural, objectMetadataItem.namePlural,
); );
const apiConfig = useRecoilValue(apiConfigState);
const maxRecords = apiConfig?.mutationMaximumAffectedRecords;
const deleteManyRecords = async ( const deleteManyRecords = async (
idsToDelete: string[], idsToDelete: string[],
options?: DeleteManyRecordsOptions, options?: DeleteManyRecordsOptions,
) => { ) => {
const deletedRecords = await apolloClient.mutate({ const numberOfBatches = Math.ceil(
mutation: deleteManyRecordsMutation, idsToDelete.length / DEFAULT_MUTATION_BATCH_SIZE,
variables: { );
filter: { id: { in: idsToDelete } },
},
optimisticResponse: options?.skipOptimisticEffect
? undefined
: {
[mutationResponseField]: idsToDelete.map((idToDelete) => ({
__typename: capitalize(objectNameSingular),
id: idToDelete,
})),
},
update: options?.skipOptimisticEffect
? undefined
: (cache, { data }) => {
const records = data?.[mutationResponseField];
if (!records?.length) return; const deletedRecords = [];
const cachedRecords = records for (let batchIndex = 0; batchIndex < numberOfBatches; batchIndex++) {
.map((record) => getRecordFromCache(record.id, cache)) const batchIds = idsToDelete.slice(
.filter(isDefined); batchIndex * DEFAULT_MUTATION_BATCH_SIZE,
(batchIndex + 1) * DEFAULT_MUTATION_BATCH_SIZE,
);
triggerDeleteRecordsOptimisticEffect({ console.log({
cache, batchIds,
objectMetadataItem, });
recordsToDelete: cachedRecords,
objectMetadataItems,
});
},
});
return deletedRecords.data?.[mutationResponseField] ?? null; const deletedRecordsResponse = await apolloClient.mutate({
mutation: deleteManyRecordsMutation,
variables: {
filter: { id: { in: batchIds } },
},
optimisticResponse: options?.skipOptimisticEffect
? undefined
: {
[mutationResponseField]: batchIds.map((idToDelete) => ({
__typename: capitalize(objectNameSingular),
id: idToDelete,
})),
},
update: options?.skipOptimisticEffect
? undefined
: (cache, { data }) => {
const records = data?.[mutationResponseField];
if (!records?.length) return;
const cachedRecords = records
.map((record) => getRecordFromCache(record.id, cache))
.filter(isDefined);
triggerDeleteRecordsOptimisticEffect({
cache,
objectMetadataItem,
recordsToDelete: cachedRecords,
objectMetadataItems,
});
},
});
const deletedRecordsForThisBatch =
deletedRecordsResponse.data?.[mutationResponseField] ?? [];
deletedRecords.push(...deletedRecordsForThisBatch);
}
return deletedRecords;
}; };
return { deleteManyRecords }; return { deleteManyRecords };

View File

@ -1,6 +1,11 @@
import { useCallback, useMemo, useState } from 'react'; import { useCallback, useMemo, useState } from 'react';
import { isNonEmptyString } from '@sniptt/guards'; import { isNonEmptyString } from '@sniptt/guards';
import { useRecoilCallback, useRecoilValue, useSetRecoilState } from 'recoil'; import {
useRecoilCallback,
useRecoilValue,
useRecoilValue,
useSetRecoilState,
} from 'recoil';
import { import {
IconClick, IconClick,
IconFileExport, IconFileExport,