From c1effb5d7df12626df8d458b78130fd06502128c Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Wed, 28 Aug 2024 14:40:33 +0200 Subject: [PATCH] [Fix partially #574] Handle properly failed rename --- .../dialog/system/SystemEditDialog.tsx | 16 +++++++++------- src/hooks/useSystems.tsx | 10 +++++++--- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/components/dialog/system/SystemEditDialog.tsx b/src/components/dialog/system/SystemEditDialog.tsx index f68c6081..fae75447 100644 --- a/src/components/dialog/system/SystemEditDialog.tsx +++ b/src/components/dialog/system/SystemEditDialog.tsx @@ -35,13 +35,15 @@ const SystemEditDialog = ({ open, handleCloseDialog, system }: Props) => { const handleUpdateSystem = async (values: any) => { setIsProcessing(true); - - system.name = values.systemName; - await updateSystem(system); - - reset({ - systemName: values.systemName, - }); + const updatedSystem = { ...system, name: values.systemName }; + await updateSystem( + updatedSystem, + () => (system.name = values.systemName), + () => + reset({ + systemName: system.name, + }), + ); setIsProcessing(false); handleCloseDialog(); }; diff --git a/src/hooks/useSystems.tsx b/src/hooks/useSystems.tsx index f29622a4..7ff4e5c4 100644 --- a/src/hooks/useSystems.tsx +++ b/src/hooks/useSystems.tsx @@ -12,7 +12,7 @@ import { useUserAuth } from "@hooks/useUserAuth"; type systemContextType = [ System[], (systemToCreate: System) => void, - (systemToDelete: System) => void, + (systemToDelete: System, onSuccess: () => void, onFail: () => void) => void, (systemToUpdate: System) => void, () => void, ]; @@ -56,7 +56,7 @@ export const SystemsProvider = ({ children }: ChildrenProps) => { .catch((reason) => showSnackbar(reason, SnackbarType.ERROR)); }; - const updateSystem = async (systemToUpdate: System) => { + const updateSystem = async (systemToUpdate: System, onSuccess: () => void, onFail: () => void) => { systemService .rename(systemToUpdate) .then((value) => { @@ -66,8 +66,12 @@ export const SystemsProvider = ({ children }: ChildrenProps) => { _systems.splice(index, 1, systemToUpdate); _setSystems(_systems); + if (onSuccess) onSuccess(); }) - .catch((reason) => showSnackbar(reason, SnackbarType.ERROR)); + .catch((reason) => { + showSnackbar(reason, SnackbarType.ERROR); + if (onFail) onFail(); + }); }; const removeSystem = async (systemToRemove: System) => {