()
| 464 | } |
| 465 | |
| 466 | export function useUpdateWorkflow() { |
| 467 | const queryClient = useQueryClient() |
| 468 | |
| 469 | return useMutation({ |
| 470 | mutationFn: async (variables: UpdateWorkflowVariables) => { |
| 471 | const { workflow: updatedWorkflow } = await requestJson(updateWorkflowContract, { |
| 472 | params: { id: variables.workflowId }, |
| 473 | body: variables.metadata, |
| 474 | }) |
| 475 | |
| 476 | return mapWorkflow(updatedWorkflow) |
| 477 | }, |
| 478 | onMutate: async (variables) => { |
| 479 | await queryClient.cancelQueries({ |
| 480 | queryKey: workflowKeys.list(variables.workspaceId, 'active'), |
| 481 | }) |
| 482 | |
| 483 | const snapshot = queryClient.getQueryData<WorkflowMetadata[]>( |
| 484 | workflowKeys.list(variables.workspaceId, 'active') |
| 485 | ) |
| 486 | |
| 487 | queryClient.setQueryData<WorkflowMetadata[]>( |
| 488 | workflowKeys.list(variables.workspaceId, 'active'), |
| 489 | (old) => |
| 490 | (old ?? []).map((w) => |
| 491 | w.id === variables.workflowId |
| 492 | ? { ...w, ...variables.metadata, lastModified: new Date() } |
| 493 | : w |
| 494 | ) |
| 495 | ) |
| 496 | |
| 497 | return { snapshot } |
| 498 | }, |
| 499 | onError: (_error, variables, context) => { |
| 500 | if (context?.snapshot) { |
| 501 | queryClient.setQueryData( |
| 502 | workflowKeys.list(variables.workspaceId, 'active'), |
| 503 | context.snapshot |
| 504 | ) |
| 505 | } |
| 506 | }, |
| 507 | onSettled: (_data, _error, variables) => { |
| 508 | return invalidateWorkflowLists(queryClient, variables.workspaceId) |
| 509 | }, |
| 510 | }) |
| 511 | } |
| 512 | |
| 513 | interface DeleteWorkflowVariables { |
| 514 | workspaceId: string |
no test coverage detected