1
- import { reduxActions } from '@codetanzania/ewea-api-states' ;
2
- import { Button , Input , Form } from 'antd' ;
3
- import PropTypes from 'prop-types' ;
4
1
import React from 'react' ;
2
+ import PropTypes from 'prop-types' ;
3
+ import get from 'lodash/get' ;
4
+ import { Button , Input , InputNumber , Form , Row , Col } from 'antd' ;
5
+ import { httpActions } from '@codetanzania/ewea-api-client' ;
6
+ import { reduxActions } from '@codetanzania/ewea-api-states' ;
5
7
import { notifyError , notifySuccess } from '../../../util' ;
8
+ import SearchableSelectInput from '../../../components/SearchableSelectInput' ;
9
+
10
+ /* http actions */
11
+ const { getAdministrativeLevels } = httpActions ;
6
12
7
- /* constants */
13
+ /* state actions */
8
14
const { putAdministrativeLevel, postAdministrativeLevel } = reduxActions ;
15
+
16
+ /* ui */
9
17
const { TextArea } = Input ;
10
- const formItemLayout = {
11
- labelCol : {
12
- xs : { span : 24 } ,
13
- sm : { span : 24 } ,
14
- md : { span : 24 } ,
15
- lg : { span : 24 } ,
16
- xl : { span : 24 } ,
17
- xxl : { span : 24 } ,
18
- } ,
19
- wrapperCol : {
20
- xs : { span : 24 } ,
21
- sm : { span : 24 } ,
22
- md : { span : 24 } ,
23
- lg : { span : 24 } ,
24
- xl : { span : 24 } ,
25
- xxl : { span : 24 } ,
26
- } ,
18
+ const labelCol = {
19
+ xs : { span : 24 } ,
20
+ sm : { span : 24 } ,
21
+ md : { span : 24 } ,
22
+ lg : { span : 24 } ,
23
+ xl : { span : 24 } ,
24
+ xxl : { span : 24 } ,
27
25
} ;
26
+ const wrapperCol = {
27
+ xs : { span : 24 } ,
28
+ sm : { span : 24 } ,
29
+ md : { span : 24 } ,
30
+ lg : { span : 24 } ,
31
+ xl : { span : 24 } ,
32
+ xxl : { span : 24 } ,
33
+ } ;
34
+
35
+ /* messages */
36
+ const MESSAGE_POST_SUCCESS = 'Administrative Level was created successfully' ;
37
+ const MESSAGE_POST_ERROR =
38
+ 'Something occurred while saving Administrative Level, Please try again!' ;
39
+ const MESSAGE_PUT_SUCCESS = 'Administrative Level was updated successfully' ;
40
+ const MESSAGE_PUT_ERROR =
41
+ 'Something occurred while updating Administrative Level, Please try again!' ;
28
42
29
43
/**
30
- * @function
31
- * @param {object } props props object
32
- * @param {* } props.administrativeLevel valid administrative level
33
- * @param {boolean } props.isEditForm edit flag
34
- * @param {boolean } props.posting posting flag
35
- * @param {Function } props.onCancel cancel callback
44
+ * @function AdministrativeLevelForm
36
45
* @name AdministrativeLevelForm
37
- * @description Administrative Level Form
38
- * @returns {object } React Component
39
- * @version 0.1.0
46
+ * @description Form for create and edit administrative level
47
+ * @param {object } props Valid form properties
48
+ * @param {object } props.administrativeLevel Valid administrative level object
49
+ * @param {boolean } props.isEditForm Flag wether form is on edit mode
50
+ * @param {boolean } props.posting Flag whether form is posting data
51
+ * @param {Function } props.onCancel Form cancel callback
52
+ * @returns {object } AdministrativeLevelForm component
53
+ * @author lally elias <lallyelias87@gmail.com>
54
+ * @license MIT
40
55
* @since 0.1.0
56
+ * @version 0.1.0
57
+ * @static
58
+ * @public
59
+ * @example
60
+ *
61
+ * <AdministrativeLevelForm
62
+ * administrativeLevel={administrativeLevel}
63
+ * isEditForm={isEditForm}
64
+ * posting={posting}
65
+ * onCancel={this.handleCloseAdministrativeLevelForm}
66
+ * />
67
+ *
41
68
*/
42
69
const AdministrativeLevelForm = ( {
43
70
administrativeLevel,
44
71
isEditForm,
45
72
posting,
46
73
onCancel,
47
74
} ) => {
75
+ // form finish(submit) handler
48
76
const onFinish = ( values ) => {
49
77
if ( isEditForm ) {
50
- const updatedEventStatus = { ...administrativeLevel , ...values } ;
78
+ const updates = { ...administrativeLevel , ...values } ;
51
79
putAdministrativeLevel (
52
- updatedEventStatus ,
53
- ( ) => {
54
- notifySuccess ( 'Administrative level was updated successfully' ) ;
55
- } ,
56
- ( ) => {
57
- notifyError (
58
- 'Something occurred while updating administrative level, please try again!'
59
- ) ;
60
- }
80
+ updates ,
81
+ ( ) => notifySuccess ( MESSAGE_PUT_SUCCESS ) ,
82
+ ( ) => notifyError ( MESSAGE_PUT_ERROR )
61
83
) ;
62
84
} else {
63
85
postAdministrativeLevel (
64
86
values ,
65
- ( ) => {
66
- notifySuccess ( 'Administrative level was created successfully' ) ;
67
- } ,
68
- ( ) => {
69
- notifyError (
70
- 'Something occurred while saving administrative level, please try again!'
71
- ) ;
72
- }
87
+ ( ) => notifySuccess ( MESSAGE_POST_SUCCESS ) ,
88
+ ( ) => notifyError ( MESSAGE_POST_ERROR )
73
89
) ;
74
90
}
75
91
} ;
76
92
77
93
return (
78
94
< Form
95
+ labelCol = { labelCol }
96
+ wrapperCol = { wrapperCol }
79
97
onFinish = { onFinish }
80
- { ...formItemLayout } // eslint-disable-line
81
- initialValues = { {
82
- ...administrativeLevel ,
83
- } }
98
+ initialValues = { { ...administrativeLevel } }
84
99
autoComplete = "off"
85
100
>
86
- { /* administrative level name */ }
101
+ { /* start: name */ }
87
102
< Form . Item
88
103
label = "Name"
104
+ title = "Administrative level name e.g County"
89
105
name = { [ 'strings' , 'name' , 'en' ] }
90
106
rules = { [
91
- { required : true , message : 'Administrative level name is required' } ,
107
+ {
108
+ required : true ,
109
+ message : 'Administrative level name is required' ,
110
+ } ,
92
111
] }
93
112
>
94
113
< Input />
95
114
</ Form . Item >
96
- { /* end administrative level name */ }
115
+ { /* end:name */ }
116
+
117
+ { /* start: level & parent */ }
118
+ < Row justify = "space-between" >
119
+ { /* start:level */ }
120
+ < Col span = { 11 } >
121
+ < Form . Item
122
+ label = "Level"
123
+ title = "Administrative level number e.g 3"
124
+ name = { [ 'numbers' , 'weight' ] }
125
+ rules = { [
126
+ {
127
+ required : true ,
128
+ message : 'Administrative level number is required' ,
129
+ } ,
130
+ ] }
131
+ >
132
+ < InputNumber min = { 0 } style = { { width : '100%' } } />
133
+ </ Form . Item >
134
+ </ Col >
135
+ { /* end:level */ }
136
+ { /* start:parent */ }
137
+ < Col span = { 11 } >
138
+ < Form . Item
139
+ label = "Parent"
140
+ title = "Administrative level parent e.g Province"
141
+ name = { [ 'relations' , 'parent' , '_id' ] }
142
+ >
143
+ < SearchableSelectInput
144
+ onSearch = { getAdministrativeLevels }
145
+ optionLabel = { ( parent ) => get ( parent , 'strings.name.en' ) }
146
+ optionValue = "_id"
147
+ initialValue = { get (
148
+ administrativeLevel ,
149
+ 'relations.parent' ,
150
+ undefined
151
+ ) }
152
+ />
153
+ </ Form . Item >
154
+ </ Col >
155
+ { /* end:parent */ }
156
+ </ Row >
157
+ { /* end: level & parent */ }
97
158
98
- { /* administrative level description */ }
159
+ { /* start: description */ }
99
160
< Form . Item
100
- { ...formItemLayout } // eslint-disable-line
101
161
label = "Description"
162
+ title = "Administrative level usage description"
102
163
name = { [ 'strings' , 'description' , 'en' ] }
103
164
>
104
165
< TextArea autoSize = { { minRows : 3 , maxRows : 10 } } />
105
166
</ Form . Item >
106
- { /* end administrative level description */ }
167
+ { /* end: description */ }
107
168
108
- { /* form actions */ }
169
+ { /* start: form actions */ }
109
170
< Form . Item wrapperCol = { { span : 24 } } style = { { textAlign : 'right' } } >
110
171
< Button onClick = { onCancel } > Cancel</ Button >
111
172
< Button
@@ -117,26 +178,38 @@ const AdministrativeLevelForm = ({
117
178
Save
118
179
</ Button >
119
180
</ Form . Item >
120
- { /* end form actions */ }
181
+ { /* end: form actions */ }
121
182
</ Form >
122
183
) ;
123
184
} ;
124
185
186
+ AdministrativeLevelForm . defaultProps = {
187
+ administrativeLevel : { } ,
188
+ } ;
189
+
125
190
AdministrativeLevelForm . propTypes = {
126
- isEditForm : PropTypes . bool . isRequired ,
127
191
administrativeLevel : PropTypes . shape ( {
192
+ _id : PropTypes . string ,
128
193
strings : PropTypes . shape ( {
129
- name : PropTypes . shape ( { en : PropTypes . string } ) ,
130
- abbreviation : PropTypes . shape ( { en : PropTypes . string } ) ,
131
- description : PropTypes . shape ( { en : PropTypes . string } ) ,
194
+ name : PropTypes . shape ( {
195
+ en : PropTypes . string . isRequired ,
196
+ } ) ,
197
+ description : PropTypes . shape ( {
198
+ en : PropTypes . string . isRequired ,
199
+ } ) ,
200
+ } ) ,
201
+ numbers : PropTypes . shape ( {
202
+ weight : PropTypes . number . isRequired ,
203
+ } ) ,
204
+ relations : PropTypes . shape ( {
205
+ parent : PropTypes . shape ( {
206
+ _id : PropTypes . string ,
207
+ } ) ,
132
208
} ) ,
133
209
} ) ,
134
- onCancel : PropTypes . func . isRequired ,
210
+ isEditForm : PropTypes . bool . isRequired ,
135
211
posting : PropTypes . bool . isRequired ,
136
- } ;
137
-
138
- AdministrativeLevelForm . defaultProps = {
139
- administrativeLevel : null ,
212
+ onCancel : PropTypes . func . isRequired ,
140
213
} ;
141
214
142
215
export default AdministrativeLevelForm ;
0 commit comments