Files
twenty/packages/twenty-front/src/modules/workflow/hooks/useActivateWorkflowVersion.ts
Thomas Trompette 460253ab98 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.
2025-01-30 17:30:50 +01:00

49 lines
1.3 KiB
TypeScript

import { useApolloClient, useMutation } from '@apollo/client';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { useFindManyRecordsQuery } from '@/object-record/hooks/useFindManyRecordsQuery';
import { ACTIVATE_WORKFLOW_VERSION } from '@/workflow/graphql/mutations/activateWorkflowVersion';
import {
ActivateWorkflowVersionMutation,
ActivateWorkflowVersionMutationVariables,
} from '~/generated/graphql';
export const useActivateWorkflowVersion = () => {
const apolloClient = useApolloClient();
const [mutate] = useMutation<
ActivateWorkflowVersionMutation,
ActivateWorkflowVersionMutationVariables
>(ACTIVATE_WORKFLOW_VERSION, {
client: apolloClient,
});
const { findManyRecordsQuery: findManyWorkflowVersionsQuery } =
useFindManyRecordsQuery({
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
});
const activateWorkflowVersion = async ({
workflowVersionId,
workflowId,
}: {
workflowVersionId: string;
workflowId: string;
}) => {
await mutate({
variables: {
workflowVersionId,
},
refetchQueries: [
{
query: findManyWorkflowVersionsQuery,
variables: {
id: workflowId,
},
},
],
});
};
return { activateWorkflowVersion };
};