@@ -23,8 +23,7 @@ async function validateUrlResponse(url) {
2323 return response ;
2424}
2525
26- async function validateCatalogContent ( response ) {
27- const data = await response . text ( ) ;
26+ async function validateCatalogContent ( data ) {
2827 let catalog ;
2928 try {
3029 catalog = parse ( data ) ;
@@ -44,6 +43,7 @@ export function ManageCatalogsModal({ show, onClose }) {
4443 const { catalogs, addCatalog, removeCatalog } = useCatalogs ( ) ;
4544 const [ title , setTitle ] = useState ( "" ) ;
4645 const [ url , setUrl ] = useState ( "" ) ;
46+ const [ catalogContent , setCatalogContent ] = useState ( "" ) ;
4747 const [ error , setError ] = useState ( null ) ;
4848 const [ showCatalogAdded , setShowCatalogAdded ] = useState ( false ) ;
4949
@@ -57,7 +57,8 @@ export function ManageCatalogsModal({ show, onClose }) {
5757 try {
5858 validateUrl ( url ) ;
5959 const response = await validateUrlResponse ( url ) ;
60- await validateCatalogContent ( response ) ;
60+ const data = await response . text ( ) ;
61+ await validateCatalogContent ( data ) ;
6162
6263 return true ;
6364 } catch ( e ) {
@@ -66,12 +67,21 @@ export function ManageCatalogsModal({ show, onClose }) {
6667 }
6768 } , [ ] ) ;
6869
69- function handleSubmit ( e ) {
70+ async function handleSubmit ( e ) {
7071 e . preventDefault ( ) ;
71- validate ( url )
72- . then ( ( ) => addCatalog ( title , url ) )
73- . then ( ( ) => setShowCatalogAdded ( true ) )
74- . then ( ( ) => reset ( ) ) ;
72+ setError ( null ) ;
73+
74+ if ( ! url && ! catalogContent ) {
75+ setError ( "Please provide either a URL or catalog content" ) ;
76+ return ;
77+ }
78+
79+ if ( url ) await validate ( url ) ;
80+ else await validateCatalogContent ( catalogContent ) ;
81+
82+ addCatalog ( { name : title , url, content : catalogContent } ) ;
83+ setShowCatalogAdded ( true ) ;
84+ reset ( ) ;
7585 }
7686
7787 function handleCancel ( ) {
@@ -137,17 +147,33 @@ export function ManageCatalogsModal({ show, onClose }) {
137147 />
138148 </ Form . Group >
139149
150+ < Alert variant = "info" >
151+ You can add a catalog by either providing the URL or the content of
152+ the catalog definition file. If both are provided, the URL will be
153+ used.
154+ </ Alert >
155+
140156 < Form . Group className = "mb-3" controlId = "labspaceComposeFile" >
141157 < Form . Label > Catalog URL</ Form . Label >
142158 < Form . Control
143159 type = "text"
144160 value = { url }
145161 onChange = { ( e ) => setUrl ( e . target . value ) }
146- required
147162 />
148163 < Form . Text muted > Location of the catalog definition file</ Form . Text >
149164 </ Form . Group >
150165
166+ < Form . Group className = "mb-3" controlId = "labspaceComposeFile" >
167+ < Form . Label > Catalog Content</ Form . Label >
168+ < Form . Control
169+ as = "textarea"
170+ rows = { 7 }
171+ value = { catalogContent }
172+ onChange = { ( e ) => setCatalogContent ( e . target . value ) }
173+ />
174+ < Form . Text muted > Content of the catalog definition file</ Form . Text >
175+ </ Form . Group >
176+
151177 { showCatalogAdded && (
152178 < Alert variant = "success" onClose = { ( ) => setShowCatalogAdded ( false ) } >
153179 Catalog added successfully!
0 commit comments