Skip to content

Commit 5e72896

Browse files
committed
fix: try out and revert invalid edit COMMIT-9945
1 parent f7170fb commit 5e72896

File tree

1 file changed

+40
-23
lines changed
  • packages/compass-data-modeling/src/store

1 file changed

+40
-23
lines changed

packages/compass-data-modeling/src/store/diagram.ts

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ export type DiagramState =
5959
current: [Edit, ...Edit[]];
6060
next: Edit[][];
6161
};
62-
editErrors?: string[];
6362
selectedItems: SelectedItems | null;
6463
isNewlyCreated: boolean;
6564
draftCollection?: string;
@@ -72,9 +71,9 @@ export enum DiagramActionTypes {
7271
RENAME_DIAGRAM = 'data-modeling/diagram/RENAME_DIAGRAM',
7372
APPLY_INITIAL_LAYOUT = 'data-modeling/diagram/APPLY_INITIAL_LAYOUT',
7473
APPLY_EDIT = 'data-modeling/diagram/APPLY_EDIT',
75-
APPLY_EDIT_FAILED = 'data-modeling/diagram/APPLY_EDIT_FAILED',
7674
UNDO_EDIT = 'data-modeling/diagram/UNDO_EDIT',
7775
REDO_EDIT = 'data-modeling/diagram/REDO_EDIT',
76+
REVERT_FAILED_EDIT = 'data-modeling/diagram/REVERT_FAILED_EDIT',
7877
COLLECTION_SELECTED = 'data-modeling/diagram/COLLECTION_SELECTED',
7978
RELATIONSHIP_SELECTED = 'data-modeling/diagram/RELATIONSHIP_SELECTED',
8079
FIELD_SELECTED = 'data-modeling/diagram/FIELD_SELECTED',
@@ -102,15 +101,14 @@ export type ApplyEditAction = {
102101
edit: Edit;
103102
};
104103

105-
export type ApplyEditFailedAction = {
106-
type: DiagramActionTypes.APPLY_EDIT_FAILED;
107-
errors: string[];
108-
};
109-
110104
export type UndoEditAction = {
111105
type: DiagramActionTypes.UNDO_EDIT;
112106
};
113107

108+
export type RevertFailedEditAction = {
109+
type: DiagramActionTypes.REVERT_FAILED_EDIT;
110+
};
111+
114112
export type RedoEditAction = {
115113
type: DiagramActionTypes.REDO_EDIT;
116114
};
@@ -140,7 +138,7 @@ export type DiagramActions =
140138
| DeleteDiagramAction
141139
| RenameDiagramAction
142140
| ApplyEditAction
143-
| ApplyEditFailedAction
141+
| RevertFailedEditAction
144142
| UndoEditAction
145143
| RedoEditAction
146144
| CollectionSelectedAction
@@ -254,23 +252,23 @@ export const diagramReducer: Reducer<DiagramState> = (
254252
action.edit.type === 'AddCollection' ? action.edit.ns : undefined,
255253
};
256254
}
257-
if (isAction(action, DiagramActionTypes.APPLY_EDIT_FAILED)) {
258-
return {
259-
...state,
260-
editErrors: action.errors,
261-
};
262-
}
263-
if (isAction(action, DiagramActionTypes.UNDO_EDIT)) {
255+
if (
256+
isAction(action, DiagramActionTypes.UNDO_EDIT) ||
257+
isAction(action, DiagramActionTypes.REVERT_FAILED_EDIT)
258+
) {
264259
const newCurrent = state.edits.prev.pop() || [];
265260
if (!isNonEmptyArray(newCurrent)) {
266261
return state;
267262
}
263+
const next = isAction(action, DiagramActionTypes.REVERT_FAILED_EDIT)
264+
? [...state.edits.next]
265+
: [...state.edits.next, state.edits.current];
268266
return {
269267
...state,
270268
edits: {
271269
prev: [...state.edits.prev],
272270
current: newCurrent,
273-
next: [...state.edits.next, state.edits.current],
271+
next,
274272
},
275273
updatedAt: new Date().toISOString(),
276274
};
@@ -594,27 +592,46 @@ export function renameCollection(
594592
return applyEdit(edit);
595593
}
596594

595+
function handleError(messages: string[]) {
596+
openToast('data-modeling-error', {
597+
variant: 'warning',
598+
title: 'Error opening diagram',
599+
description: messages.join(' '),
600+
});
601+
}
602+
597603
export function applyEdit(
598604
rawEdit: EditAction
599-
): DataModelingThunkAction<boolean, ApplyEditAction | ApplyEditFailedAction> {
605+
): DataModelingThunkAction<boolean, ApplyEditAction | RevertFailedEditAction> {
600606
return (dispatch, getState, { dataModelStorage }) => {
601607
const edit = {
602608
...rawEdit,
603609
id: new UUID().toString(),
604610
timestamp: new Date().toISOString(),
605611
};
606-
const { result: isValid, errors } = validateEdit(edit);
607-
if (!isValid) {
608-
dispatch({
609-
type: DiagramActionTypes.APPLY_EDIT_FAILED,
610-
errors,
611-
});
612+
const { result, errors } = validateEdit(edit);
613+
let isValid = result;
614+
if (!result) {
615+
handleError(errors);
612616
return isValid;
613617
}
614618
dispatch({
615619
type: DiagramActionTypes.APPLY_EDIT,
616620
edit,
617621
});
622+
623+
// try to build the model with the latest edit
624+
try {
625+
selectCurrentModelFromState(getState());
626+
} catch (e) {
627+
handleError([
628+
'Something went wrong when applying the changes.',
629+
(e as Error).message,
630+
]);
631+
dispatch({ type: DiagramActionTypes.REVERT_FAILED_EDIT });
632+
isValid = false;
633+
}
634+
618635
void dataModelStorage.save(getCurrentDiagramFromState(getState()));
619636
return isValid;
620637
};

0 commit comments

Comments
 (0)