@@ -21,7 +21,7 @@ function createEmptyField() {
2121}
2222
2323// FUNCTION - FIELD ROW COMPONENT
24- function FieldRow ( { field, index, depth, collections, onChange, onRemove } ) {
24+ function FieldRow ( { field, index, depth, collections, collectionsLoading , collectionsError , onChange, onRemove } ) {
2525 const [ expanded , setExpanded ] = useState ( true ) ;
2626
2727 const handleChange = ( prop , value ) => {
@@ -283,11 +283,12 @@ function FieldRow({ field, index, depth, collections, onChange, onRemove }) {
283283 </ span >
284284 < select
285285 value = { field . ref || '' }
286+ disabled = { collectionsLoading || collectionsError }
286287 onChange = { ( e ) => handleChange ( 'ref' , e . target . value ) }
287288 className = "input-field"
288289 style = { { flex : 1 , fontSize : '0.85rem' , padding : '4px 8px' , background : 'rgba(0,0,0,0.2)' , border : '1px solid var(--color-border)' , borderRadius : '4px' } }
289290 >
290- < option value = "" > Select collection...</ option >
291+ < option value = "" > { collectionsLoading ? 'Loading collections…' : collectionsError ? 'Failed to load' : ' Select collection...' } </ option >
291292 { collections . map ( c => (
292293 < option key = { c . name } value = { c . name } > { c . name } </ option >
293294 ) ) }
@@ -320,6 +321,8 @@ function FieldRow({ field, index, depth, collections, onChange, onRemove }) {
320321 index = { subIdx }
321322 depth = { depth + 1 }
322323 collections = { collections }
324+ collectionsLoading = { collectionsLoading }
325+ collectionsError = { collectionsError }
323326 onChange = { handleSubFieldChange }
324327 onRemove = { removeSubField }
325328 />
@@ -370,6 +373,8 @@ function FieldRow({ field, index, depth, collections, onChange, onRemove }) {
370373 index = { subIdx }
371374 depth = { depth + 1 }
372375 collections = { collections }
376+ collectionsLoading = { collectionsLoading }
377+ collectionsError = { collectionsError }
373378 onChange = { handleItemSubFieldChange }
374379 onRemove = { removeItemSubField }
375380 />
@@ -384,11 +389,12 @@ function FieldRow({ field, index, depth, collections, onChange, onRemove }) {
384389 </ span >
385390 < select
386391 value = { field . items ?. ref || '' }
392+ disabled = { collectionsLoading || collectionsError }
387393 onChange = { ( e ) => handleItemsChange ( 'ref' , e . target . value ) }
388394 className = "input-field"
389395 style = { { flex : 1 , fontSize : '0.85rem' , padding : '4px 8px' , background : 'rgba(0,0,0,0.2)' , border : '1px solid var(--color-border)' , borderRadius : '4px' } }
390396 >
391- < option value = "" > Select collection...</ option >
397+ < option value = "" > { collectionsLoading ? 'Loading collections…' : collectionsError ? 'Failed to load' : ' Select collection...' } </ option >
392398 { collections . map ( c => (
393399 < option key = { c . name } value = { c . name } > { c . name } </ option >
394400 ) ) }
@@ -454,15 +460,31 @@ function CreateCollection() {
454460 const [ fields , setFields ] = useState ( getInitialFields ( ) ) ;
455461 const [ loading , setLoading ] = useState ( false ) ;
456462 const [ collections , setCollections ] = useState ( [ ] ) ;
463+ const [ collectionsLoading , setCollectionsLoading ] = useState ( true ) ;
464+ const [ collectionsError , setCollectionsError ] = useState ( null ) ;
457465
458- // Fetch existing collections for Ref picker
466+ // Fetch existing collections for Ref picker — runs immediately on mount
467+ // so it fires in parallel with any other in-flight requests.
459468 useEffect ( ( ) => {
460469 let isMounted = true ;
461470 const fetchCollections = async ( ) => {
471+ if ( isMounted ) {
472+ setCollectionsLoading ( true ) ;
473+ setCollections ( [ ] ) ;
474+ setCollectionsError ( null ) ;
475+ }
462476 try {
463477 const res = await api . get ( `/api/projects/${ projectId } ` ) ;
464478 if ( isMounted ) setCollections ( res . data . collections || [ ] ) ;
465- } catch { /* ignore */ }
479+ } catch ( err ) {
480+ console . error ( 'Failed to fetch collections for Ref picker:' , err ) ;
481+ if ( isMounted ) {
482+ setCollectionsError ( 'Failed to load collections' ) ;
483+ toast . error ( 'Failed to load collections for references' ) ;
484+ }
485+ } finally {
486+ if ( isMounted ) setCollectionsLoading ( false ) ;
487+ }
466488 } ;
467489 fetchCollections ( ) ;
468490 return ( ) => { isMounted = false ; } ;
@@ -598,6 +620,8 @@ function CreateCollection() {
598620 index = { index }
599621 depth = { 1 }
600622 collections = { collections }
623+ collectionsLoading = { collectionsLoading }
624+ collectionsError = { collectionsError }
601625 onChange = { handleFieldChange }
602626 onRemove = { removeField }
603627 />
0 commit comments