Skip to content

Commit 77d95af

Browse files
committed
refactor(administrative-levels): improve form & migrate to antd v4
see #250
1 parent 85584aa commit 77d95af

File tree

3 files changed

+155
-77
lines changed

3 files changed

+155
-77
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,172 @@
1-
import { reduxActions } from '@codetanzania/ewea-api-states';
2-
import { Button, Input, Form } from 'antd';
3-
import PropTypes from 'prop-types';
41
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';
57
import { notifyError, notifySuccess } from '../../../util';
8+
import SearchableSelectInput from '../../../components/SearchableSelectInput';
9+
10+
/* http actions */
11+
const { getAdministrativeLevels } = httpActions;
612

7-
/* constants */
13+
/* state actions */
814
const { putAdministrativeLevel, postAdministrativeLevel } = reduxActions;
15+
16+
/* ui */
917
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 },
2725
};
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!';
2842

2943
/**
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
3645
* @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
4055
* @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+
*
4168
*/
4269
const AdministrativeLevelForm = ({
4370
administrativeLevel,
4471
isEditForm,
4572
posting,
4673
onCancel,
4774
}) => {
75+
// form finish(submit) handler
4876
const onFinish = (values) => {
4977
if (isEditForm) {
50-
const updatedEventStatus = { ...administrativeLevel, ...values };
78+
const updates = { ...administrativeLevel, ...values };
5179
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)
6183
);
6284
} else {
6385
postAdministrativeLevel(
6486
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)
7389
);
7490
}
7591
};
7692

7793
return (
7894
<Form
95+
labelCol={labelCol}
96+
wrapperCol={wrapperCol}
7997
onFinish={onFinish}
80-
{...formItemLayout} // eslint-disable-line
81-
initialValues={{
82-
...administrativeLevel,
83-
}}
98+
initialValues={{ ...administrativeLevel }}
8499
autoComplete="off"
85100
>
86-
{/* administrative level name */}
101+
{/* start:name */}
87102
<Form.Item
88103
label="Name"
104+
title="Administrative level name e.g County"
89105
name={['strings', 'name', 'en']}
90106
rules={[
91-
{ required: true, message: 'Administrative level name is required' },
107+
{
108+
required: true,
109+
message: 'Administrative level name is required',
110+
},
92111
]}
93112
>
94113
<Input />
95114
</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 */}
97158

98-
{/* administrative level description */}
159+
{/* start:description */}
99160
<Form.Item
100-
{...formItemLayout} // eslint-disable-line
101161
label="Description"
162+
title="Administrative level usage description"
102163
name={['strings', 'description', 'en']}
103164
>
104165
<TextArea autoSize={{ minRows: 3, maxRows: 10 }} />
105166
</Form.Item>
106-
{/* end administrative level description */}
167+
{/* end:description */}
107168

108-
{/* form actions */}
169+
{/* start:form actions */}
109170
<Form.Item wrapperCol={{ span: 24 }} style={{ textAlign: 'right' }}>
110171
<Button onClick={onCancel}>Cancel</Button>
111172
<Button
@@ -117,26 +178,38 @@ const AdministrativeLevelForm = ({
117178
Save
118179
</Button>
119180
</Form.Item>
120-
{/* end form actions */}
181+
{/* end:form actions */}
121182
</Form>
122183
);
123184
};
124185

186+
AdministrativeLevelForm.defaultProps = {
187+
administrativeLevel: {},
188+
};
189+
125190
AdministrativeLevelForm.propTypes = {
126-
isEditForm: PropTypes.bool.isRequired,
127191
administrativeLevel: PropTypes.shape({
192+
_id: PropTypes.string,
128193
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+
}),
132208
}),
133209
}),
134-
onCancel: PropTypes.func.isRequired,
210+
isEditForm: PropTypes.bool.isRequired,
135211
posting: PropTypes.bool.isRequired,
136-
};
137-
138-
AdministrativeLevelForm.defaultProps = {
139-
administrativeLevel: null,
212+
onCancel: PropTypes.func.isRequired,
140213
};
141214

142215
export default AdministrativeLevelForm;

src/Units/Form/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ const wrapperCol = {
2929
/* messages */
3030
const MESSAGE_POST_SUCCESS = 'Unit was created successfully';
3131
const MESSAGE_POST_ERROR =
32-
'Something occurred while saving Unit, please try again!';
32+
'Something occurred while saving Unit, Please try again!';
3333
const MESSAGE_PUT_SUCCESS = 'Unit was updated successfully';
3434
const MESSAGE_PUT_ERROR =
35-
'Something occurred while updating Unit, please try again!';
35+
'Something occurred while updating Unit, Please try again!';
3636

3737
/**
3838
* @function UnitForm
@@ -169,6 +169,7 @@ UnitForm.defaultProps = {
169169

170170
UnitForm.propTypes = {
171171
unit: PropTypes.shape({
172+
_id: PropTypes.string,
172173
strings: PropTypes.shape({
173174
code: PropTypes.string.isRequired,
174175
name: PropTypes.shape({
@@ -177,7 +178,6 @@ UnitForm.propTypes = {
177178
description: PropTypes.shape({
178179
en: PropTypes.string.isRequired,
179180
}),
180-
_id: PropTypes.string,
181181
}),
182182
}),
183183
isEditForm: PropTypes.bool.isRequired,

src/Units/index.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,8 @@ class UnitList extends Component {
326326
<Topbar
327327
search={{
328328
size: 'large',
329-
placeholder: 'Search for units ...',
330-
title: 'Search for units ...',
329+
placeholder: 'Search units ...',
330+
title: 'Search units ...',
331331
onChange: this.handleListSearch,
332332
value: searchQuery,
333333
}}
@@ -465,15 +465,20 @@ class UnitList extends Component {
465465
}
466466

467467
UnitList.propTypes = {
468-
units: PropTypes.arrayOf(PropTypes.shape({ name: PropTypes.string }))
469-
.isRequired,
468+
units: PropTypes.arrayOf(
469+
PropTypes.shape({
470+
_id: PropTypes.string,
471+
})
472+
).isRequired,
470473
loading: PropTypes.bool.isRequired,
471474
posting: PropTypes.bool.isRequired,
472475
searchQuery: PropTypes.string,
473476
showForm: PropTypes.bool.isRequired,
474477
page: PropTypes.number.isRequired,
475478
total: PropTypes.number.isRequired,
476-
unit: PropTypes.shape({ name: PropTypes.string }),
479+
unit: PropTypes.shape({
480+
_id: PropTypes.string,
481+
}),
477482
};
478483

479484
UnitList.defaultProps = {

0 commit comments

Comments
 (0)