11import React from 'react' ;
2+ import useEnvStore from 'stores/EnvStore' ;
3+ import { EditText } from 'react-edit-text' ;
4+ import 'react-edit-text/dist/index.css' ;
5+ import { toast } from 'react-toastify' ;
6+ import { TrashIcon } from '@heroicons/react/24/outline' ;
7+
8+ const Env = ( ) => {
9+ const variables = useEnvStore ( ( state ) => state . variables ) ;
10+ const handleAddRow = useEnvStore ( ( state ) => state . handleAddRow ) ;
11+ const handleKeyChange = useEnvStore ( ( state ) => state . handleKeyChange ) ;
12+ const handleValueChange = useEnvStore ( ( state ) => state . handleValueChange ) ;
13+
14+ const handleSave = ( { name, value, previousValue } ) => {
15+ console . log ( 'handle save' ) ;
16+ if ( name === 'editVariableKey' ) {
17+ const existingVar = Object . keys ( variables ) . find ( ( key ) => key === value ) ;
18+ if ( existingVar ) {
19+ toast . error ( 'A variable with the same name already exists' ) ;
20+ } else {
21+ handleKeyChange ( value , previousValue ) ;
22+ }
23+ } else {
24+ handleValueChange ( name , value ) ;
25+ }
26+ } ;
227
3- const Env = ( { variables } ) => {
428 return (
529 < div className = 'p-4' >
6- < div > Test Tab panel for environment</ div >
30+ { /* <div>Test Tab panel for environment</div> */ }
731 < div className = 'overflow-x-auto' >
832 < table className = 'table table-zebra' >
933 { /* head */ }
@@ -12,63 +36,34 @@ const Env = ({ variables }) => {
1236 < th > </ th >
1337 < th > Key</ th >
1438 < th > Value</ th >
15- < th > New Value</ th >
39+ { /* <th>New Value</th> */ }
1640 </ tr >
1741 </ thead >
1842 < tbody >
19- < tr >
20- < th > 1</ th >
21- < td > Test variable 1</ td >
22- < td > ABC</ td >
23- < td >
24- < input
25- type = 'text'
26- className = 'block w-full rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 outline-blue-300 focus:border-blue-100 focus:ring-blue-100'
27- placeholder = 'label'
28- required
29- onChange = { ( event ) => {
30- const labelValue = event . target . value ;
31- console . log ( `New value for ABC: ${ labelValue } ` ) ;
32- } }
33- />
34- </ td >
35- </ tr >
36- < tr >
37- < th > 2</ th >
38- < td > Test variable 2</ td >
39- < td > DEF</ td >
40- < td >
41- < input
42- type = 'text'
43- className = 'block w-full rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 outline-blue-300 focus:border-blue-100 focus:ring-blue-100'
44- placeholder = 'label'
45- required
46- onChange = { ( event ) => {
47- const labelValue = event . target . value ;
48- console . log ( `New value for DEF: ${ labelValue } ` ) ;
49- } }
50- />
51- </ td >
52- </ tr >
53- < tr >
54- < th > 3</ th >
55- < td > Test variable 3</ td >
56- < td > ZXE</ td >
57- < td >
58- < input
59- type = 'text'
60- className = 'block w-full rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 outline-blue-300 focus:border-blue-100 focus:ring-blue-100'
61- placeholder = 'label'
62- required
63- onChange = { ( event ) => {
64- const labelValue = event . target . value ;
65- console . log ( `New value for ZXE: ${ labelValue } ` ) ;
66- } }
67- />
68- </ td >
69- </ tr >
43+ { Object . entries ( variables ) . map ( ( [ key , value ] , index ) => (
44+ < tr key = { index } >
45+ < th > { index } </ th >
46+ < td >
47+ < EditText name = 'editVariableKey' className = 'text-xl' value = { key } onSave = { handleSave } />
48+ </ td >
49+ < td >
50+ < EditText name = { key } className = 'text-xl' value = { value } onSave = { handleSave } />
51+ </ td >
52+ < td >
53+ < div
54+ className = 'relative inline-block p-2 text-left transition duration-200 ease-out rounded rounded-l-none hover:bg-slate-200'
55+ onClick = { ( ) => {
56+ console . log ( `Delete ${ key } ` ) ;
57+ } }
58+ >
59+ < TrashIcon className = 'w-4 h-4' aria-hidden = 'true' />
60+ </ div >
61+ </ td >
62+ </ tr >
63+ ) ) }
7064 </ tbody >
7165 </ table >
66+ < button onClick = { handleAddRow } > + Add Variable</ button >
7267 </ div >
7368 </ div >
7469 ) ;
0 commit comments