11import { fr } from "@codegouvfr/react-dsfr" ;
2+ import Accordion from "@codegouvfr/react-dsfr/Accordion" ;
23import Alert from "@codegouvfr/react-dsfr/Alert" ;
34import Button from "@codegouvfr/react-dsfr/Button" ;
45import { Upload } from "@codegouvfr/react-dsfr/Upload" ;
@@ -12,8 +13,8 @@ import { StyleFormatEnum } from "@/@types/app";
1213import { useStyleForm } from "@/contexts/StyleFormContext" ;
1314import { useTranslation } from "@/i18n" ;
1415import TMSStyleTools from "@/modules/Style/TMSStyleFilesManager/TMSStyleTools" ;
15- import { encodeKey , getFileExtension } from "@/utils" ;
16- import { getParserForExtension , mbParser , sldParser } from "@/utils/geostyler" ;
16+ import { encodeKey , getFileExtension , readFileContent } from "@/utils" ;
17+ import { getDefaultStyle , getParserForExtension , mbParser , propertyNamesToLowerCase , sldParser } from "@/utils/geostyler" ;
1718import ConfirmDialog , { ConfirmDialogModal } from "../ConfirmDialog" ;
1819import LoadingText from "../LoadingText" ;
1920import useStylesHandler from "./useStylesHandler" ;
@@ -29,32 +30,6 @@ type UploadStyleFileProps = {
2930 acceptedFileExtensions ?: string [ ] ;
3031} ;
3132
32- function propertyNamesToLowerCase ( sldContent : string ) {
33- return sldContent . replace ( / < ( o g c : ) ? P r o p e r t y N a m e > ( .* ?) < \/ ( o g c : ) ? P r o p e r t y N a m e > / g, ( _ , p1 , p2 ) => {
34- return `<${ p1 ?? "" } PropertyName>${ p2 . toLowerCase ( ) } </${ p1 ?? "" } PropertyName>` ;
35- } ) ;
36- }
37-
38- function getDefaultStyle ( currentTable : string ) : GsStyle {
39- return {
40- name : currentTable ,
41- rules : [
42- {
43- name : `rule_${ Math . floor ( Math . random ( ) * 10000 )
44- . toString ( )
45- . padStart ( 4 , "0" ) } `,
46- symbolizers : [
47- {
48- kind : "Mark" ,
49- wellKnownName : "circle" ,
50- color : "#0E1058" ,
51- } ,
52- ] ,
53- } ,
54- ] ,
55- } ;
56- }
57-
5833const tmsStyleTools = new TMSStyleTools ( ) ;
5934
6035const UploadStyleFile : FC < UploadStyleFileProps > = ( props ) => {
@@ -63,9 +38,21 @@ const UploadStyleFile: FC<UploadStyleFileProps> = (props) => {
6338 const { t } = useTranslation ( "UploadStyleFile" ) ;
6439
6540 const { currentTable, styleFormats, service, isMapbox } = useStyleForm ( ) ;
66- const { setError, trigger } = useFormContext ( ) ;
67-
68- const { gsStyle, setGsStyle, strStyle } = useStylesHandler ( {
41+ const {
42+ formState : { errors } ,
43+ setError,
44+ trigger,
45+ } = useFormContext ( ) ;
46+
47+ const {
48+ gsStyle,
49+ setGsStyle,
50+ strStyle,
51+ readStyleResult,
52+ setReadStyleResult,
53+ writeStyleResult,
54+ // setWriteStyleResult
55+ } = useStylesHandler ( {
6956 parser,
7057 format : styleFormats [ currentTable ] ?? StyleFormatEnum . SLD ,
7158 initialStrStyle : value ,
@@ -97,22 +84,6 @@ const UploadStyleFile: FC<UploadStyleFileProps> = (props) => {
9784 }
9885 } , [ strStyle , encodedFieldName ] ) ;
9986
100- const readFileContent = async ( file : File ) : Promise < string > => {
101- return new Promise ( ( resolve , reject ) => {
102- const reader = new FileReader ( ) ;
103- reader . onerror = ( ) => reject ( new Error ( "Impossible de lire le fichier sélectionné" ) ) ;
104- reader . onabort = ( ) => reject ( new Error ( "Lecture du fichier annulée" ) ) ;
105- reader . onload = ( e ) => {
106- if ( typeof e . target ?. result === "string" ) {
107- resolve ( e . target . result ) ;
108- } else {
109- reject ( new Error ( "Format de fichier non supporté" ) ) ;
110- }
111- } ;
112- reader . readAsText ( file ) ;
113- } ) ;
114- } ;
115-
11687 async function handleUpload ( event : React . ChangeEvent < HTMLInputElement > ) {
11788 setUploadError ( undefined ) ;
11889
@@ -134,6 +105,7 @@ const UploadStyleFile: FC<UploadStyleFileProps> = (props) => {
134105 onFormatChange ?.( targetFormat ) ;
135106
136107 const gsStyleResult : ReadStyleResult | undefined = await parser . readStyle ( extension === "json" ? JSON . parse ( content ) : content ) ;
108+ setReadStyleResult ( gsStyleResult ) ;
137109
138110 // debug
139111 if ( gsStyleResult . unsupportedProperties ) console . warn ( "unsupported" , gsStyleResult . unsupportedProperties ) ;
@@ -148,6 +120,7 @@ const UploadStyleFile: FC<UploadStyleFileProps> = (props) => {
148120 try {
149121 const currentStr = strStyle ;
150122 const writeRes = await parser . writeStyle ( gsStyle ) ;
123+ // setWriteStyleResult(writeRes);
151124 const newStr = typeof writeRes . output === "object" ? JSON . stringify ( writeRes . output ) : ( writeRes . output as string ) ;
152125
153126 // mettre à jour uniquement si le style a changé
@@ -167,16 +140,20 @@ const UploadStyleFile: FC<UploadStyleFileProps> = (props) => {
167140 let defaultStyle = getDefaultStyle ( currentTable ) ;
168141
169142 if ( service && isMapbox ) {
170- const output = ( await mbParser . writeStyle ( defaultStyle ) ) . output ;
143+ const writeResult = await mbParser . writeStyle ( defaultStyle ) ;
144+ // setWriteStyleResult(writeResult);
145+ const output = writeResult . output ;
171146 if ( output ) {
172147 const finalMbStyle = tmsStyleTools . buildMbStyle ( service , output ) ;
173- defaultStyle = ( await mbParser . readStyle ( finalMbStyle ) ) . output as GsStyle ;
148+ const readResult = await mbParser . readStyle ( finalMbStyle ) ;
149+ defaultStyle = readResult . output as GsStyle ;
150+ setReadStyleResult ( readResult ) ;
174151 }
175152 }
176153
177154 setGsStyle ( defaultStyle ) ;
178155 setUploadError ( undefined ) ;
179- } , [ currentTable , setGsStyle , isMapbox , service ] ) ;
156+ } , [ currentTable , setGsStyle , setReadStyleResult , isMapbox , service ] ) ;
180157
181158 const handleRemoveStyle = ( ) => {
182159 setGsStyle ( undefined ) ;
@@ -192,6 +169,74 @@ const UploadStyleFile: FC<UploadStyleFileProps> = (props) => {
192169 { uploadError && (
193170 < Alert severity = "error" title = { "Erreur de chargement de style" } description = { uploadError } onClose = { ( ) => setUploadError ( undefined ) } />
194171 ) }
172+ { errors ?. [ "style_files" ] ?. [ encodeKey ( currentTable ) ] ?. message && (
173+ < Alert severity = "error" small description = { errors ?. [ "style_files" ] ?. [ encodeKey ( currentTable ) ] ?. message } />
174+ ) }
175+
176+ < Accordion label = "Read Style Result" >
177+ { readStyleResult ?. errors && (
178+ < >
179+ Erreurs :
180+ < pre >
181+ < code style = { { whiteSpace : "pre" } } className = { fr . cx ( "fr-text--sm" ) } >
182+ { JSON . stringify ( readStyleResult ?. errors , null , 4 ) }
183+ </ code >
184+ </ pre >
185+ </ >
186+ ) }
187+ { readStyleResult ?. warnings && (
188+ < >
189+ Avertissements :
190+ < pre >
191+ < code style = { { whiteSpace : "pre" } } className = { fr . cx ( "fr-text--sm" ) } >
192+ { JSON . stringify ( readStyleResult ?. warnings , null , 4 ) }
193+ </ code >
194+ </ pre >
195+ </ >
196+ ) }
197+ { readStyleResult ?. unsupportedProperties && (
198+ < >
199+ Propriétés non supportées :
200+ < pre >
201+ < code style = { { whiteSpace : "pre" } } className = { fr . cx ( "fr-text--sm" ) } >
202+ { JSON . stringify ( readStyleResult ?. unsupportedProperties , null , 4 ) }
203+ </ code >
204+ </ pre >
205+ </ >
206+ ) }
207+ </ Accordion >
208+ < Accordion label = "Write Style Result" >
209+ { writeStyleResult ?. errors && (
210+ < >
211+ Erreurs :
212+ < pre >
213+ < code style = { { whiteSpace : "pre" } } className = { fr . cx ( "fr-text--sm" ) } >
214+ { JSON . stringify ( writeStyleResult ?. errors , null , 4 ) }
215+ </ code >
216+ </ pre >
217+ </ >
218+ ) }
219+ { writeStyleResult ?. warnings && (
220+ < >
221+ Avertissements :
222+ < pre >
223+ < code style = { { whiteSpace : "pre" } } className = { fr . cx ( "fr-text--sm" ) } >
224+ { JSON . stringify ( writeStyleResult ?. warnings , null , 4 ) }
225+ </ code >
226+ </ pre >
227+ </ >
228+ ) }
229+ { writeStyleResult ?. unsupportedProperties && (
230+ < >
231+ Propriétés non supportées :
232+ < pre >
233+ < code style = { { whiteSpace : "pre" } } className = { fr . cx ( "fr-text--sm" ) } >
234+ { JSON . stringify ( writeStyleResult ?. unsupportedProperties , null , 4 ) }
235+ </ code >
236+ </ pre >
237+ </ >
238+ ) }
239+ </ Accordion >
195240 { gsStyle ? (
196241 < div >
197242 < div className = { fr . cx ( "fr-grid-row" , "fr-grid-row--right" ) } >
0 commit comments