11import styles from '../styles/workingDocsTable.module.css' ;
22
3- const WorkingDocs = ( { handleChange, addNewDoc, docs, removeDoc, originalDocsCount, updateMyVariable } : any ) => {
3+ type Doc = {
4+ title ?: string ;
5+ link ?: string ;
6+ } ;
7+
8+ type WorkingDocsProps = {
9+ /** The complete array of docs, both old + newly added. */
10+ docs : Doc [ ] ;
11+ /**
12+ * Number of docs that existed in the DB before.
13+ * If `index < originalDocsCount`, we treat it as an “existing doc”.
14+ * Otherwise, it's a newly added doc.
15+ */
16+ originalDocsCount : number ;
17+
18+ /** Called when a NEW doc changes (indexes >= originalDocsCount). */
19+ handleChange : ( e : any , newDocIndex : number ) => void ;
20+
21+ /** Called when the user clicks “Add New Working Document”. */
22+ addNewDoc : ( ) => void ;
23+
24+ /** Called to remove a NEW doc from the parent's state. */
25+ removeDoc : ( newDocIndex : number ) => void ;
26+
27+ /**
28+ * Called to update an OLD doc (i.e. in DB).
29+ * If `e === null`, it means remove. Otherwise it’s a change event.
30+ */
31+ updateMyVariable : ( e : any | null , oldDocIndex : number ) => void ;
32+ } ;
33+
34+ const WorkingDocs = ( {
35+ docs,
36+ originalDocsCount,
37+ handleChange,
38+ addNewDoc,
39+ removeDoc,
40+ updateMyVariable
41+ } : WorkingDocsProps ) => {
42+
43+ /**
44+ * Called whenever an input changes in the table row.
45+ * If it's an existing doc (index < originalDocsCount),
46+ * we call `updateMyVariable`. Otherwise, we call `handleChange`.
47+ */
448 const handleDocChange = ( e : any , index : number ) => {
549 if ( index < originalDocsCount ) {
50+ // This is an existing doc from the DB
651 updateMyVariable ( e , index ) ;
752 } else {
53+ // This is a newly added doc
854 handleChange ( e , index - originalDocsCount ) ;
955 }
1056 } ;
1157
58+ /**
59+ * Called when the user clicks the “X” to remove a doc row.
60+ * If it’s an existing doc, call `updateMyVariable(null, index)`.
61+ * If it’s a new doc, call `removeDoc(...)`.
62+ */
1263 const handleDocRemove = ( index : number ) => {
1364 if ( index < originalDocsCount ) {
65+ // Remove an existing doc from DB or mark it removed
1466 updateMyVariable ( null , index ) ;
1567 } else {
68+ // Remove a new doc from local state
1669 removeDoc ( index - originalDocsCount ) ;
1770 }
1871 } ;
1972
73+ /**
74+ * Called when the user clicks “Add New Working Document”.
75+ * If we currently have 0 docs, we initialize the first doc as empty.
76+ * Otherwise, we call `addNewDoc()` to push another blank doc to local.
77+ */
2078 const handleAddNewDoc = ( ) => {
2179 if ( docs . length === 0 ) {
22- // Add the first document
80+ // Force-initialize the very first doc
2381 handleChange ( { target : { name : 'title' , value : '' } } , 0 ) ;
2482 handleChange ( { target : { name : 'link' , value : '' } } , 0 ) ;
2583 } else {
26- // Add a new document
2784 addNewDoc ( ) ;
2885 }
2986 } ;
3087
88+ /** Utility function to ensure links have a protocol. */
3189 const formatUrl = ( url : string ) => {
3290 if ( ! url ?. startsWith ( 'http://' ) && ! url ?. startsWith ( 'https://' ) ) {
3391 return `http://${ url } ` ;
@@ -47,8 +105,9 @@ const WorkingDocs = ({ handleChange, addNewDoc, docs, removeDoc, originalDocsCou
47105 </ tr >
48106 </ thead >
49107 < tbody >
50- { docs . map ( ( doc : any , index : number ) => (
108+ { docs . map ( ( doc : Doc , index : number ) => (
51109 < tr className = { styles . tr } key = { index } >
110+ { /* TITLE */ }
52111 < td className = { styles . td } >
53112 < input
54113 className = { styles . input }
@@ -59,8 +118,10 @@ const WorkingDocs = ({ handleChange, addNewDoc, docs, removeDoc, originalDocsCou
59118 onChange = { ( e ) => handleDocChange ( e , index ) }
60119 />
61120 </ td >
121+
122+ { /* LINK */ }
62123 < td className = { `${ styles . td } ${ styles . centerAligned } ${ styles . linkCell } ` } >
63- { /* Change here: Always show input fields if there 's only one row */ }
124+ { /* If there's only 1 row total, or if it 's a newly added doc, show the text input. */ }
64125 { ( docs . length === 1 || index >= originalDocsCount ) ? (
65126 < input
66127 className = { styles . input }
@@ -71,17 +132,31 @@ const WorkingDocs = ({ handleChange, addNewDoc, docs, removeDoc, originalDocsCou
71132 onChange = { ( e ) => handleDocChange ( e , index ) }
72133 />
73134 ) : (
74- < a href = { formatUrl ( doc . link ) } target = "_blank" rel = "noopener noreferrer" > Link</ a >
135+ // Otherwise, for older docs, show a clickable link
136+ < a href = { formatUrl ( doc . link || '' ) } target = "_blank" rel = "noopener noreferrer" >
137+ Link
138+ </ a >
75139 ) }
76140 </ td >
141+
142+ { /* REMOVE BUTTON */ }
77143 < td className = { `${ styles . td } ${ styles . centerAligned } ${ styles . removeButtonColumn } ` } >
78- < button className = { styles . removeButton } type = "button" onClick = { ( ) => handleDocRemove ( index ) } > X</ button >
144+ < button
145+ className = { styles . removeButton }
146+ type = "button"
147+ onClick = { ( ) => handleDocRemove ( index ) }
148+ >
149+ X
150+ </ button >
79151 </ td >
80152 </ tr >
81153 ) ) }
82154 </ tbody >
83155 </ table >
84- < button className = { styles . button } type = "button" onClick = { handleAddNewDoc } > Add New Working Document</ button >
156+
157+ < button className = { styles . button } type = "button" onClick = { handleAddNewDoc } >
158+ Add New Working Document
159+ </ button >
85160 </ >
86161 ) ;
87162} ;
0 commit comments