@@ -4,20 +4,23 @@ import { useMemo, useLayoutEffect, useState, useEffect } from "@wordpress/elemen
44import { __ } from "@wordpress/i18n" ;
55
66export default function Edit ( { attributes, setAttributes } ) {
7+ const [ errorMessage , setErrorMessage ] = useState ( null ) ;
78 const [ isTypingUrl , setIsTypingUrl ] = useState ( false ) ;
89 const [ datasets , setDatasets ] = useState ( null ) ;
910 const [ selectedDatasets , setSelectedDatasets ] = useState ( attributes . selected_datasets ) ;
1011 const isValidURL = useMemo ( ( ) => {
1112 try {
12- new URL ( attributes . rest_uri ) ;
13- return true ;
13+ const url = new URL ( attributes . rest_uri ) ;
14+ return url . protocol === "http:" || url . protocol === "https:" ? true : setErrorMessage ( __ ( 'Invalid URL' , 'openkaarten-frontend-plugin' ) )
1415 } catch ( error ) {
16+ setErrorMessage ( __ ( 'Invalid URL' , 'openkaarten-frontend-plugin' ) )
1517 return false ;
1618 }
1719 } , [ attributes . rest_uri ] ) ;
1820
1921 useEffect ( ( ) => {
2022 if ( isValidURL ) {
23+ setErrorMessage ( null )
2124 const { username, password, url } = stripCredentialsFromUrl ( attributes . rest_uri ) ;
2225
2326 fetch ( '/wp-json/openkaarten-frontend-plugin/v1/proxy-datasets' , {
@@ -31,36 +34,49 @@ export default function Edit({ attributes, setAttributes }) {
3134 password,
3235 } ) ,
3336 } )
34- . then ( response => {
35- if ( ! response . ok ) {
36- throw new Error ( `Error status: ${ response . status } ` ) ;
37- }
38- return response . json ( ) ; // Initial parsing attempt.
39- } )
40- . then ( data => {
41-
42- // Check if data is a string and might need additional parsing.
43- if ( typeof data === "string" ) {
44- try {
45- data = JSON . parse ( data ) ; // Parse again if it's a JSON string.
46- } catch ( error ) {
47- console . error ( 'Error parsing nested JSON:' , error ) ;
48- setDatasets ( [ ] ) ;
49- return ;
37+ . then ( response => {
38+ if ( ! response . ok ) {
39+ // Try to read the JSON error information.
40+ return response . json ( )
41+ . then ( errData => {
42+ console . error ( 'Server error response:' , errData ) ;
43+ setErrorMessage ( __ ( `Fetching datasets failed: ${ errData . debug_info || 'Unknown error' } ` , 'openkaarten-frontend-plugin' ) ) ;
44+ throw new Error ( errData . message || `HTTP ${ response . status } ` ) ;
45+ } )
46+ . catch ( ( ) => {
47+ // Fallback if JSON is not available.
48+ throw new Error ( `Error status: ${ response . status } ` ) ;
49+ } ) ;
50+ }
51+ return response . json ( ) ;
52+ } )
53+ . then ( data => {
54+ // Check if data is a string and might need additional parsing.
55+ if ( typeof data === "string" ) {
56+ try {
57+ data = JSON . parse ( data ) ; // Parse again if it's a JSON string.
58+ } catch ( error ) {
59+ setErrorMessage ( __ ( `Error parsing nested JSON` , 'openkaarten-frontend-plugin' ) ) ;
60+ console . error ( 'Error parsing nested JSON:' , error ) ;
61+ setDatasets ( [ ] ) ;
62+ return ;
63+ }
5064 }
51- }
5265
53- if ( data && data . type === "DatasetCollection" && Array . isArray ( data . datasets ) ) {
54- setDatasets ( data . datasets ) ;
55- } else {
56- console . error ( "Unexpected response format or 'datasets' is not an array." ) ;
57- setDatasets ( [ ] ) ; // Fallback to empty array if structure is unexpected.
58- }
59- } )
60- . catch ( error => {
61- console . error ( 'Fetching datasets failed:' , error . message ) ;
62- setDatasets ( [ ] ) ;
63- } ) ;
66+ if ( data && data . type === "DatasetCollection" && Array . isArray ( data . datasets ) ) {
67+ setErrorMessage ( null ) ;
68+ setDatasets ( data . datasets ) ;
69+ } else {
70+ setErrorMessage ( __ ( "Unexpected response format or 'datasets' is not an array." , 'openkaarten-frontend-plugin' ) ) ;
71+ console . error ( "Unexpected response format or 'datasets' is not an array." ) ;
72+ setDatasets ( [ ] ) ; // Fallback to empty array if structure is unexpected.
73+ }
74+ } )
75+ . catch ( error => {
76+ setErrorMessage ( __ ( 'Fetching datasets failed' , 'openkaarten-frontend-plugin' ) ) ;
77+ console . error ( 'Fetching datasets failed:' , error . message ) ;
78+ setDatasets ( [ ] ) ;
79+ } ) ;
6480 }
6581 } , [ attributes . rest_uri , isValidURL ] ) ;
6682
@@ -76,7 +92,7 @@ export default function Edit({ attributes, setAttributes }) {
7692
7793 options . unshift ( {
7894 value : "" ,
79- label : __ ( 'Select the datalayers you want to display on the map' , 'openkaarten-frontend-plugin' ) ,
95+ label : datasets ?. length > 0 ? __ ( 'Select the datalayers you want to display on the map' , 'openkaarten-frontend-plugin' ) : __ ( 'No datalayers found ', 'openkaarten-frontend-plugin' ) ,
8096 disabled : true ,
8197 } ) ;
8298 return options ;
@@ -122,10 +138,10 @@ export default function Edit({ attributes, setAttributes }) {
122138 setIsTypingUrl ( false ) ;
123139 } }
124140 />
125- { ! ! attributes . rest_uri && ! isTypingUrl && ! isValidURL && (
126- < Notice status = "error" >
141+ { ! ! attributes . rest_uri && ! isTypingUrl && errorMessage && (
142+ < Notice status = "error" isDismissible = { false } >
127143 < p >
128- { __ ( 'Invalid URL' , 'openkaarten-frontend-plugin' ) }
144+ { errorMessage }
129145 </ p >
130146 </ Notice >
131147 ) }
0 commit comments