|
| 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 | +}); |
0 commit comments