1
- import { reduxActions } from '@codetanzania/ewea-api-states' ;
2
- import { Form } from '@ant-design/compatible' ;
3
- import '@ant-design/compatible/assets/index.css' ;
4
- import { Button , Col , Input , Row } from 'antd' ;
1
+ import React from 'react' ;
5
2
import PropTypes from 'prop-types' ;
6
- import React , { Component } from 'react' ;
3
+ import { reduxActions } from '@codetanzania/ewea-api-states' ;
4
+ import { Button , Col , Form , Input , Row } from 'antd' ;
5
+ import get from 'lodash/get' ;
6
+
7
7
import { notifyError , notifySuccess } from '../../../util' ;
8
8
9
- /* constants */
9
+ /* redux actions */
10
10
const { postPartyRole, putPartyRole } = reduxActions ;
11
+ /* constants */
11
12
const { TextArea } = Input ;
13
+ const formItemLayout = {
14
+ labelCol : {
15
+ xs : { span : 24 } ,
16
+ sm : { span : 24 } ,
17
+ md : { span : 24 } ,
18
+ lg : { span : 24 } ,
19
+ xl : { span : 24 } ,
20
+ xxl : { span : 24 } ,
21
+ } ,
22
+ wrapperCol : {
23
+ xs : { span : 24 } ,
24
+ sm : { span : 24 } ,
25
+ md : { span : 24 } ,
26
+ lg : { span : 24 } ,
27
+ xl : { span : 24 } ,
28
+ xxl : { span : 24 } ,
29
+ } ,
30
+ } ;
12
31
13
32
/**
14
- * @class
33
+ * @function
15
34
* @name RoleForm
16
- * @description Render React Form
17
- *
18
- * @version 0.1.0
35
+ * @description Form component for creating and editing stakeholder roles
36
+ * @param {object } props Form properties object
37
+ * @param {object|null } props.role Role object to be edited if null will be created
38
+ * @param {boolean } props.posting Flag for showing spinner when posting data to the api
39
+ * @param {Function } props.onCancel Callback function for closing form
40
+ * @returns {object } Render Role Form
41
+ * @version 0.2.0
19
42
* @since 0.1.0
20
43
*/
21
- class RoleForm extends Component {
22
- /**
23
- * @function
24
- * @name handleSubmit
25
- * @description Handle form submit action
26
- *
27
- * @param {object } event onSubmit event
28
- *
29
- * @version 0.1.0
30
- * @since 0.1.0
31
- */
32
- handleSubmit = ( event ) => {
33
- event . preventDefault ( ) ;
34
-
35
- const {
36
- form : { validateFieldsAndScroll } ,
37
- role,
38
- isEditForm,
39
- } = this . props ;
40
-
41
- validateFieldsAndScroll ( ( error , values ) => {
42
- if ( ! error ) {
43
- if ( isEditForm ) {
44
- const updatedRole = { ...role , ...values } ;
45
- putPartyRole (
46
- updatedRole ,
47
- ( ) => {
48
- notifySuccess ( 'Role was updated successfully' ) ;
49
- } ,
50
- ( ) => {
51
- notifyError (
52
- 'Something occurred while updating role, please try again!'
53
- ) ;
54
- }
44
+ const RoleForm = ( { role, posting, onCancel } ) => {
45
+ const onFinish = ( values ) => {
46
+ if ( get ( role , '_id' ) ) {
47
+ const updatedRole = { ...role , ...values } ;
48
+ putPartyRole (
49
+ updatedRole ,
50
+ ( ) => {
51
+ notifySuccess ( 'Role was updated successfully' ) ;
52
+ } ,
53
+ ( ) => {
54
+ notifyError (
55
+ 'Something occurred while updating role, please try again!'
55
56
) ;
56
- } else {
57
- postPartyRole (
58
- values ,
59
- ( ) => {
60
- notifySuccess ( 'Role was created successfully' ) ;
61
- } ,
62
- ( ) => {
63
- notifyError (
64
- 'Something occurred while saving role, please try again!'
65
- ) ;
66
- }
57
+ }
58
+ ) ;
59
+ } else {
60
+ postPartyRole (
61
+ values ,
62
+ ( ) => {
63
+ notifySuccess ( 'Role was created successfully' ) ;
64
+ } ,
65
+ ( ) => {
66
+ notifyError (
67
+ 'Something occurred while saving role, please try again!'
67
68
) ;
68
69
}
69
- }
70
- } ) ;
70
+ ) ;
71
+ }
71
72
} ;
72
73
73
- render ( ) {
74
- const {
75
- isEditForm,
76
- role,
77
- posting,
78
- onCancel,
79
- form : { getFieldDecorator } ,
80
- } = this . props ;
81
-
82
- const formItemLayout = {
83
- labelCol : {
84
- xs : { span : 24 } ,
85
- sm : { span : 24 } ,
86
- md : { span : 24 } ,
87
- lg : { span : 24 } ,
88
- xl : { span : 24 } ,
89
- xxl : { span : 24 } ,
90
- } ,
91
- wrapperCol : {
92
- xs : { span : 24 } ,
93
- sm : { span : 24 } ,
94
- md : { span : 24 } ,
95
- lg : { span : 24 } ,
96
- xl : { span : 24 } ,
97
- xxl : { span : 24 } ,
98
- } ,
99
- } ;
100
-
101
- return (
102
- < Form onSubmit = { this . handleSubmit } autoComplete = "off" >
103
- { /* role name and abbreviation */ }
104
- < Row type = "flex" justify = "space-between" >
105
- < Col xxl = { 17 } xl = { 17 } lg = { 17 } md = { 17 } sm = { 24 } xs = { 24 } >
106
- { /* role name */ }
107
- { /* eslint-disable-next-line react/jsx-props-no-spreading */ }
108
- < Form . Item { ...formItemLayout } label = " Name" >
109
- { getFieldDecorator ( 'strings.name.en' , {
110
- initialValue : isEditForm ? role . strings . name . en : undefined ,
111
- rules : [ { required : true , message : 'Role name is required' } ] ,
112
- } ) ( < Input /> ) }
113
- </ Form . Item >
114
- { /* end role name */ }
115
- </ Col >
74
+ return (
75
+ < Form
76
+ onFinish = { onFinish }
77
+ { ...formItemLayout } // eslint-disable-line
78
+ initialValues = { { ...role } }
79
+ autoComplete = "off"
80
+ >
81
+ { /* role name and abbreviation */ }
82
+ < Row type = "flex" justify = "space-between" >
83
+ < Col xxl = { 17 } xl = { 17 } lg = { 17 } md = { 17 } sm = { 24 } xs = { 24 } >
84
+ { /* role name */ }
85
+ < Form . Item
86
+ name = { [ 'strings' , 'name' , 'en' ] }
87
+ label = "Name"
88
+ rules = { [ { required : true , message : 'Role name is required' } ] }
89
+ >
90
+ < Input />
91
+ </ Form . Item >
92
+ { /* end role name */ }
93
+ </ Col >
116
94
117
- < Col xxl = { 6 } xl = { 6 } lg = { 6 } md = { 6 } sm = { 24 } xs = { 24 } >
118
- { /* role abbreviation */ }
119
- { /* eslint-disable-next-line react/jsx-props-no-spreading */ }
120
- < Form . Item { ...formItemLayout } label = "Abbreviation" >
121
- { getFieldDecorator ( 'strings.abbreviation.en' , {
122
- initialValue : isEditForm
123
- ? role . strings . abbreviation . en
124
- : undefined ,
125
- } ) ( < Input /> ) }
126
- </ Form . Item >
127
- { /* end role abbreviation */ }
128
- </ Col >
129
- </ Row >
130
- { /* end role name and abbreviation */ }
95
+ < Col xxl = { 6 } xl = { 6 } lg = { 6 } md = { 6 } sm = { 24 } xs = { 24 } >
96
+ { /* role abbreviation */ }
97
+ < Form . Item
98
+ name = { [ 'strings' , 'abbreviation' , 'en' ] }
99
+ label = "Abbreviation"
100
+ >
101
+ < Input />
102
+ </ Form . Item >
103
+ { /* end role abbreviation */ }
104
+ </ Col >
105
+ </ Row >
106
+ { /* end role name and abbreviation */ }
131
107
132
- { /* role description */ }
133
- { /* eslint-disable-next-line react/jsx-props-no-spreading */ }
134
- < Form . Item { ...formItemLayout } label = "Description" >
135
- { getFieldDecorator ( 'strings.description.en' , {
136
- initialValue : isEditForm ? role . strings . description . en : undefined ,
137
- } ) ( < TextArea autoSize = { { minRows : 3 , maxRows : 10 } } /> ) }
138
- </ Form . Item >
139
- { /* end role description */ }
108
+ { /* role description */ }
109
+ < Form . Item name = { [ 'strings' , 'description' , 'en' ] } label = "Description" >
110
+ < TextArea autoSize = { { minRows : 3 , maxRows : 10 } } />
111
+ </ Form . Item >
112
+ { /* end role description */ }
140
113
141
- { /* form actions */ }
142
- < Form . Item wrapperCol = { { span : 24 } } style = { { textAlign : 'right' } } >
143
- < Button onClick = { onCancel } > Cancel</ Button >
144
- < Button
145
- style = { { marginLeft : 8 } }
146
- type = "primary"
147
- htmlType = "submit"
148
- loading = { posting }
149
- >
150
- Save
151
- </ Button >
152
- </ Form . Item >
153
- { /* end form actions */ }
154
- </ Form >
155
- ) ;
156
- }
157
- }
114
+ { /* form actions */ }
115
+ < Form . Item wrapperCol = { { span : 24 } } style = { { textAlign : 'right' } } >
116
+ < Button onClick = { onCancel } > Cancel</ Button >
117
+ < Button
118
+ style = { { marginLeft : 8 } }
119
+ type = "primary"
120
+ htmlType = "submit"
121
+ loading = { posting }
122
+ >
123
+ Save
124
+ </ Button >
125
+ </ Form . Item >
126
+ { /* end form actions */ }
127
+ </ Form >
128
+ ) ;
129
+ } ;
158
130
159
131
RoleForm . propTypes = {
160
- isEditForm : PropTypes . bool . isRequired ,
161
132
role : PropTypes . shape ( {
162
133
strings : PropTypes . shape ( {
163
134
name : PropTypes . shape ( { en : PropTypes . string } ) ,
164
135
abbreviation : PropTypes . shape ( { en : PropTypes . string } ) ,
165
136
description : PropTypes . shape ( { en : PropTypes . string } ) ,
166
137
} ) ,
167
138
} ) ,
168
- form : PropTypes . shape ( {
169
- getFieldDecorator : PropTypes . func ,
170
- validateFieldsAndScroll : PropTypes . func ,
171
- } ) . isRequired ,
172
139
onCancel : PropTypes . func . isRequired ,
173
140
posting : PropTypes . bool . isRequired ,
174
141
} ;
@@ -177,4 +144,4 @@ RoleForm.defaultProps = {
177
144
role : null ,
178
145
} ;
179
146
180
- export default Form . create ( ) ( RoleForm ) ;
147
+ export default RoleForm ;
0 commit comments