Skip to content

Commit b22ccd0

Browse files
committed
feat(administrativelevels): add list and allow crud actions
1 parent 0977e1b commit b22ccd0

File tree

5 files changed

+836
-0
lines changed

5 files changed

+836
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
import {
2+
clearFocalPersonFilters,
3+
Connect,
4+
filterFocalPeople,
5+
} from '@codetanzania/ewea-api-states';
6+
import { httpActions } from '@codetanzania/ewea-api-client';
7+
import { Form } from '@ant-design/compatible';
8+
import '@ant-design/compatible/assets/index.css';
9+
import { Button } from 'antd';
10+
import PropTypes from 'prop-types';
11+
import React, { Component } from 'react';
12+
import SearchableSelectInput from '../../../components/SearchableSelectInput';
13+
14+
/* declarations */
15+
const {
16+
getPartyGroups,
17+
getAdministrativeAreas,
18+
getPartyRoles,
19+
getAgencies,
20+
} = httpActions;
21+
22+
/**
23+
* @class
24+
* @name EventStatusesFilters
25+
* @description Filter modal component for filtering contacts
26+
*
27+
* @version 0.1.0
28+
* @since 0.1.0
29+
*/
30+
class EventStatusesFilters extends Component {
31+
/**
32+
* @function
33+
* @name handleSubmit
34+
* @description Handle filter action
35+
*
36+
* @param {object} event onSubmit event object
37+
* @returns {undefined}
38+
*
39+
* @version 0.1.0
40+
* @since 0.1.0
41+
*/
42+
handleSubmit = (event) => {
43+
event.preventDefault();
44+
const {
45+
form: { validateFields },
46+
onCancel,
47+
} = this.props;
48+
49+
validateFields((error, values) => {
50+
if (!error) {
51+
filterFocalPeople(values);
52+
onCancel();
53+
}
54+
});
55+
};
56+
57+
/**
58+
* @function
59+
* @name handleClearFilter
60+
* @description Action handle when clear
61+
*
62+
* @version 0.1.0
63+
* @since 0.1.0
64+
*/
65+
handleClearFilter = () => {
66+
const { onCancel, onClearCache } = this.props;
67+
clearFocalPersonFilters();
68+
69+
onClearCache();
70+
onCancel();
71+
};
72+
73+
/**
74+
* @function
75+
* @name cacheFilters
76+
* @description cache lazy loaded value from filters
77+
*
78+
* @param {object} values Object with key value of what is to be cached
79+
*
80+
* @version 0.1.0
81+
* @since 0.1.0
82+
*/
83+
cacheFilters = (values) => {
84+
const { onCache } = this.props;
85+
onCache(values);
86+
};
87+
88+
render() {
89+
const {
90+
form: { getFieldDecorator },
91+
onCancel,
92+
filter,
93+
cached,
94+
} = this.props;
95+
96+
const formItemLayout = {
97+
labelCol: {
98+
xs: { span: 24 },
99+
sm: { span: 24 },
100+
md: { span: 24 },
101+
lg: { span: 24 },
102+
xl: { span: 24 },
103+
xxl: { span: 24 },
104+
},
105+
wrapperCol: {
106+
xs: { span: 24 },
107+
sm: { span: 24 },
108+
md: { span: 24 },
109+
lg: { span: 24 },
110+
xl: { span: 24 },
111+
xxl: { span: 24 },
112+
},
113+
};
114+
115+
return (
116+
<Form onSubmit={this.handleSubmit} autoComplete="off">
117+
{/* start contact group filters */}
118+
{/* eslint-disable-next-line react/jsx-props-no-spreading */}
119+
<Form.Item {...formItemLayout} label="By Area(s)">
120+
{getFieldDecorator('area', {
121+
initialValue: filter ? filter.area : [],
122+
})(
123+
<SearchableSelectInput
124+
onSearch={getAdministrativeAreas}
125+
optionLabel={(area) => area.strings.name.en}
126+
optionValue="_id"
127+
mode="multiple"
128+
onCache={(areas) => {
129+
this.cacheFilters({ areas });
130+
}}
131+
initialValue={cached && cached.areas ? cached.areas : []}
132+
/>
133+
)}
134+
</Form.Item>
135+
{/* end contact group filters */}
136+
137+
{/* start contact group filters */}
138+
{/* eslint-disable-next-line react/jsx-props-no-spreading */}
139+
<Form.Item {...formItemLayout} label="By Group(s)">
140+
{getFieldDecorator('group', {
141+
initialValue: filter ? filter.group : [],
142+
})(
143+
<SearchableSelectInput
144+
onSearch={getPartyGroups}
145+
optionLabel={(group) => group.strings.name.en}
146+
optionValue="_id"
147+
mode="multiple"
148+
onCache={(groups) => this.cacheFilters({ groups })}
149+
initialValue={cached && cached.groups ? cached.groups : []}
150+
/>
151+
)}
152+
</Form.Item>
153+
{/* end contact group filters */}
154+
155+
{/* start contact group filters */}
156+
{/* eslint-disable-next-line react/jsx-props-no-spreading */}
157+
<Form.Item {...formItemLayout} label="By Role(s)">
158+
{getFieldDecorator('role', {
159+
initialValue: filter ? filter.role : [],
160+
})(
161+
<SearchableSelectInput
162+
onSearch={getPartyRoles}
163+
optionLabel={(role) => role.strings.name.en}
164+
optionValue="_id"
165+
mode="multiple"
166+
onCache={(roles) => this.cacheFilters({ roles })}
167+
initialValue={cached && cached.roles ? cached.roles : []}
168+
/>
169+
)}
170+
</Form.Item>
171+
{/* end contact group filters */}
172+
173+
{/* start contact group filters */}
174+
{/* eslint-disable-next-line react/jsx-props-no-spreading */}
175+
<Form.Item {...formItemLayout} label="By Agencies">
176+
{getFieldDecorator('party', {
177+
initialValue: filter ? filter.party : [],
178+
})(
179+
<SearchableSelectInput
180+
onSearch={getAgencies}
181+
optionLabel="name"
182+
optionValue="_id"
183+
mode="multiple"
184+
onCache={(agencies) => this.cacheFilters({ agencies })}
185+
initialValue={cached && cached.agencies ? cached.agencies : []}
186+
/>
187+
)}
188+
</Form.Item>
189+
{/* end contact group filters */}
190+
191+
{/* form actions */}
192+
<Form.Item wrapperCol={{ span: 24 }} style={{ textAlign: 'right' }}>
193+
<Button onClick={onCancel}>Cancel</Button>
194+
<Button style={{ marginLeft: 8 }} onClick={this.handleClearFilter}>
195+
Clear
196+
</Button>
197+
<Button style={{ marginLeft: 8 }} type="primary" htmlType="submit">
198+
Filter
199+
</Button>
200+
</Form.Item>
201+
{/* end form actions */}
202+
</Form>
203+
);
204+
}
205+
}
206+
207+
EventStatusesFilters.propTypes = {
208+
filter: PropTypes.objectOf(
209+
PropTypes.shape({
210+
groups: PropTypes.arrayOf(PropTypes.string),
211+
})
212+
),
213+
form: PropTypes.shape({
214+
getFieldDecorator: PropTypes.func,
215+
validateFields: PropTypes.func,
216+
}).isRequired,
217+
onCancel: PropTypes.func.isRequired,
218+
cached: PropTypes.shape({
219+
groups: PropTypes.arrayOf(PropTypes.shape({ name: PropTypes.string })),
220+
areas: PropTypes.arrayOf(PropTypes.shape({ name: PropTypes.string })),
221+
roles: PropTypes.arrayOf(PropTypes.shape({ name: PropTypes.string })),
222+
agencies: PropTypes.arrayOf(PropTypes.shape({ name: PropTypes.string })),
223+
}),
224+
onCache: PropTypes.func.isRequired,
225+
onClearCache: PropTypes.func.isRequired,
226+
};
227+
228+
EventStatusesFilters.defaultProps = {
229+
filter: null,
230+
cached: null,
231+
};
232+
233+
export default Connect(Form.create()(EventStatusesFilters), {
234+
filter: 'eventStatuses.filter',
235+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import {
2+
putAdministrativeLevel,
3+
postAdministrativeLevel,
4+
} from '@codetanzania/ewea-api-states';
5+
import { Button, Input, Form } from 'antd';
6+
import PropTypes from 'prop-types';
7+
import React from 'react';
8+
import { notifyError, notifySuccess } from '../../../util';
9+
10+
/* constants */
11+
const { TextArea } = Input;
12+
const formItemLayout = {
13+
labelCol: {
14+
xs: { span: 24 },
15+
sm: { span: 24 },
16+
md: { span: 24 },
17+
lg: { span: 24 },
18+
xl: { span: 24 },
19+
xxl: { span: 24 },
20+
},
21+
wrapperCol: {
22+
xs: { span: 24 },
23+
sm: { span: 24 },
24+
md: { span: 24 },
25+
lg: { span: 24 },
26+
xl: { span: 24 },
27+
xxl: { span: 24 },
28+
},
29+
};
30+
31+
/**
32+
* @function
33+
* @name AdministrativeLevelForm
34+
* @description Administrative Level Form
35+
* @param {object} props React props
36+
* @returns {object} React Component
37+
* @version 0.1.0
38+
* @since 0.1.0
39+
*/
40+
const AdministrativeLevelForm = ({
41+
administrativeLevel,
42+
isEditForm,
43+
posting,
44+
onCancel,
45+
}) => {
46+
const onFinish = (values) => {
47+
if (isEditForm) {
48+
const updatedEventStatus = { ...administrativeLevel, ...values };
49+
putAdministrativeLevel(
50+
updatedEventStatus,
51+
() => {
52+
notifySuccess('Administrative level was updated successfully');
53+
},
54+
() => {
55+
notifyError(
56+
'Something occurred while updating administrative level, please try again!'
57+
);
58+
}
59+
);
60+
} else {
61+
postAdministrativeLevel(
62+
values,
63+
() => {
64+
notifySuccess('Administrative level was created successfully');
65+
},
66+
() => {
67+
notifyError(
68+
'Something occurred while saving administrative level, please try again!'
69+
);
70+
}
71+
);
72+
}
73+
};
74+
75+
return (
76+
<Form
77+
onFinish={onFinish}
78+
{...formItemLayout} // eslint-disable-line
79+
initialValues={{
80+
...administrativeLevel,
81+
}}
82+
autoComplete="off"
83+
>
84+
{/* administrative level name */}
85+
<Form.Item
86+
label="Name"
87+
name={['strings', 'name', 'en']}
88+
rules={[
89+
{ required: true, message: 'Administrative level name is required' },
90+
]}
91+
>
92+
<Input />
93+
</Form.Item>
94+
{/* end administrative level name */}
95+
96+
{/* administrative level description */}
97+
<Form.Item
98+
{...formItemLayout} // eslint-disable-line
99+
label="Description"
100+
name={['strings', 'description', 'en']}
101+
>
102+
<TextArea autoSize={{ minRows: 3, maxRows: 10 }} />
103+
</Form.Item>
104+
{/* end administrative level description */}
105+
106+
{/* form actions */}
107+
<Form.Item wrapperCol={{ span: 24 }} style={{ textAlign: 'right' }}>
108+
<Button onClick={onCancel}>Cancel</Button>
109+
<Button
110+
style={{ marginLeft: 8 }}
111+
type="primary"
112+
htmlType="submit"
113+
loading={posting}
114+
>
115+
Save
116+
</Button>
117+
</Form.Item>
118+
{/* end form actions */}
119+
</Form>
120+
);
121+
};
122+
123+
AdministrativeLevelForm.propTypes = {
124+
isEditForm: PropTypes.bool.isRequired,
125+
administrativeLevel: PropTypes.shape({
126+
strings: PropTypes.shape({
127+
name: PropTypes.shape({ en: PropTypes.string }),
128+
abbreviation: PropTypes.shape({ en: PropTypes.string }),
129+
description: PropTypes.shape({ en: PropTypes.string }),
130+
}),
131+
}),
132+
onCancel: PropTypes.func.isRequired,
133+
posting: PropTypes.bool.isRequired,
134+
};
135+
136+
AdministrativeLevelForm.defaultProps = {
137+
administrativeLevel: null,
138+
};
139+
140+
export default AdministrativeLevelForm;

0 commit comments

Comments
 (0)