From 8ee03b82b114685dad4da05514454e28888a7930 Mon Sep 17 00:00:00 2001 From: Sacha Al Himdani Date: Wed, 3 Sep 2025 11:05:37 +0200 Subject: [PATCH] Update counter only on success Signed-off-by: Sacha Al Himdani --- .../src/components/tasks/TaskFormDialog.tsx | 6 ++++-- .../src/components/tasks/TasksCard.tsx | 4 ++-- .../src/hooks/useMutationWithIncrement.ts | 20 ++++++++++--------- .../measures/tabs/MeasureEvidencesTab.tsx | 16 ++++++++------- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/apps/console/src/components/tasks/TaskFormDialog.tsx b/apps/console/src/components/tasks/TaskFormDialog.tsx index 98643c4d3..4f86b1615 100644 --- a/apps/console/src/components/tasks/TaskFormDialog.tsx +++ b/apps/console/src/components/tasks/TaskFormDialog.tsx @@ -144,8 +144,10 @@ export default function TaskFormDialog(props: Props) { }, connections: [props.connection!], }, - onCompleted: () => { - updateStoreCounter(relayEnv, data.measureId, "tasks(first:0)", 1); + onCompleted: (_response, errors) => { + if (!errors) { + updateStoreCounter(relayEnv, data.measureId, "tasks(first:0)", 1); + } }, }); reset(); diff --git a/apps/console/src/components/tasks/TasksCard.tsx b/apps/console/src/components/tasks/TasksCard.tsx index ddf149ae4..8f8611341 100644 --- a/apps/console/src/components/tasks/TasksCard.tsx +++ b/apps/console/src/components/tasks/TasksCard.tsx @@ -165,8 +165,8 @@ function TaskRow(props: TaskRowProps) { input: { taskId: props.task.id }, connections: [props.connectionId], }, - onCompleted: () => { - if (params.measureId) { + onCompleted: (_response, errors) => { + if (!errors && params.measureId) { updateStoreCounter( relayEnv, params.measureId, diff --git a/apps/console/src/hooks/useMutationWithIncrement.ts b/apps/console/src/hooks/useMutationWithIncrement.ts index 09faeb91f..b6fab88c7 100644 --- a/apps/console/src/hooks/useMutationWithIncrement.ts +++ b/apps/console/src/hooks/useMutationWithIncrement.ts @@ -6,10 +6,10 @@ import { } from "react-relay"; import { commitLocalUpdate, + type Environment, type GraphQLTaggedNode, type MutationParameters, } from "relay-runtime"; -import type RelayModernEnvironment from "relay-runtime/lib/store/RelayModernEnvironment"; const defaultOptions = { field: "totalCount", @@ -36,13 +36,15 @@ export function useMutationWithIncrement( return mutate({ ...queryOptions, onCompleted: (response, error) => { - updateStoreCounter( - relayEnv, - options.id, - options.node, - options.value, - options.field, - ); + if (!error) { + updateStoreCounter( + relayEnv, + options.id, + options.node, + options.value, + options.field, + ); + } queryOptions.onCompleted?.(response, error); }, }); @@ -54,7 +56,7 @@ export function useMutationWithIncrement( } export function updateStoreCounter( - relayEnv: RelayModernEnvironment, + relayEnv: Environment, recordId: string, nodeName: string, value: number = 1, diff --git a/apps/console/src/pages/organizations/measures/tabs/MeasureEvidencesTab.tsx b/apps/console/src/pages/organizations/measures/tabs/MeasureEvidencesTab.tsx index 39bac0acb..4d1dadc90 100644 --- a/apps/console/src/pages/organizations/measures/tabs/MeasureEvidencesTab.tsx +++ b/apps/console/src/pages/organizations/measures/tabs/MeasureEvidencesTab.tsx @@ -180,13 +180,15 @@ function EvidenceRow(props: { evidenceId: evidence.id, }, }, - onCompleted: () => { - updateStoreCounter( - relayEnv, - props.measureId, - "evidences(first:0)", - -1 - ); + onCompleted: (_response, errors) => { + if (!errors) { + updateStoreCounter( + relayEnv, + props.measureId, + "evidences(first:0)", + -1 + ); + } }, }); },