Use refetch instead of cache modify when activating/deactivating a workflow (#9937)
Workflow version statuses are too complex to handle in cache. You always need to: - modify entity in cache - modify queries in cache - do the same for all other version statuses that should also be updated This complex behavior leads to bugs because of a cache wrongly updated. Let's simply do a refetch versions on status update.
This commit is contained in:
@ -1,12 +1,8 @@
|
||||
import { useApolloClient, useMutation } from '@apollo/client';
|
||||
|
||||
import { triggerUpdateRecordOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerUpdateRecordOptimisticEffect';
|
||||
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
|
||||
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
|
||||
import { modifyRecordFromCache } from '@/object-record/cache/utils/modifyRecordFromCache';
|
||||
import { useFindManyRecordsQuery } from '@/object-record/hooks/useFindManyRecordsQuery';
|
||||
import { DEACTIVATE_WORKFLOW_VERSION } from '@/workflow/graphql/mutations/deactivateWorkflowVersion';
|
||||
import { WorkflowVersion } from '@/workflow/types/Workflow';
|
||||
import { isDefined } from 'twenty-ui';
|
||||
import {
|
||||
DeactivateWorkflowVersionMutation,
|
||||
DeactivateWorkflowVersionMutationVariables,
|
||||
@ -21,8 +17,8 @@ export const useDeactivateWorkflowVersion = () => {
|
||||
client: apolloClient,
|
||||
});
|
||||
|
||||
const { objectMetadataItem: objectMetadataItemWorkflowVersion } =
|
||||
useObjectMetadataItem({
|
||||
const { findManyRecordsQuery: findManyWorkflowVersionsQuery } =
|
||||
useFindManyRecordsQuery({
|
||||
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
|
||||
});
|
||||
|
||||
@ -31,40 +27,14 @@ export const useDeactivateWorkflowVersion = () => {
|
||||
variables: {
|
||||
workflowVersionId,
|
||||
},
|
||||
update: () => {
|
||||
modifyRecordFromCache({
|
||||
cache: apolloClient.cache,
|
||||
recordId: workflowVersionId,
|
||||
objectMetadataItem: objectMetadataItemWorkflowVersion,
|
||||
fieldModifiers: {
|
||||
status: () => 'DEACTIVATED',
|
||||
refetchQueries: [
|
||||
{
|
||||
query: findManyWorkflowVersionsQuery,
|
||||
variables: {
|
||||
id: workflowVersionId,
|
||||
},
|
||||
});
|
||||
|
||||
const cacheSnapshot = apolloClient.cache.extract();
|
||||
const workflowVersion: WorkflowVersion | undefined = Object.values(
|
||||
cacheSnapshot,
|
||||
).find(
|
||||
(item) =>
|
||||
item.__typename === 'WorkflowVersion' &&
|
||||
item.id === workflowVersionId,
|
||||
);
|
||||
|
||||
if (!isDefined(workflowVersion)) {
|
||||
return;
|
||||
}
|
||||
|
||||
triggerUpdateRecordOptimisticEffect({
|
||||
cache: apolloClient.cache,
|
||||
objectMetadataItem: objectMetadataItemWorkflowVersion,
|
||||
currentRecord: workflowVersion,
|
||||
updatedRecord: {
|
||||
...workflowVersion,
|
||||
status: 'DEACTIVATED',
|
||||
},
|
||||
objectMetadataItems: [objectMetadataItemWorkflowVersion],
|
||||
});
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user