@@ -4,6 +4,7 @@ import { isAction } from './util';
44import type {
55 Edit ,
66 MongoDBDataModelDescription ,
7+ StaticModel ,
78} from '../services/data-model-storage' ;
89import { AnalysisProcessActionTypes } from './analysis-process' ;
910import { memoize } from 'lodash' ;
@@ -13,9 +14,9 @@ import { showConfirmation, showPrompt } from '@mongodb-js/compass-components';
1314export type DiagramState =
1415 | ( Omit < MongoDBDataModelDescription , 'edits' > & {
1516 edits : {
16- prev : Edit [ ] ;
17+ prev : Edit [ ] [ ] ;
1718 current : Edit [ ] ;
18- next : Edit [ ] ;
19+ next : Edit [ ] [ ] ;
1920 } ;
2021 } )
2122 | null ; // null when no diagram is currently open
@@ -47,8 +48,7 @@ export type RenameDiagramAction = {
4748
4849export type ApplyEditAction = {
4950 type : DiagramActionTypes . APPLY_EDIT ;
50- // TODO
51- edit : unknown ;
51+ edit : Edit ;
5252} ;
5353
5454export type UndoEditAction = {
@@ -86,27 +86,35 @@ export const diagramReducer: Reducer<DiagramState> = (
8686 } ;
8787 }
8888
89- // if (isAction(action, AnalysisProcessActionTypes.ANALYSIS_FINISHED)) {
90- // return {
91- // id: new UUID().toString(),
92- // name: action.name,
93- // connectionId: action.connectionId,
94- // edits: {
95- // prev: [],
96- // current: [
97- // {
98- // // TODO
99- // type: 'SetModel',
100- // model: {
101- // schema: action.schema,
102- // relations: action.relations,
103- // },
104- // },
105- // ],
106- // next: [],
107- // },
108- // };
109- // }
89+ if ( isAction ( action , AnalysisProcessActionTypes . ANALYSIS_FINISHED ) ) {
90+ return {
91+ id : new UUID ( ) . toString ( ) ,
92+ name : action . name ,
93+ connectionId : action . connectionId ,
94+ edits : {
95+ prev : [ ] ,
96+ current : [
97+ {
98+ type : 'SetModel' ,
99+ id : new UUID ( ) . toString ( ) ,
100+ timestamp : new Date ( ) . toISOString ( ) ,
101+ model : {
102+ collections : action . collections . map ( ( collection ) => ( {
103+ ns : collection . ns ,
104+ jsonSchema : collection . schema ,
105+ // TODO
106+ indexes : [ ] ,
107+ shardKey : undefined ,
108+ displayPosition : [ 0 , 0 ] ,
109+ } ) ) ,
110+ relationships : action . relations ,
111+ } ,
112+ } ,
113+ ] ,
114+ next : [ ] ,
115+ } ,
116+ } ;
117+ }
110118
111119 // All actions below are only applicable when diagram is open
112120 if ( ! state ) {
@@ -120,6 +128,7 @@ export const diagramReducer: Reducer<DiagramState> = (
120128 } ;
121129 }
122130 if ( isAction ( action , DiagramActionTypes . APPLY_EDIT ) ) {
131+ console . log ( 'Applying edit' , action . edit ) ;
123132 return {
124133 ...state ,
125134 edits : {
@@ -129,34 +138,34 @@ export const diagramReducer: Reducer<DiagramState> = (
129138 } ,
130139 } ;
131140 }
132- // if (isAction(action, DiagramActionTypes.UNDO_EDIT)) {
133- // const newCurrent = state.edits.prev.pop();
134- // if (!newCurrent) {
135- // return state;
136- // }
137- // return {
138- // ...state,
139- // edits: {
140- // prev: [...state.edits.prev],
141- // current: newCurrent,
142- // next: [...state.edits.next, state.edits.current],
143- // },
144- // };
145- // }
146- // if (isAction(action, DiagramActionTypes.REDO_EDIT)) {
147- // const newCurrent = state.edits.next.pop();
148- // if (!newCurrent) {
149- // return state;
150- // }
151- // return {
152- // ...state,
153- // edits: {
154- // prev: [...state.edits.prev, state.edits.current],
155- // current: newCurrent,
156- // next: [...state.edits.next],
157- // },
158- // };
159- // }
141+ if ( isAction ( action , DiagramActionTypes . UNDO_EDIT ) ) {
142+ const newCurrent = state . edits . prev . pop ( ) ;
143+ if ( ! newCurrent ) {
144+ return state ;
145+ }
146+ return {
147+ ...state ,
148+ edits : {
149+ prev : [ ...state . edits . prev ] ,
150+ current : newCurrent ,
151+ next : [ ...state . edits . next , state . edits . current ] ,
152+ } ,
153+ } ;
154+ }
155+ if ( isAction ( action , DiagramActionTypes . REDO_EDIT ) ) {
156+ const newCurrent = state . edits . next . pop ( ) ;
157+ if ( ! newCurrent ) {
158+ return state ;
159+ }
160+ return {
161+ ...state ,
162+ edits : {
163+ prev : [ ...state . edits . prev , state . edits . current ] ,
164+ current : newCurrent ,
165+ next : [ ...state . edits . next ] ,
166+ } ,
167+ } ;
168+ }
160169 return state ;
161170} ;
162171
@@ -175,7 +184,7 @@ export function redoEdit(): DataModelingThunkAction<void, RedoEditAction> {
175184}
176185
177186export function applyEdit (
178- edit : unknown
187+ edit : Edit
179188) : DataModelingThunkAction < void , ApplyEditAction > {
180189 return ( dispatch , getState , { dataModelStorage } ) => {
181190 dispatch ( { type : DiagramActionTypes . APPLY_EDIT , edit } ) ;
@@ -230,20 +239,29 @@ export function renameDiagram(
230239 } ;
231240}
232241
233- // TODO
234- function _applyEdit ( model : any , edit : any ) {
235- if ( edit && 'type' in edit ) {
236- if ( edit . type === 'SetModel' ) {
237- return edit . model ;
238- }
242+ function _applyEdit ( edit : Edit , model ?: StaticModel ) : StaticModel {
243+ if ( edit . type === 'SetModel' ) {
244+ return edit . model ;
245+ }
246+ if ( ! model ) {
247+ throw new Error ( 'Editing a model that has not been initialized' ) ;
248+ }
249+ if ( edit . type === 'AddRelationship' ) {
250+ return {
251+ ...model ,
252+ relationships : [ ...model . relationships , edit . relationship ] ,
253+ } ;
239254 }
240255 return model ;
241256}
242257
243- export function getCurrentModel ( diagram : MongoDBDataModelDescription ) : unknown {
244- let model ;
258+ export function getCurrentModel (
259+ diagram : MongoDBDataModelDescription
260+ ) : StaticModel {
261+ let model : StaticModel ;
245262 for ( const edit of diagram . edits ) {
246- model = _applyEdit ( model , edit ) ;
263+ console . log ( 'Applying edit' , edit , diagram ) ;
264+ model = _applyEdit ( edit , model ) ;
247265 }
248266 return model ;
249267}
0 commit comments