Skip to content

Commit 52b45ae

Browse files
committed
refactor(roles): migrate form to antd v4
see #250
1 parent de3bc87 commit 52b45ae

File tree

2 files changed

+113
-146
lines changed

2 files changed

+113
-146
lines changed

src/Stakeholders/Roles/Form/index.js

+112-145
Original file line numberDiff line numberDiff line change
@@ -1,174 +1,141 @@
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';
52
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+
77
import { notifyError, notifySuccess } from '../../../util';
88

9-
/* constants */
9+
/* redux actions */
1010
const { postPartyRole, putPartyRole } = reduxActions;
11+
/* constants */
1112
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+
};
1231

1332
/**
14-
* @class
33+
* @function
1534
* @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
1942
* @since 0.1.0
2043
*/
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!'
5556
);
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!'
6768
);
6869
}
69-
}
70-
});
70+
);
71+
}
7172
};
7273

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>
11694

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 */}
131107

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 */}
140113

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+
};
158130

159131
RoleForm.propTypes = {
160-
isEditForm: PropTypes.bool.isRequired,
161132
role: PropTypes.shape({
162133
strings: PropTypes.shape({
163134
name: PropTypes.shape({ en: PropTypes.string }),
164135
abbreviation: PropTypes.shape({ en: PropTypes.string }),
165136
description: PropTypes.shape({ en: PropTypes.string }),
166137
}),
167138
}),
168-
form: PropTypes.shape({
169-
getFieldDecorator: PropTypes.func,
170-
validateFieldsAndScroll: PropTypes.func,
171-
}).isRequired,
172139
onCancel: PropTypes.func.isRequired,
173140
posting: PropTypes.bool.isRequired,
174141
};
@@ -177,4 +144,4 @@ RoleForm.defaultProps = {
177144
role: null,
178145
};
179146

180-
export default Form.create()(RoleForm);
147+
export default RoleForm;

src/Stakeholders/Roles/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ class Roles extends Component {
170170
* @since 0.1.0
171171
*/
172172
handleAfterCloseForm = () => {
173+
selectPartyRole(null);
173174
this.setState({ isEditForm: false });
174175
};
175176

@@ -431,7 +432,6 @@ class Roles extends Component {
431432
>
432433
<RoleForm
433434
posting={posting}
434-
isEditForm={isEditForm}
435435
role={role}
436436
onCancel={this.closePartyRolesForm}
437437
/>

0 commit comments

Comments
 (0)