|
| 1 | +// Copyright (c) 2014 - 2023 UNICEF. All rights reserved. |
| 2 | + |
| 3 | +import { useEffect, useState, useRef } from "react"; |
| 4 | +import PropTypes from "prop-types"; |
| 5 | +import { useFormContext } from "react-hook-form"; |
| 6 | +import { FormGroup, FormControlLabel, FormLabel, FormControl, Checkbox } from "@mui/material"; |
| 7 | +import { useLocation } from "react-router-dom"; |
| 8 | +import qs from "qs"; |
| 9 | + |
| 10 | +import css from "../styles.css"; |
| 11 | +import FieldSelect from "../date-filter/field-select"; |
| 12 | +import Panel from "../../panel"; |
| 13 | +import { getOption } from "../../../../record-form"; |
| 14 | +import { useI18n } from "../../../../i18n"; |
| 15 | +import { |
| 16 | + registerInput, |
| 17 | + whichOptions, |
| 18 | + optionText, |
| 19 | + handleMoreFiltersChange, |
| 20 | + resetSecondaryFilter, |
| 21 | + setMoreFilterOnPrimarySection |
| 22 | +} from "../utils"; |
| 23 | +import handleFilterChange, { getFilterProps } from "../value-handlers"; |
| 24 | +import { useMemoizedSelector } from "../../../../../libs"; |
| 25 | +import { selectUserModules } from "../../../../application"; |
| 26 | +import { MODULES } from "../../../../../config"; |
| 27 | + |
| 28 | +import { NAME } from "./constants"; |
| 29 | + |
| 30 | +function Component({ filter, moreSectionFilters = {}, setMoreSectionFilters, mode, reset, setReset }) { |
| 31 | + const i18n = useI18n(); |
| 32 | + const { register, unregister, setValue, user, getValues } = useFormContext(); |
| 33 | + const valueRef = useRef(); |
| 34 | + const modules = useMemoizedSelector(state => selectUserModules(state)); |
| 35 | + const location = useLocation(); |
| 36 | + const moduleIDFromSearchQuery = qs.parse(location.search)?.module_id?.[0]; |
| 37 | + |
| 38 | + const moduleOptions = modules |
| 39 | + .map(primeroModule => ({ id: primeroModule.unique_id, display_name: primeroModule.name })) |
| 40 | + .filter(primeroModule => ![MODULES.GBV, MODULES.MRM].includes(primeroModule.id)) |
| 41 | + .toJS(); |
| 42 | + |
| 43 | + const { options, fieldName, optionStringsSource, isObject } = getFilterProps({ |
| 44 | + filter, |
| 45 | + user, |
| 46 | + i18n |
| 47 | + }); |
| 48 | + |
| 49 | + const defaultValue = isObject ? {} : []; |
| 50 | + const [inputValue, setInputValue] = useState(defaultValue); |
| 51 | + const [selectValue, setSelectValue] = useState(moduleIDFromSearchQuery || getValues("module_id.0")); |
| 52 | + |
| 53 | + const setSecondaryValues = (name, values) => { |
| 54 | + setValue(name, values); |
| 55 | + setInputValue(values); |
| 56 | + }; |
| 57 | + |
| 58 | + const handleReset = () => { |
| 59 | + setValue(fieldName, defaultValue); |
| 60 | + resetSecondaryFilter(mode?.secondary, fieldName, getValues()[fieldName], moreSectionFilters, setMoreSectionFilters); |
| 61 | + }; |
| 62 | + |
| 63 | + useEffect(() => { |
| 64 | + registerInput({ |
| 65 | + register, |
| 66 | + name: fieldName, |
| 67 | + ref: valueRef, |
| 68 | + defaultValue, |
| 69 | + setInputValue |
| 70 | + }); |
| 71 | + |
| 72 | + setMoreFilterOnPrimarySection(moreSectionFilters, fieldName, setSecondaryValues); |
| 73 | + |
| 74 | + if (reset && !mode?.defaultFilter) { |
| 75 | + setValue(fieldName, defaultValue); |
| 76 | + handleReset(); |
| 77 | + } |
| 78 | + |
| 79 | + return () => { |
| 80 | + unregister(fieldName); |
| 81 | + if (setReset) { |
| 82 | + setReset(false); |
| 83 | + } |
| 84 | + }; |
| 85 | + }, [register, unregister, fieldName]); |
| 86 | + |
| 87 | + const lookups = useMemoizedSelector(state => getOption(state, optionStringsSource, i18n.locale)); |
| 88 | + |
| 89 | + const filterOptions = whichOptions({ |
| 90 | + optionStringsSource, |
| 91 | + lookups, |
| 92 | + options: options?.[moduleOptions.length === 1 ? moduleOptions[0].id : selectValue], |
| 93 | + i18n |
| 94 | + }); |
| 95 | + |
| 96 | + const handleSelectChange = event => { |
| 97 | + const { value } = event.target; |
| 98 | + |
| 99 | + setSelectValue(value); |
| 100 | + setValue("module_id", [value]); |
| 101 | + }; |
| 102 | + |
| 103 | + const handleChange = event => { |
| 104 | + handleFilterChange({ |
| 105 | + type: isObject ? "objectCheckboxes" : "checkboxes", |
| 106 | + event, |
| 107 | + setInputValue, |
| 108 | + inputValue, |
| 109 | + setValue, |
| 110 | + fieldName |
| 111 | + }); |
| 112 | + |
| 113 | + if (mode?.secondary) { |
| 114 | + handleMoreFiltersChange(moreSectionFilters, setMoreSectionFilters, fieldName, getValues()[fieldName]); |
| 115 | + } |
| 116 | + }; |
| 117 | + const renderOptions = () => { |
| 118 | + return filterOptions?.map(option => { |
| 119 | + return ( |
| 120 | + <FormControlLabel |
| 121 | + key={`${fieldName}-${option.id}-form-control`} |
| 122 | + control={ |
| 123 | + <Checkbox |
| 124 | + onChange={handleChange} |
| 125 | + value={option.id} |
| 126 | + checked={isObject ? option.key in inputValue : inputValue.includes(String(option.id))} |
| 127 | + /> |
| 128 | + } |
| 129 | + label={optionText(option, i18n)} |
| 130 | + /> |
| 131 | + ); |
| 132 | + }); |
| 133 | + }; |
| 134 | + |
| 135 | + return ( |
| 136 | + <Panel |
| 137 | + filter={filter} |
| 138 | + getValues={getValues} |
| 139 | + handleReset={handleReset} |
| 140 | + selectedDefaultValueField={isObject ? "or" : null} |
| 141 | + > |
| 142 | + <FormControl component="fieldset" sx={{ mt: 0, width: "100%" }}> |
| 143 | + <FormLabel component="legend">{i18n.t("cases.status")}</FormLabel> |
| 144 | + {moduleOptions.length > 1 && ( |
| 145 | + <FieldSelect options={moduleOptions} handleSelectedField={handleSelectChange} selectedField={selectValue} /> |
| 146 | + )} |
| 147 | + <div className={css.panelContent}>{filterOptions && <FormGroup>{renderOptions()}</FormGroup>}</div> |
| 148 | + </FormControl> |
| 149 | + </Panel> |
| 150 | + ); |
| 151 | +} |
| 152 | + |
| 153 | +Component.displayName = NAME; |
| 154 | + |
| 155 | +Component.propTypes = { |
| 156 | + filter: PropTypes.object.isRequired, |
| 157 | + mode: PropTypes.shape({ |
| 158 | + defaultFilter: PropTypes.bool, |
| 159 | + secondary: PropTypes.bool |
| 160 | + }), |
| 161 | + moreSectionFilters: PropTypes.object, |
| 162 | + reset: PropTypes.bool, |
| 163 | + setMoreSectionFilters: PropTypes.func, |
| 164 | + setReset: PropTypes.func |
| 165 | +}; |
| 166 | + |
| 167 | +export default Component; |
0 commit comments