Skip to content

Commit db5de48

Browse files
committed
create collection immediately with a draft name
1 parent ec4df03 commit db5de48

File tree

4 files changed

+134
-59
lines changed

4 files changed

+134
-59
lines changed

packages/compass-data-modeling/src/components/diagram-editor.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
type DiagramState,
1616
selectCurrentModelFromState,
1717
createNewRelationship,
18-
startCreatingCollection,
18+
addCollection,
1919
} from '../store/diagram';
2020
import {
2121
Banner,
@@ -356,6 +356,6 @@ export default connect(
356356
{
357357
onRetryClick: retryAnalysis,
358358
onCancelClick: cancelAnalysis,
359-
onAddCollectionClick: startCreatingCollection,
359+
onAddCollectionClick: addCollection,
360360
}
361361
)(DiagramEditor);

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

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import {
1717
TextArea,
1818
} from '@mongodb-js/compass-components';
1919
import {
20-
addCollection,
2120
createNewRelationship,
2221
deleteRelationship,
22+
nameDraftCollection,
2323
renameCollection,
2424
selectCurrentModelFromState,
2525
selectRelationship,
@@ -34,10 +34,11 @@ import {
3434
import { useChangeOnBlur } from './use-change-on-blur';
3535

3636
type CollectionDrawerContentProps = {
37-
namespace?: string;
37+
namespace: string;
3838
collections: DataModelCollection[];
3939
note?: string;
4040
relationships: Relationship[];
41+
isDraftCollection?: boolean;
4142
onCreateNewRelationshipClick: (namespace: string) => void;
4243
onEditRelationshipClick: (rId: string) => void;
4344
onDeleteRelationshipClick: (rId: string) => void;
@@ -82,7 +83,7 @@ const relationshipContentStyles = css({
8283
export function getIsCollectionNameValid(
8384
collectionName: string,
8485
namespaces: string[],
85-
namespace?: string
86+
namespace: string
8687
): {
8788
isValid: boolean;
8889
errorMessage?: string;
@@ -96,10 +97,11 @@ export function getIsCollectionNameValid(
9697

9798
const namespacesWithoutCurrent = namespaces.filter((ns) => ns !== namespace);
9899

99-
const isDuplicate = namespacesWithoutCurrent.some((ns) => {
100-
const database = namespace ? toNS(namespace).database : toNS(ns).database;
101-
return ns.trim() === `${database}.${collectionName.trim()}`.trim();
102-
});
100+
const isDuplicate = namespacesWithoutCurrent.some(
101+
(ns) =>
102+
ns.trim() ===
103+
`${toNS(namespace).database}.${collectionName.trim()}`.trim()
104+
);
103105

104106
return {
105107
isValid: !isDuplicate,
@@ -114,6 +116,7 @@ const CollectionDrawerContent: React.FunctionComponent<
114116
collections,
115117
note = '',
116118
relationships,
119+
isDraftCollection,
117120
onCreateNewRelationshipClick,
118121
onEditRelationshipClick,
119122
onDeleteRelationshipClick,
@@ -127,13 +130,13 @@ const CollectionDrawerContent: React.FunctionComponent<
127130
const database = useMemo(() => toNS(namespaces[0]).database, [namespaces]); // TODO: what if there are no namespaces?
128131

129132
const { value: collectionName, ...nameInputProps } = useChangeOnBlur(
130-
namespace ? toNS(namespace).collection : 'new-collection',
133+
toNS(namespace).collection,
131134
(collectionName) => {
132135
const trimmedName = collectionName.trim();
133136
if (!isCollectionNameValid) {
134137
return;
135138
}
136-
if (!namespace) {
139+
if (isDraftCollection) {
137140
onCreateCollection(`${database}.${trimmedName}`);
138141
return;
139142
}
@@ -156,15 +159,15 @@ const CollectionDrawerContent: React.FunctionComponent<
156159
);
157160

158161
const noteInputProps = useChangeOnBlur(note, (newNote) => {
159-
if (namespace) onNoteChange(namespace, newNote);
162+
onNoteChange(namespace, newNote);
160163
});
161164

162165
const nameInputRef = useRef<HTMLInputElement | null>(null);
163166
useEffect(() => {
164-
if (!namespace) {
167+
if (isDraftCollection) {
165168
nameInputRef.current?.focus();
166169
}
167-
}, [namespace]);
170+
}, [isDraftCollection]);
168171

169172
return (
170173
<>
@@ -191,9 +194,8 @@ const CollectionDrawerContent: React.FunctionComponent<
191194
<Button
192195
className={titleBtnStyles}
193196
size="xsmall"
194-
disabled={!namespace}
195197
onClick={() => {
196-
if (namespace) onCreateNewRelationshipClick(namespace);
198+
onCreateNewRelationshipClick(namespace);
197199
}}
198200
>
199201
Add relationship
@@ -253,27 +255,26 @@ const CollectionDrawerContent: React.FunctionComponent<
253255

254256
<DMDrawerSection label="Notes">
255257
<DMFormFieldContainer>
256-
<TextArea
257-
label=""
258-
aria-label="Notes"
259-
disabled={!namespace}
260-
{...noteInputProps}
261-
></TextArea>
258+
<TextArea label="" aria-label="Notes" {...noteInputProps}></TextArea>
262259
</DMFormFieldContainer>
263260
</DMDrawerSection>
264261
</>
265262
);
266263
};
267264

268265
export default connect(
269-
(state: DataModelingState, ownProps: { namespace?: string }) => {
266+
(state: DataModelingState, ownProps: { namespace: string }) => {
270267
const model = selectCurrentModelFromState(state);
271268
const collection = model.collections.find((collection) => {
272269
return collection.ns === ownProps.namespace;
273270
});
271+
if (!collection) {
272+
throw new Error('Namespace not found in model: ' + ownProps.namespace);
273+
}
274274
return {
275-
note: collection?.note,
276-
namespace: collection?.ns,
275+
note: collection.note,
276+
namespace: collection.ns,
277+
isDraftCollection: state.diagram?.draftCollection === ownProps.namespace,
277278
collections: model.collections,
278279
relationships: model.relationships.filter((r) => {
279280
const [local, foreign] = r.relationship;
@@ -289,6 +290,6 @@ export default connect(
289290
onDeleteRelationshipClick: deleteRelationship,
290291
onNoteChange: updateCollectionNote,
291292
onRenameCollection: renameCollection,
292-
onCreateCollection: addCollection,
293+
onCreateCollection: nameDraftCollection,
293294
}
294295
)(CollectionDrawerContent);

packages/compass-data-modeling/src/components/drawer/diagram-editor-side-panel.tsx

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const drawerTitleActionGroupStyles = css({});
3333

3434
type DiagramEditorSidePanelProps = {
3535
selectedItems: {
36-
id?: string;
36+
id: string;
3737
type: 'relationship' | 'collection';
3838
label: string;
3939
} | null;
@@ -52,30 +52,26 @@ function DiagramEditorSidePanel({
5252
label: selectedItems.label,
5353
content: (
5454
<CollectionDrawerContent
55-
key={selectedItems.id || 'new-collection'}
55+
key={selectedItems.id}
5656
namespace={selectedItems.id}
5757
></CollectionDrawerContent>
5858
),
5959
actions: [
60-
...(selectedItems.id
61-
? [
62-
{
63-
action: 'delete',
64-
label: 'Delete Collection',
65-
icon: 'Trash' as const,
66-
},
67-
]
68-
: []),
60+
{
61+
action: 'delete',
62+
label: 'Delete Collection',
63+
icon: 'Trash' as const,
64+
},
6965
],
7066
handleAction: (actionName: string) => {
71-
if (actionName === 'delete' && selectedItems.id) {
67+
if (actionName === 'delete') {
7268
onDeleteCollection(selectedItems.id);
7369
}
7470
},
7571
};
7672
}
7773

78-
if (selectedItems?.type === 'relationship' && selectedItems.id) {
74+
if (selectedItems?.type === 'relationship') {
7975
return {
8076
label: selectedItems.label,
8177
content: (
@@ -88,7 +84,7 @@ function DiagramEditorSidePanel({
8884
{ action: 'delete', label: 'Delete', icon: 'Trash' as const },
8985
],
9086
handleAction: (actionName: string) => {
91-
if (actionName === 'delete' && selectedItems.id) {
87+
if (actionName === 'delete') {
9288
onDeleteRelationship(selectedItems.id);
9389
}
9490
},

0 commit comments

Comments
 (0)