@@ -15,6 +15,7 @@ import React, {
1515 useCallback ,
1616 useContext ,
1717 useImperativeHandle ,
18+ useState ,
1819} from "react" ;
1920import ReactDOM from "react-dom" ;
2021import ediTDorContext from "../../context/ediTDorContext" ;
@@ -41,6 +42,14 @@ interface AddLinkTdDialogProps {
4142const AddLinkTdDialog = forwardRef < AddLinkTdDialogRef , AddLinkTdDialogProps > (
4243 ( props , ref ) => {
4344 const context = useContext ( ediTDorContext ) ;
45+
46+ const [ formData , setFormData ] = useState < Link > ( ( ) => ( {
47+ href : "" ,
48+ rel : "" ,
49+ type : "" ,
50+ } ) ) ;
51+ const [ errorMessage , setErrorMessage ] = useState < string > ( "" ) ;
52+
4453 const [ display , setDisplay ] = React . useState < boolean > ( ( ) => {
4554 return false ;
4655 } ) ;
@@ -80,9 +89,7 @@ const AddLinkTdDialog = forwardRef<AddLinkTdDialogRef, AddLinkTdDialogProps>(
8089 } ;
8190
8291 const addLinksToTd = ( link : Link ) : void => {
83- // clone instead of mutating original TD
8492 const updatedTd = structuredClone ( context . parsedTD ) ;
85- // initialize links array if missing
8693 if ( ! Array . isArray ( updatedTd . links ) ) {
8794 updatedTd . links = [ ] ;
8895 }
@@ -101,8 +108,13 @@ const AddLinkTdDialog = forwardRef<AddLinkTdDialogRef, AddLinkTdDialogProps>(
101108 try {
102109 const res = await fileTdService . readFromFile ( ) ;
103110
104- ( document . getElementById ( "link-href" ) as HTMLInputElement ) . value =
105- `./${ res . fileName } ` ;
111+ setFormData ( ( prev ) => ( {
112+ ...prev ,
113+ href : `./${ res . fileName } ` ,
114+ } ) ) ;
115+
116+ setErrorMessage ( "" ) ;
117+
106118 setCurrentLinkedTd (
107119 res . fileHandle ? res . fileHandle : JSON . parse ( res . td )
108120 ) ;
@@ -155,6 +167,13 @@ const AddLinkTdDialog = forwardRef<AddLinkTdDialogRef, AddLinkTdDialogProps>(
155167 id = "rel"
156168 className = "w-full rounded-md border-2 border-gray-600 bg-gray-600 p-2 text-white focus:border-blue-500 focus:outline-none sm:text-sm"
157169 placeholder = "relation name"
170+ value = { formData . rel }
171+ onChange = { ( e ) =>
172+ setFormData ( {
173+ ...formData ,
174+ rel : e . target . value ,
175+ } )
176+ }
158177 />
159178 < datalist id = "relationType" >
160179 < RelationType > </ RelationType >
@@ -167,7 +186,7 @@ const AddLinkTdDialog = forwardRef<AddLinkTdDialogRef, AddLinkTdDialogProps>(
167186 htmlFor = "link-href"
168187 className = "pl-2 pr-2 text-sm font-medium text-gray-400"
169188 >
170- Target ressource :
189+ Target resource :
171190 </ label >
172191
173192 < BaseButton
@@ -194,12 +213,21 @@ const AddLinkTdDialog = forwardRef<AddLinkTdDialogRef, AddLinkTdDialogProps>(
194213 type = "text"
195214 name = "link-href"
196215 id = "link-href"
197- className = "rounded-md border-2 border-gray-600 bg-gray-600 p-2 text-white focus:border-blue-500 focus:outline-none sm:text-sm"
198- placeholder = "The target ressource"
199- onChange = { ( ) => {
200- clearHrefErrorMessage ( ) ;
216+ className = { `rounded-md border-2 bg-gray-600 p-2 text-white focus:outline-none sm:text-sm ${
217+ errorMessage
218+ ? "border-red-400"
219+ : "border-gray-600 focus:border-blue-500"
220+ } `}
221+ placeholder = "The target resource"
222+ onChange = { ( e ) => {
223+ setFormData ( {
224+ ...formData ,
225+ href : e . target . value ,
226+ } ) ;
227+ setErrorMessage ( "" ) ;
201228 } }
202- disabled = { linkingMethod !== "url" }
229+ readOnly = { linkingMethod !== "url" }
230+ value = { formData . href }
203231 />
204232 { linkingMethod === "upload" && (
205233 < BaseButton
@@ -213,10 +241,11 @@ const AddLinkTdDialog = forwardRef<AddLinkTdDialogRef, AddLinkTdDialogProps>(
213241 </ BaseButton >
214242 ) }
215243 </ div >
216- < span
217- id = "link-href-info"
218- className = "pl-2 text-xs text-red-400"
219- > </ span >
244+ { errorMessage && (
245+ < span id = "link-href-info" className = "pl-2 text-xs text-red-400" >
246+ { errorMessage }
247+ </ span >
248+ ) }
220249 < div >
221250 < label
222251 htmlFor = "type"
@@ -231,6 +260,13 @@ const AddLinkTdDialog = forwardRef<AddLinkTdDialogRef, AddLinkTdDialogProps>(
231260 id = "type"
232261 className = "w-full rounded-md border-2 border-gray-600 bg-gray-600 p-2 text-white focus:border-blue-500 focus:outline-none sm:text-sm"
233262 placeholder = "media type"
263+ value = { formData . type }
264+ onChange = { ( e ) =>
265+ setFormData ( {
266+ ...formData ,
267+ type : e . target . value ,
268+ } )
269+ }
234270 />
235271 < datalist id = "mediaType" >
236272 < option value = "application/td+json" />
@@ -245,37 +281,30 @@ const AddLinkTdDialog = forwardRef<AddLinkTdDialogRef, AddLinkTdDialogProps>(
245281
246282 const handleAddLink = ( ) => {
247283 if ( ! context . isValidJSON ) {
248- showHrefErrorMessage ( "Can't add link. TD is malformed" ) ;
284+ setErrorMessage ( "Can't add link. TD is malformed" ) ;
249285 return ;
250286 }
251287
252288 const linkedTd : Record < string , any > = { } ;
253289
254290 const link : Link = {
255- href :
256- (
257- document . getElementById ( "link-href" ) as HTMLInputElement
258- ) . value . trim ( ) || "/" ,
291+ href : formData . href . trim ( ) ,
259292 } ;
260- const rel = (
261- document . getElementById ( "rel" ) as HTMLInputElement
262- ) . value . trim ( ) ;
263- const type = (
264- document . getElementById ( "type" ) as HTMLInputElement
265- ) . value . trim ( ) ;
266293
267- if ( rel ) link . rel = rel ;
268- if ( type ) link . type = type ;
294+ if ( formData . rel ) link . rel = formData . rel . trim ( ) ;
295+ if ( formData . type ) link . type = formData . type . trim ( ) ;
269296
270297 let isValidUrl = true ;
298+ let url : URL | null = null ;
271299 try {
272- var url = new URL ( link . href ) ;
300+ url = new URL ( link . href ) ;
273301 } catch ( _ ) {
274302 isValidUrl = false ;
275303 }
276304 if (
277305 linkingMethod === "url" &&
278306 isValidUrl &&
307+ url &&
279308 ( url . protocol === "http:" || url . protocol === "https:" )
280309 ) {
281310 try {
@@ -285,7 +314,7 @@ const AddLinkTdDialog = forwardRef<AddLinkTdDialogRef, AddLinkTdDialogProps>(
285314 if (
286315 httpRequest
287316 . getResponseHeader ( "content-type" )
288- . includes ( "application/td+json" )
317+ ? .includes ( "application/td+json" )
289318 ) {
290319 const thingDescription = httpRequest . response ;
291320 let parsedTd = JSON . parse ( thingDescription ) ;
@@ -301,9 +330,9 @@ const AddLinkTdDialog = forwardRef<AddLinkTdDialogRef, AddLinkTdDialogProps>(
301330 }
302331
303332 if ( link . href === "" ) {
304- showHrefErrorMessage ( "The href field is mandatory ..." ) ;
333+ setErrorMessage ( "The href field is mandatory ..." ) ;
305334 } else if ( checkIfLinkExists ( link ) ) {
306- showHrefErrorMessage (
335+ setErrorMessage (
307336 "A Link with the target Thing Description already exists ..."
308337 ) ;
309338 } else {
@@ -322,7 +351,7 @@ const AddLinkTdDialog = forwardRef<AddLinkTdDialogRef, AddLinkTdDialogProps>(
322351 rightButton = { "Add" }
323352 children = { children }
324353 title = { `Add Link ` }
325- description = { `Tell us how this ${ tdJSON . title } can interact with other ressources ` }
354+ description = { `Tell us how this ${ tdJSON . title } can interact with other resources ` }
326355 /> ,
327356 document . getElementById ( "modal-root" ) as HTMLElement
328357 ) ;
@@ -332,24 +361,5 @@ const AddLinkTdDialog = forwardRef<AddLinkTdDialogRef, AddLinkTdDialogProps>(
332361 }
333362) ;
334363
335- const showHrefErrorMessage = ( msg ) => {
336- ( document . getElementById ( "link-href-info" ) as HTMLElement ) . textContent = msg ;
337- ( document . getElementById ( "link-href" ) as HTMLElement ) . classList . remove (
338- "border-gray-600"
339- ) ;
340- ( document . getElementById ( "link-href" ) as HTMLElement ) . classList . add (
341- "border-red-400"
342- ) ;
343- } ;
344-
345- const clearHrefErrorMessage = ( ) => {
346- ( document . getElementById ( "link-href" ) as HTMLElement ) . classList . add (
347- "border-gray-600"
348- ) ;
349- ( document . getElementById ( "link-href" ) as HTMLElement ) . classList . remove (
350- "border-red-400"
351- ) ;
352- } ;
353-
354364AddLinkTdDialog . displayName = "AddLinkTdDialog" ;
355365export default AddLinkTdDialog ;
0 commit comments