@@ -33,20 +33,38 @@ function importFiles(apiPath, formData) {
3333 xhr . open ( 'POST' , serverAddress . value + apiPath ) ;
3434 xhr . responseType = 'json' ;
3535 xhr . onload = function ( ) {
36- if ( xhr . status >= 200 && xhr . status < 300 ) {
36+ if ( xhr . status >= 200 && xhr . status <= 500 ) {
3737 try {
3838 const data = xhr . response ;
39- console . log ( data ) ;
40- if ( data && data . error ) {
41- error ( `Import Error: ${ data . error } ` ) ;
42- } else if ( data && ( data . import ?. length || data . update ?. length ) ) {
43- if ( data . import ?. length ) {
44- success ( "Success!" ) ;
45- success ( `Imported IDs: ${ data . import . join ( ', ' ) } ` ) ;
46- }
47- if ( data . update ?. length ) {
48- success ( "Success!" ) ;
49- success ( `Updated IDs: ${ data . update . join ( ', ' ) } ` ) ;
39+ if ( data ) {
40+ if ( data . error !== false ) {
41+ error ( `Import Error: ${ data . error } ` ) ;
42+ } else if ( data . import ?. length || data . update ?. length ) {
43+ success ( "Import Success!" ) ;
44+ if ( data . import ?. length ) {
45+ success ( `Imported new IDs: [ ${ data . import . join ( ', ' ) } ]` ) ;
46+ }
47+ if ( data . update ?. length ) {
48+ success ( `Updated existing IDs: [ ${ data . update . join ( ', ' ) } ]` ) ;
49+ }
50+ if ( data . discard ?. length ) {
51+ info ( `Discarded existing IDs: [ ${ data . discard . join ( ', ' ) } ]` ) ;
52+ }
53+ if ( data . fail ?. length ) {
54+ error ( `Failed IDs: [ ${ data . fail . join ( ', ' ) } ]` ) ;
55+ }
56+ } else if ( data . discard ?. length ) {
57+ info ( `No new lenses were imported because they already exist.` ) ;
58+ info ( `Discarded existing IDs: [ ${ data . discard . join ( ', ' ) } ]` ) ;
59+
60+ if ( data . fail ?. length ) {
61+ error ( `Failed IDs: [ ${ data . fail . join ( ', ' ) } ]` ) ;
62+ }
63+ } else if ( data . fail ?. length ) {
64+ error ( `Importing failed without error.` ) ;
65+ error ( `Failed IDs: [ ${ data . fail . join ( ', ' ) } ]` ) ;
66+ } else {
67+ info ( `The request was executed successfully, but no changes were made.` ) ;
5068 }
5169 } else {
5270 error ( `API Error: no response` ) ;
@@ -60,7 +78,7 @@ function importFiles(apiPath, formData) {
6078 }
6179 } ;
6280 xhr . onerror = function ( ) {
63- error ( "Network Error" ) ;
81+ error ( "Network Error: Make sure your server is reachable. " ) ;
6482 } ;
6583 xhr . send ( formData ) ;
6684}
@@ -69,6 +87,10 @@ function error(message) {
6987 out ( message , 'warning' ) ;
7088}
7189
90+ function info ( message ) {
91+ out ( message , 'info' ) ;
92+ }
93+
7294function success ( message ) {
7395 out ( message , 'success' ) ;
7496}
@@ -92,6 +114,14 @@ function parseLensId(path) {
92114 return null ;
93115}
94116
117+ function isUuid ( string ) {
118+ const uuid = string . match ( / [ a - f 0 - 9 ] { 32 } / gi)
119+ if ( uuid && uuid [ 0 ] ) {
120+ return true ;
121+ }
122+ return false ;
123+ }
124+
95125function isValidUrl ( string ) {
96126 try {
97127 new URL ( string ) ;
@@ -160,7 +190,13 @@ startCacheImport.addEventListener("click", function (e) {
160190
161191 response . innerHTML = "" ;
162192
193+ if ( ! cacheImportForm . has ( 'file[]' ) ) {
194+ error ( `Error: There is no file to upload.` ) ;
195+ return false ;
196+ }
197+
163198 importFiles ( importCacheApiPath , cacheImportForm ) ;
199+ return true ;
164200} ) ;
165201
166202addLensUpload . addEventListener ( "click" , function ( e ) {
@@ -182,14 +218,16 @@ resetLensUpload.addEventListener("click", function (e) {
182218
183219startLensUpload . addEventListener ( "click" , function ( e ) {
184220 e . preventDefault ( ) ;
185-
221+
186222 lensUploadForm = new FormData ( ) ;
187223
188224 response . innerHTML = "" ;
189225 let isErrorOccurred = false ;
190226
191227 const form = this . closest ( 'form' ) ;
192228 if ( form && ! form . checkValidity ( ) ) {
229+ error ( `Error: You must fill in the input fields correctly.` ) ;
230+ form . reportValidity ( ) ;
193231 return false ;
194232 }
195233
@@ -201,25 +239,33 @@ startLensUpload.addEventListener("click", function (e) {
201239 if ( fileInput . files . length > 0 ) {
202240 lensUploadForm . append ( 'file[]' , fileInput . files [ 0 ] ) ;
203241 } else {
204- error ( `Error: You have to select a .lns file.` ) ;
242+ error ( `Error: You have to select a .lns or .zip file.` ) ;
205243 isErrorOccurred = true ;
206244 }
207245
208246 const inputVal = idInput . value . trim ( ) ;
209247 const lensId = parseLensId ( inputVal ) ;
210248 if ( lensId ) {
211249 lensUploadForm . append ( 'id[]' , lensId ) ;
212- } else if ( isValidUrl ( inputVal ) ) {
250+ } else if ( isValidUrl ( inputVal ) || isUuid ( inputVal ) ) {
213251 lensUploadForm . append ( 'id[]' , inputVal ) ;
214252 } else {
215- error ( `Error: "${ inputVal } " is neither a valid Lens ID nor a share URL.` ) ;
253+ error ( `Error: "${ inputVal } " is neither a valid Lens ID nor a share URL or UUID .` ) ;
216254 isErrorOccurred = true ;
217255 }
218256 } ) ;
219257
220- if ( ! isErrorOccurred ) {
221- importFiles ( importLensApiPath , lensUploadForm ) ;
258+ if ( isErrorOccurred ) {
259+ return false ;
260+ }
261+
262+ if ( ! lensUploadForm . has ( 'id[]' ) || ! lensUploadForm . has ( 'file[]' ) ) {
263+ error ( `Error: There is no file to upload.` ) ;
264+ return false ;
222265 }
266+
267+ importFiles ( importLensApiPath , lensUploadForm ) ;
268+ return true ;
223269} ) ;
224270
225271document . addEventListener ( "DOMContentLoaded" , function ( ) {
0 commit comments