Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesBochet committed Oct 11, 2024
1 parent 20a759c commit ec79de7
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { isDefined } from '~/utils/isDefined';
export const triggerDestroyRecordsOptimisticEffect = ({
cache,
objectMetadataItem,
recordsToDelete,
recordsToDestroy,
objectMetadataItems,
}: {
cache: ApolloCache<unknown>;
objectMetadataItem: ObjectMetadataItem;
recordsToDelete: RecordGqlNode[];
recordsToDestroy: RecordGqlNode[];
objectMetadataItems: ObjectMetadataItem[];
}) => {
cache.modify<StoreObject>({
Expand All @@ -36,7 +36,7 @@ export const triggerDestroyRecordsOptimisticEffect = ({

const rootQueryCachedObjectRecordConnection = rootQueryCachedResponse;

const recordIdsToDelete = recordsToDelete.map(({ id }) => id);
const recordIdsToDelete = recordsToDestroy.map(({ id }) => id);

const cachedEdges = readField<RecordGqlRefEdge[]>(
'edges',
Expand Down Expand Up @@ -69,15 +69,15 @@ export const triggerDestroyRecordsOptimisticEffect = ({
},
});

recordsToDelete.forEach((recordToDelete) => {
recordsToDestroy.forEach((recordToDestroy) => {
triggerUpdateRelationsOptimisticEffect({
cache,
sourceObjectMetadataItem: objectMetadataItem,
currentSourceRecord: recordToDelete,
currentSourceRecord: recordToDestroy,
updatedSourceRecord: null,
objectMetadataItems,
});

cache.evict({ id: cache.identify(recordToDelete) });
cache.evict({ id: cache.identify(recordToDestroy) });
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -65,50 +65,45 @@ export const triggerUpdateRecordOptimisticEffect = ({
const rootQueryFilter = rootQueryVariables?.filter;
const rootQueryOrderBy = rootQueryVariables?.orderBy;

const shouldTryToMatchFilter = isDefined(rootQueryFilter);

if (shouldTryToMatchFilter) {
const updatedRecordMatchesThisRootQueryFilter =
isRecordMatchingFilter({
record: updatedRecord,
filter: rootQueryFilter,
objectMetadataItem,
});
const updatedRecordMatchesThisRootQueryFilter = isRecordMatchingFilter({
record: updatedRecord,
filter: rootQueryFilter ?? {},
objectMetadataItem,
});

const updatedRecordIndexInRootQueryEdges =
rootQueryCurrentEdges.findIndex(
(cachedEdge) =>
readField('id', cachedEdge.node) === updatedRecord.id,
);

const updatedRecordIndexInRootQueryEdges =
rootQueryCurrentEdges.findIndex(
(cachedEdge) =>
readField('id', cachedEdge.node) === updatedRecord.id,
);

const updatedRecordFoundInRootQueryEdges =
updatedRecordIndexInRootQueryEdges > -1;

const updatedRecordShouldBeAddedToRootQueryEdges =
updatedRecordMatchesThisRootQueryFilter &&
!updatedRecordFoundInRootQueryEdges;

const updatedRecordShouldBeRemovedFromRootQueryEdges =
!updatedRecordMatchesThisRootQueryFilter &&
updatedRecordFoundInRootQueryEdges;

if (updatedRecordShouldBeAddedToRootQueryEdges) {
const updatedRecordNodeReference = toReference(updatedRecord);

if (isDefined(updatedRecordNodeReference)) {
rootQueryNextEdges.push({
__typename: getEdgeTypename(objectMetadataItem.nameSingular),
node: updatedRecordNodeReference,
cursor: '',
});
}
}
const updatedRecordFoundInRootQueryEdges =
updatedRecordIndexInRootQueryEdges > -1;

const updatedRecordShouldBeAddedToRootQueryEdges =
updatedRecordMatchesThisRootQueryFilter &&
!updatedRecordFoundInRootQueryEdges;

const updatedRecordShouldBeRemovedFromRootQueryEdges =
!updatedRecordMatchesThisRootQueryFilter &&
updatedRecordFoundInRootQueryEdges;

if (updatedRecordShouldBeRemovedFromRootQueryEdges) {
rootQueryNextEdges.splice(updatedRecordIndexInRootQueryEdges, 1);
if (updatedRecordShouldBeAddedToRootQueryEdges) {
const updatedRecordNodeReference = toReference(updatedRecord);

if (isDefined(updatedRecordNodeReference)) {
rootQueryNextEdges.push({
__typename: getEdgeTypename(objectMetadataItem.nameSingular),
node: updatedRecordNodeReference,
cursor: '',
});
}
}

if (updatedRecordShouldBeRemovedFromRootQueryEdges) {
rootQueryNextEdges.splice(updatedRecordIndexInRootQueryEdges, 1);
}

const rootQueryNextEdgesShouldBeSorted = isDefined(rootQueryOrderBy);

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export const triggerUpdateRelationsOptimisticEffect = ({
triggerDestroyRecordsOptimisticEffect({
cache,
objectMetadataItem: fullTargetObjectMetadataItem,
recordsToDelete: targetRecordsToDetachFrom,
recordsToDestroy: targetRecordsToDetachFrom,
objectMetadataItems,
});
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ export const useDeleteRecordFromCache = ({

const { objectMetadataItems } = useObjectMetadataItems();

return (recordToDelete: ObjectRecord) => {
return (recordToDestroy: ObjectRecord) => {
deleteRecordFromCache({
objectMetadataItem,
objectMetadataItems,
recordToDelete,
recordToDestroy,
cache: apolloClient.cache,
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ import { ObjectRecord } from '@/object-record/types/ObjectRecord';
export const deleteRecordFromCache = ({
objectMetadataItem,
objectMetadataItems,
recordToDelete,
recordToDestroy,
cache,
}: {
objectMetadataItem: ObjectMetadataItem;
objectMetadataItems: ObjectMetadataItem[];
recordToDelete: ObjectRecord;
recordToDestroy: ObjectRecord;
cache: ApolloCache<object>;
}) => {
triggerDestroyRecordsOptimisticEffect({
cache,
objectMetadataItem,
objectMetadataItems,
recordsToDelete: [
recordsToDestroy: [
{
...recordToDelete,
...recordToDestroy,
__typename: getObjectTypename(objectMetadataItem.nameSingular),
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,19 @@ export const useCreateManyRecords = <
},
})
.catch((error: Error) => {
recordsCreatedInCache.forEach((recordToDelete) => {
recordsCreatedInCache.forEach((recordToDestroy) => {
deleteRecordFromCache({
objectMetadataItems,
objectMetadataItem,
cache: apolloClient.cache,
recordToDelete,
recordToDestroy,
});
});

triggerDestroyRecordsOptimisticEffect({
cache: apolloClient.cache,
objectMetadataItem,
recordsToDelete: recordsCreatedInCache,
recordsToDestroy: recordsCreatedInCache,
objectMetadataItems,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ export const useCreateOneRecord = <
objectMetadataItems,
objectMetadataItem,
cache: apolloClient.cache,
recordToDelete: recordCreatedInCache,
recordToDestroy: recordCreatedInCache,
});

triggerDestroyRecordsOptimisticEffect({
cache: apolloClient.cache,
objectMetadataItem,
recordsToDelete: [recordCreatedInCache],
recordsToDestroy: [recordCreatedInCache],
objectMetadataItems,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const useDestroyManyRecords = ({
triggerDestroyRecordsOptimisticEffect({
cache,
objectMetadataItem,
recordsToDelete: cachedRecords,
recordsToDestroy: cachedRecords,
objectMetadataItems,
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const useDestroyOneRecord = ({
triggerDestroyRecordsOptimisticEffect({
cache,
objectMetadataItem,
recordsToDelete: [cachedRecord],
recordsToDestroy: [cachedRecord],
objectMetadataItems,
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export const usePersistViewFilterRecords = () => {
idToDestroy: viewFilterId,
},
update: (cache, { data }) => {
const record = data?.['deleteViewFilter'];
const record = data?.['destroyViewFilter'];

if (!record) return;

Expand All @@ -145,7 +145,7 @@ export const usePersistViewFilterRecords = () => {
triggerDestroyRecordsOptimisticEffect({
cache,
objectMetadataItem,
recordsToDelete: [cachedRecord],
recordsToDestroy: [cachedRecord],
objectMetadataItems,
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export const usePersistViewSortRecords = () => {
idToDestroy: viewSortId,
},
update: (cache, { data }) => {
const record = data?.['deleteViewSort'];
const record = data?.['destroyViewSort'];

if (!record) return;

Expand All @@ -140,7 +140,7 @@ export const usePersistViewSortRecords = () => {
triggerDestroyRecordsOptimisticEffect({
cache,
objectMetadataItem,
recordsToDelete: [cachedRecord],
recordsToDestroy: [cachedRecord],
objectMetadataItems,
});
},
Expand Down

0 comments on commit ec79de7

Please sign in to comment.