Skip to content

Commit 4a20b0e

Browse files
committed
address PR suggestions
1 parent 6a9fe0f commit 4a20b0e

File tree

4 files changed

+39
-35
lines changed

4 files changed

+39
-35
lines changed

packages/compass-components/src/components/drawer-portal.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ type DrawerSectionProps = Omit<SectionData, 'content' | 'onClick'> & {
3232
order?: number;
3333
};
3434

35+
type DrawerOpenStateContextValue = {
36+
current: {
37+
isDrawerOpen: boolean;
38+
};
39+
};
40+
3541
type DrawerActionsContextValue = {
3642
current: {
3743
openDrawer: (id: string) => void;
@@ -43,6 +49,14 @@ type DrawerActionsContextValue = {
4349

4450
const DrawerStateContext = React.createContext<DrawerSectionProps[]>([]);
4551

52+
const DrawerOpenStateContext = React.createContext<DrawerOpenStateContextValue>(
53+
{
54+
current: {
55+
isDrawerOpen: false,
56+
},
57+
}
58+
);
59+
4660
const DrawerActionsContext = React.createContext<DrawerActionsContextValue>({
4761
current: {
4862
openDrawer: () => undefined,
@@ -126,8 +140,10 @@ export const DrawerContentProvider: React.FunctionComponent = ({
126140
const DrawerContextGrabber: React.FunctionComponent = ({ children }) => {
127141
const drawerToolbarContext = useDrawerToolbarContext();
128142
const actions = useContext(DrawerActionsContext);
143+
const state = useContext(DrawerOpenStateContext);
129144
actions.current.openDrawer = drawerToolbarContext.openDrawer;
130145
actions.current.closeDrawer = drawerToolbarContext.closeDrawer;
146+
state.current.isDrawerOpen = drawerToolbarContext.isDrawerOpen;
131147
return <>{children}</>;
132148
};
133149

@@ -323,11 +339,11 @@ export function useDrawerActions() {
323339
}
324340

325341
export const useDrawerState = () => {
326-
const drawerToolbarContext = useDrawerToolbarContext();
342+
const drawerToolbarContext = useContext(DrawerOpenStateContext);
327343
const drawerState = useContext(DrawerStateContext);
328344
return {
329345
isOpen:
330-
drawerToolbarContext.isDrawerOpen &&
346+
drawerToolbarContext.current.isDrawerOpen &&
331347
// the second check is a workaround, because LG doesn't set isDrawerOpen to false when it's empty
332348
drawerState.length > 0,
333349
};

packages/compass-data-modeling/src/components/drawer/collection-drawer-content.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
import {
2020
createNewRelationship,
2121
deleteRelationship,
22-
nameDraftCollection,
2322
renameCollection,
2423
selectCurrentModelFromState,
2524
selectRelationship,
@@ -44,7 +43,6 @@ type CollectionDrawerContentProps = {
4443
onDeleteRelationshipClick: (rId: string) => void;
4544
onNoteChange: (namespace: string, note: string) => void;
4645
onRenameCollection: (fromNS: string, toNS: string) => void;
47-
onCreateCollection: (ns: string, position?: [number, number]) => void;
4846
};
4947

5048
const titleBtnStyles = css({
@@ -122,25 +120,18 @@ const CollectionDrawerContent: React.FunctionComponent<
122120
onDeleteRelationshipClick,
123121
onNoteChange,
124122
onRenameCollection,
125-
onCreateCollection,
126123
}) => {
127124
const namespaces = useMemo(() => {
128125
return collections.map((c) => c.ns);
129126
}, [collections]);
130-
const database = useMemo(() => toNS(namespaces[0]).database, [namespaces]); // TODO(COMPASS-9718) use diagram.database
131-
132127
const { value: collectionName, ...nameInputProps } = useChangeOnBlur(
133128
toNS(namespace).collection,
134129
(collectionName) => {
135130
const trimmedName = collectionName.trim();
136131
if (!isCollectionNameValid) {
137132
return;
138133
}
139-
if (isDraftCollection) {
140-
onCreateCollection(`${database}.${trimmedName}`);
141-
return;
142-
}
143-
if (trimmedName === toNS(namespace).collection) {
134+
if (!isDraftCollection && trimmedName === toNS(namespace).collection) {
144135
return;
145136
}
146137
onRenameCollection(
@@ -290,6 +281,5 @@ export default connect(
290281
onDeleteRelationshipClick: deleteRelationship,
291282
onNoteChange: updateCollectionNote,
292283
onRenameCollection: renameCollection,
293-
onCreateCollection: nameDraftCollection,
294284
}
295285
)(CollectionDrawerContent);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
undoEdit,
1010
selectFieldsForCurrentModel,
1111
addCollection,
12-
nameDraftCollection,
12+
renameCollection,
1313
} from './diagram';
1414
import type {
1515
Edit,
@@ -260,7 +260,9 @@ describe('Data Modeling store', function () {
260260

261261
// name the new collection
262262
const newCollectionNs = 'db.myCollection';
263-
store.dispatch(nameDraftCollection(newCollectionNs));
263+
store.dispatch(
264+
renameCollection(firstCollectionDraftName, newCollectionNs)
265+
);
264266

265267
// now the collection is added to the edit history
266268
const diagramAfterCreation = getCurrentDiagramFromState(store.getState());

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -197,22 +197,23 @@ export const diagramReducer: Reducer<DiagramState> = (
197197
updatedAt: new Date().toISOString(),
198198
};
199199
}
200-
if (isAction(action, DiagramActionTypes.DRAFT_COLLECTION_NAMED)) {
201-
if (!state.draftCollection) {
202-
throw new Error('There is no draft collection to name');
203-
}
200+
if (
201+
isAction(action, DiagramActionTypes.APPLY_EDIT) &&
202+
state.draftCollection &&
203+
action.edit.type === 'RenameCollection'
204+
) {
204205
return {
205206
...state,
206207
edits: getEditsAfterDraftCollectionNamed(
207208
state.edits,
208209
state.draftCollection,
209-
action.namespace
210+
action.edit.toNS
210211
),
211212
editErrors: undefined,
212213
updatedAt: new Date().toISOString(),
213214
selectedItems: {
214215
type: 'collection',
215-
id: action.namespace,
216+
id: action.edit.toNS,
216217
},
217218
draftCollection: undefined,
218219
};
@@ -341,12 +342,11 @@ const updateSelectedItemsFromAppliedEdit = (
341342
currentSelection: SelectedItems | null,
342343
edit: Edit
343344
): SelectedItems | null => {
344-
if (!currentSelection) {
345-
return currentSelection;
346-
}
347-
348345
switch (edit.type) {
349346
case 'RenameCollection': {
347+
if (!currentSelection) {
348+
return currentSelection;
349+
}
350350
if (
351351
currentSelection?.type === 'collection' &&
352352
currentSelection.id === edit.fromNS
@@ -358,6 +358,12 @@ const updateSelectedItemsFromAppliedEdit = (
358358
}
359359
break;
360360
}
361+
case 'AddCollection': {
362+
return {
363+
type: 'collection',
364+
id: edit.ns,
365+
};
366+
}
361367
}
362368

363369
return currentSelection;
@@ -467,15 +473,6 @@ export function renameCollection(
467473
};
468474
}
469475

470-
export function nameDraftCollection(
471-
namespace: string
472-
): DataModelingThunkAction<void, DraftCollectionNamedAction> {
473-
return (dispatch, getState, { dataModelStorage }) => {
474-
dispatch({ type: DiagramActionTypes.DRAFT_COLLECTION_NAMED, namespace });
475-
void dataModelStorage.save(getCurrentDiagramFromState(getState()));
476-
};
477-
}
478-
479476
export function applyEdit(
480477
rawEdit: EditAction
481478
): DataModelingThunkAction<boolean, ApplyEditAction | ApplyEditFailedAction> {
@@ -694,7 +691,6 @@ export function addCollection(
694691
position,
695692
};
696693
dispatch(applyEdit(edit));
697-
dispatch(selectCollection(ns));
698694
return true;
699695
};
700696
}

0 commit comments

Comments
 (0)