Skip to content

Commit

Permalink
Merge pull request #579 from kbss-cvut/fix/fix-574-system-rename-shou…
Browse files Browse the repository at this point in the history
…ld-not-create-duplicate-names

Handle properly failed rename
  • Loading branch information
kostobog authored Aug 28, 2024
2 parents 7f7507e + c1effb5 commit c72ec39
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
16 changes: 9 additions & 7 deletions src/components/dialog/system/SystemEditDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
Expand Down
10 changes: 7 additions & 3 deletions src/hooks/useSystems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
Expand Down Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand Down

0 comments on commit c72ec39

Please sign in to comment.