1
- import React , { useState } from 'react' ;
1
+ import React from 'react' ;
2
2
import { Input , List , Button , Form } from 'antd' ;
3
3
import { PlusOutlined , CloseCircleTwoTone } from '@ant-design/icons' ;
4
4
import ConfigResourceContainer from './ConfigResource'
@@ -7,83 +7,76 @@ import { IDictionary, IResource, IConfig } from '../interfaces'
7
7
interface IProps {
8
8
dictionary : IDictionary ;
9
9
config : IConfig ;
10
+ setConfig : any ;
10
11
}
11
12
12
- function ConfigContainer ( { dictionary, config } :IProps ) {
13
- const [ configState , setConfig ] = useState < IConfig > ( config ) ;
14
-
13
+ function ConfigContainer ( { dictionary, config, setConfig } :IProps ) {
15
14
const handleFeaturePathsChange = ( index ) => {
16
15
return ( e ) => {
17
- let featurePaths = configState . features_path ;
16
+ let featurePaths = config . features_path ;
18
17
featurePaths [ index ] = e . target . value ;
19
18
20
19
setConfig ( {
21
- features_path : configState . features_path . map ( ( val , i ) => {
20
+ features_path : config . features_path . map ( ( val , i ) => {
22
21
if ( i === index ) return e . target . value ;
23
22
return val ;
24
23
} ) ,
25
- resources : configState . resources
24
+ resources : config . resources
26
25
} )
27
26
}
28
27
}
29
28
const handleFeaturePathsDelete = ( index ) => {
30
29
return ( ) => {
31
30
setConfig ( {
32
- features_path : configState . features_path . filter ( ( item , i ) => index !== i ) ,
33
- resources : configState . resources
31
+ features_path : config . features_path . filter ( ( item , i ) => index !== i ) ,
32
+ resources : config . resources
34
33
} )
35
34
}
36
35
}
37
36
const handleFeaturePathsAdd = ( ) => {
38
37
setConfig ( {
39
- features_path : [ ...configState . features_path , `somewhere/features-${ configState . features_path . length } ` ] ,
40
- resources : configState . resources
38
+ features_path : [ ...config . features_path , `somewhere/features-${ config . features_path . length } ` ] ,
39
+ resources : config . resources
41
40
} )
42
41
}
43
42
44
43
const handleResourceItemChange = ( selectedName : string , newItem : IResource | null ) => {
45
44
if ( newItem === null ) return handleResourceItemRemove ( selectedName ) ;
46
- const newResourceItem = configState . resources . map ( ( item : IResource ) => {
45
+ const newResourceItem = config . resources . map ( ( item : IResource ) => {
47
46
if ( item . name === selectedName ) {
48
47
return newItem ;
49
48
}
50
49
return item ;
51
50
} ) ;
52
51
53
52
setConfig ( {
54
- features_path : configState . features_path ,
53
+ features_path : config . features_path ,
55
54
resources : newResourceItem
56
55
} ) ;
57
56
}
58
57
59
58
const handleResourceItemRemove = ( selectedName : string ) => {
60
- const newResourceItems = configState . resources . filter ( ( item : IResource ) => {
59
+ const newResourceItems = config . resources . filter ( ( item : IResource ) => {
61
60
return ( item . name !== selectedName )
62
61
} ) ;
63
62
64
63
setConfig ( {
65
- features_path : configState . features_path ,
64
+ features_path : config . features_path ,
66
65
resources : newResourceItems
67
66
} ) ;
68
67
}
69
68
70
69
const handleAddResourceItem = ( ) => {
71
70
const newResourceItem = {
72
- name : `new-${ configState . resources . length } ` ,
73
- type : " wiremock" ,
71
+ name : `new-${ config . resources . length } ` ,
72
+ type : ' wiremock' ,
74
73
parameters : { }
75
74
} ;
76
-
77
75
setConfig ( {
78
- features_path : configState . features_path ,
79
- resources : [ ...configState . resources , newResourceItem ]
76
+ features_path : config . features_path ,
77
+ resources : [ ...config . resources , newResourceItem ]
80
78
} ) ;
81
79
}
82
-
83
-
84
- const handleSave = ( ) => {
85
-
86
- }
87
80
88
81
return (
89
82
< div className = "App" >
@@ -92,10 +85,11 @@ function ConfigContainer({ dictionary, config }:IProps) {
92
85
< strong > Features Path</ strong >
93
86
< List
94
87
itemLayout = "horizontal"
95
- dataSource = { configState . features_path }
88
+ dataSource = { config . features_path }
96
89
renderItem = { ( item , index ) => (
97
90
< List . Item >
98
91
< Input
92
+ key = { index }
99
93
style = { { width : '300px' } }
100
94
onChange = { handleFeaturePathsChange ( index ) }
101
95
placeholder = "Features path"
@@ -114,10 +108,13 @@ function ConfigContainer({ dictionary, config }:IProps) {
114
108
</ Button >
115
109
</ div >
116
110
< table style = { { width : '100%' } } >
117
- < tr >
118
- < th > Resources</ th >
119
- </ tr >
120
- { configState . resources . map ( ( item , index ) => {
111
+ < thead >
112
+ < tr >
113
+ < th > Resources</ th >
114
+ </ tr >
115
+ </ thead >
116
+ < tbody >
117
+ { config . resources . map ( ( item , index ) => {
121
118
return (
122
119
< ConfigResourceContainer
123
120
key = { index }
@@ -127,27 +124,25 @@ function ConfigContainer({ dictionary, config }:IProps) {
127
124
/>
128
125
) ;
129
126
} ) }
130
- < tr >
131
- < td colSpan = { 3 } >
132
- < Form . Item >
133
- < Button
134
- style = { { height : '60px' } }
135
- type = "dashed"
136
- onClick = { handleAddResourceItem }
137
- block
138
- >
139
- < PlusOutlined /> Add new resource
140
- </ Button >
141
- </ Form . Item >
142
- </ td >
143
- </ tr >
127
+ </ tbody >
128
+ < tfoot >
129
+ < tr >
130
+ < td colSpan = { 3 } >
131
+ < Form . Item >
132
+ < Button
133
+ style = { { height : '60px' } }
134
+ type = "dashed"
135
+ onClick = { handleAddResourceItem }
136
+ block
137
+ >
138
+ < PlusOutlined /> Add new resource
139
+ </ Button >
140
+ </ Form . Item >
141
+ </ td >
142
+ </ tr >
143
+ </ tfoot >
144
144
</ table >
145
-
146
- < Button onClick = { handleSave } type = "primary" >
147
- Save
148
- </ Button >
149
-
150
- < div > { JSON . stringify ( configState ) } </ div >
145
+ < div > { JSON . stringify ( config ) } </ div >
151
146
</ div >
152
147
) ;
153
148
}
0 commit comments