|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { Connect, reduxActions } from '@codetanzania/ewea-api-states'; |
| 4 | +import { httpActions } from '@codetanzania/ewea-api-client'; |
| 5 | +import { Button, Form } from 'antd'; |
| 6 | +import get from 'lodash/get'; |
| 7 | +import isFunction from 'lodash/isFunction'; |
| 8 | +import SearchableSelectInput from '../../../components/SearchableSelectInput'; |
| 9 | + |
| 10 | +/* http actions */ |
| 11 | +const { getAgencies, getFeatureTypes, getAdministrativeAreas } = httpActions; |
| 12 | + |
| 13 | +/* state actions */ |
| 14 | +const { clearFeatureFilters, filterFeatures } = reduxActions; |
| 15 | + |
| 16 | +/* ui */ |
| 17 | +const labelCol = { |
| 18 | + xs: { span: 24 }, |
| 19 | + sm: { span: 24 }, |
| 20 | + md: { span: 24 }, |
| 21 | + lg: { span: 24 }, |
| 22 | + xl: { span: 24 }, |
| 23 | + xxl: { span: 24 }, |
| 24 | +}; |
| 25 | +const wrapperCol = { |
| 26 | + xs: { span: 24 }, |
| 27 | + sm: { span: 24 }, |
| 28 | + md: { span: 24 }, |
| 29 | + lg: { span: 24 }, |
| 30 | + xl: { span: 24 }, |
| 31 | + xxl: { span: 24 }, |
| 32 | +}; |
| 33 | + |
| 34 | +/** |
| 35 | + * @function |
| 36 | + * @name FeatureFiltersForm |
| 37 | + * @description Feature filters form |
| 38 | + * @param {object} props Filter props |
| 39 | + * @param {object} props.filter Filter object from the store |
| 40 | + * @param {object} props.cached Cached values for lazy loaded values |
| 41 | + * @param {Function} props.onCache Cache value callback |
| 42 | + * @param {Function} props.onClearCache Clear cached values callback |
| 43 | + * @param {Function} props.onCancel Close filter form callback |
| 44 | + * @returns {object} FeatureFiltersForm component |
| 45 | + * @author lally elias <lallyelias87@gmail.com> |
| 46 | + * @license MIT |
| 47 | + * @since 0.1.0 |
| 48 | + * @version 0.1.0 |
| 49 | + * @static |
| 50 | + * @public |
| 51 | + * @example |
| 52 | + * |
| 53 | + * <FeatureFiltersForm |
| 54 | + * filter={filter} |
| 55 | + * cached={cached} |
| 56 | + * onCache={this.handleOnCache} |
| 57 | + * onClearCache={this.handleOnClearCache} |
| 58 | + * onCancel={this.handleCloseFilterForm} |
| 59 | + * /> |
| 60 | + */ |
| 61 | +const FeatureFiltersForm = ({ |
| 62 | + filter, |
| 63 | + cached, |
| 64 | + onCache, |
| 65 | + onClearCache, |
| 66 | + onCancel, |
| 67 | +}) => { |
| 68 | + // form finish(submit) handler |
| 69 | + const onFinish = (values) => { |
| 70 | + // TODO: clear false values |
| 71 | + filterFeatures(values); |
| 72 | + onCancel(); |
| 73 | + }; |
| 74 | + |
| 75 | + /** |
| 76 | + * @function |
| 77 | + * @name onClearFilters |
| 78 | + * @description On clear form filters callback |
| 79 | + * |
| 80 | + * @version 0.1.0 |
| 81 | + * @since 0.1.0 |
| 82 | + */ |
| 83 | + const onClearFilters = () => { |
| 84 | + clearFeatureFilters(); |
| 85 | + if (isFunction(onClearCache)) { |
| 86 | + onClearCache(); |
| 87 | + } |
| 88 | + onCancel(); |
| 89 | + }; |
| 90 | + |
| 91 | + return ( |
| 92 | + <Form |
| 93 | + onFinish={onFinish} |
| 94 | + labelCol={labelCol} |
| 95 | + wrapperCol={wrapperCol} |
| 96 | + autoComplete="off" |
| 97 | + initialValues={{ |
| 98 | + ...filter, |
| 99 | + }} |
| 100 | + > |
| 101 | + {/* start:feature type filter */} |
| 102 | + <Form.Item |
| 103 | + label="By Type" |
| 104 | + title="Critical infrastructure type e.g Hospital" |
| 105 | + name={['relations.type']} |
| 106 | + > |
| 107 | + <SearchableSelectInput |
| 108 | + onSearch={(optns = {}) => { |
| 109 | + return getFeatureTypes(optns); |
| 110 | + }} |
| 111 | + optionLabel={(type) => get(type, 'strings.name.en')} |
| 112 | + optionValue="_id" |
| 113 | + mode="multiple" |
| 114 | + onCache={(type) => onCache({ 'relations.type': type })} |
| 115 | + initialValue={get(cached, 'relations.type')} |
| 116 | + /> |
| 117 | + </Form.Item> |
| 118 | + {/* end:feature type filter */} |
| 119 | + |
| 120 | + {/* start:feature area filter */} |
| 121 | + <Form.Item |
| 122 | + label="By Area" |
| 123 | + title="Critical infrastructure area e.g Dar es Salaam" |
| 124 | + name={['relations.area']} |
| 125 | + > |
| 126 | + <SearchableSelectInput |
| 127 | + onSearch={(optns = {}) => { |
| 128 | + return getAdministrativeAreas(optns); |
| 129 | + }} |
| 130 | + optionLabel={(area) => { |
| 131 | + return get(area, 'strings.name.en'); |
| 132 | + }} |
| 133 | + optionValue="_id" |
| 134 | + mode="multiple" |
| 135 | + onCache={(area) => onCache({ 'relations.area': area })} |
| 136 | + initialValue={get(cached, 'relations.area', [])} |
| 137 | + /> |
| 138 | + </Form.Item> |
| 139 | + {/* end:feature area filter */} |
| 140 | + |
| 141 | + {/* start:feature custodians filter */} |
| 142 | + <Form.Item |
| 143 | + label="By Custodian" |
| 144 | + title="Responsible Agencies e.g Police Force" |
| 145 | + name={['relations.custodians']} |
| 146 | + > |
| 147 | + <SearchableSelectInput |
| 148 | + onSearch={(optns = {}) => { |
| 149 | + return getAgencies(optns); |
| 150 | + }} |
| 151 | + optionLabel={(custodian) => get(custodian, 'name')} |
| 152 | + optionValue="_id" |
| 153 | + mode="multiple" |
| 154 | + onCache={(custodians) => |
| 155 | + onCache({ 'relations.custodians': custodians }) |
| 156 | + } |
| 157 | + initialValue={get(cached, 'relations.custodians', [])} |
| 158 | + /> |
| 159 | + </Form.Item> |
| 160 | + {/* end:feature custodians filter */} |
| 161 | + |
| 162 | + {/* start:form actions */} |
| 163 | + <Form.Item wrapperCol={{ span: 24 }} style={{ textAlign: 'right' }}> |
| 164 | + <Button onClick={onCancel}>Cancel</Button> |
| 165 | + <Button style={{ marginLeft: 8 }} onClick={onClearFilters}> |
| 166 | + Clear |
| 167 | + </Button> |
| 168 | + <Button style={{ marginLeft: 8 }} type="primary" htmlType="submit"> |
| 169 | + Filter |
| 170 | + </Button> |
| 171 | + </Form.Item> |
| 172 | + {/* end:form actions */} |
| 173 | + </Form> |
| 174 | + ); |
| 175 | +}; |
| 176 | + |
| 177 | +FeatureFiltersForm.propTypes = { |
| 178 | + filter: PropTypes.objectOf( |
| 179 | + PropTypes.shape({ |
| 180 | + type: PropTypes.arrayOf( |
| 181 | + PropTypes.shape({ |
| 182 | + _id: PropTypes.string, |
| 183 | + }) |
| 184 | + ), |
| 185 | + area: PropTypes.arrayOf( |
| 186 | + PropTypes.shape({ |
| 187 | + _id: PropTypes.string, |
| 188 | + }) |
| 189 | + ), |
| 190 | + custodians: PropTypes.arrayOf( |
| 191 | + PropTypes.shape({ |
| 192 | + _id: PropTypes.string, |
| 193 | + }) |
| 194 | + ), |
| 195 | + }) |
| 196 | + ), |
| 197 | + cached: PropTypes.shape({ |
| 198 | + type: PropTypes.arrayOf( |
| 199 | + PropTypes.shape({ |
| 200 | + _id: PropTypes.string, |
| 201 | + }) |
| 202 | + ), |
| 203 | + }), |
| 204 | + onCache: PropTypes.func.isRequired, |
| 205 | + onClearCache: PropTypes.func.isRequired, |
| 206 | + onCancel: PropTypes.func.isRequired, |
| 207 | +}; |
| 208 | + |
| 209 | +FeatureFiltersForm.defaultProps = { |
| 210 | + filter: null, |
| 211 | + cached: null, |
| 212 | +}; |
| 213 | + |
| 214 | +export default Connect(FeatureFiltersForm, { |
| 215 | + filter: 'dispatches.filter', |
| 216 | +}); |
0 commit comments