From 4c4d9967440f9c18f7f31c38a0995ff8083d5b2d Mon Sep 17 00:00:00 2001 From: Thomas Trompette Date: Fri, 10 Jan 2025 17:53:08 +0100 Subject: [PATCH] Update cache queries --- .../hooks/useActivateWorkflowVersion.ts | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/twenty-front/src/modules/workflow/hooks/useActivateWorkflowVersion.ts b/packages/twenty-front/src/modules/workflow/hooks/useActivateWorkflowVersion.ts index 5797c9707399..7002a52d5943 100644 --- a/packages/twenty-front/src/modules/workflow/hooks/useActivateWorkflowVersion.ts +++ b/packages/twenty-front/src/modules/workflow/hooks/useActivateWorkflowVersion.ts @@ -1,6 +1,7 @@ import { useApolloMetadataClient } from '@/object-metadata/hooks/useApolloMetadataClient'; 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'; @@ -56,7 +57,16 @@ export const useActivateWorkflowVersion = () => { item.workflowId === workflowId, ); - for (const workflowVersion of allWorkflowVersions) { + const previousActiveWorkflowVersions = allWorkflowVersions.filter( + (version) => + version.status === 'ACTIVE' && version.id !== workflowVersionId, + ); + + if (!previousActiveWorkflowVersions) { + return; + } + + for (const workflowVersion of previousActiveWorkflowVersions) { apolloClient.cache.modify({ id: apolloClient.cache.identify(workflowVersion), fields: { @@ -68,6 +78,17 @@ export const useActivateWorkflowVersion = () => { }, }, }); + + triggerUpdateRecordOptimisticEffect({ + cache: apolloClient.cache, + objectMetadataItem: objectMetadataItemWorkflowVersion, + currentRecord: workflowVersion, + updatedRecord: { + ...workflowVersion, + status: 'ARCHIVED', + }, + objectMetadataItems: [objectMetadataItemWorkflowVersion], + }); } }, });