@@ -17,9 +17,9 @@ import {
1717 TextArea ,
1818} from '@mongodb-js/compass-components' ;
1919import {
20- addCollection ,
2120 createNewRelationship ,
2221 deleteRelationship ,
22+ nameDraftCollection ,
2323 renameCollection ,
2424 selectCurrentModelFromState ,
2525 selectRelationship ,
@@ -34,10 +34,11 @@ import {
3434import { useChangeOnBlur } from './use-change-on-blur' ;
3535
3636type 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({
8283export 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
268265export 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 ) ;
0 commit comments