Skip to content

Commit

Permalink
Update RegionFilters
Browse files Browse the repository at this point in the history
  • Loading branch information
echaidemenos committed Jul 26, 2024
1 parent 5cf31ed commit 7bf79f5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 25 deletions.
39 changes: 23 additions & 16 deletions apps/frontend/components/Filters/MultiSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const MenuProps = {
};

interface MultiSelectProps {
value?: string[];
options: string[];
onChange: (v: string[]) => void;
placeholder: string;
Expand All @@ -42,6 +43,7 @@ interface MultiSelectProps {
| 'defaultValue'
>
>;
disableMulti?: boolean;
}

const MultiSelect = ({
Expand All @@ -52,9 +54,11 @@ const MultiSelect = ({
formatPrefix,
width = 200,
selectProps = {},
value: propValue = [],
disableMulti = false,
}: MultiSelectProps) => {
const intl = useIntl();
const [value, setValue] = React.useState<string[]>([]);
const [value, setValue] = React.useState<string[]>(propValue);

React.useEffect(() => {
const filtered = value.filter(x => options.includes(x));
Expand All @@ -79,8 +83,9 @@ const MultiSelect = ({
// On autofill we get a stringified value.
const newVal =
typeof newValue === 'string' ? newValue.split(',') : newValue;
setValue(newVal);
onChange(newVal);
const keepLast = disableMulti ? [newVal[newVal.length - 1]] : newVal;
setValue(keepLast);
onChange(keepLast);
};

return (
Expand Down Expand Up @@ -109,19 +114,21 @@ const MultiSelect = ({
MenuProps={MenuProps}
{...selectProps}
>
<MenuItem key="allSelected" value="all">
<Checkbox
checked={isAllSelected}
indeterminate={value.length > 0 && value.length < options.length}
/>
<ListItemText
primary={
<Typography fontWeight="bold">
<FormattedMessage id={allSelectedText} />
</Typography>
}
/>
</MenuItem>
{!disableMulti && (
<MenuItem key="allSelected" value="all">
<Checkbox
checked={isAllSelected}
indeterminate={value.length > 0 && value.length < options.length}
/>
<ListItemText
primary={
<Typography fontWeight="bold">
<FormattedMessage id={allSelectedText} />
</Typography>
}
/>
</MenuItem>
)}
{options.map(option => (
<MenuItem key={option} value={option}>
<Checkbox checked={value.indexOf(option) > -1} />
Expand Down
8 changes: 8 additions & 0 deletions apps/frontend/components/Filters/RegionFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ interface Props {
value: Region;
onChange: (regionValues: Region) => void;
disableAll?: boolean;
disableMulti?: boolean;
}

export const RegionFilters = ({
value,
onChange,
disableAll,
disableMulti,
}: Props): JSX.Element => {
const { user } = useAuth();
const theme = useTheme();
Expand Down Expand Up @@ -67,13 +69,15 @@ export const RegionFilters = ({
<Stack direction="row" gap={theme.spacing(1)} height="100%">
<FormControl>
<MultiSelect
value={value.province}
options={allowedProvinces}
onChange={v => {
onChange({ province: v, district: [], commune: [] });
}}
placeholder="common.province"
allSelectedText="validation_search_params.all-province"
formatPrefix="province"
disableMulti={disableMulti}
selectProps={{
disabled: disableAll === true || user === undefined ? true : false,
startAdornment: (
Expand All @@ -94,13 +98,15 @@ export const RegionFilters = ({

<FormControl>
<MultiSelect
value={value.district}
options={districtsFiltered}
onChange={v => {
onChange({ ...value, district: v, commune: [] });
}}
placeholder="common.district"
allSelectedText="validation_search_params.all-district"
formatPrefix="district"
disableMulti={disableMulti}
selectProps={{
disabled: disableAll === true || value.province.length === 0,
startAdornment: (
Expand All @@ -121,13 +127,15 @@ export const RegionFilters = ({

<FormControl>
<MultiSelect
value={value.commune}
options={communesFiltered}
onChange={v => {
onChange({ ...value, commune: v });
}}
placeholder="common.commune"
allSelectedText="validation_search_params.all-commune"
formatPrefix="commune"
disableMulti={disableMulti}
selectProps={{
disabled: disableAll === true || value.district.length === 0,
startAdornment: (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { Dayjs } from 'dayjs';

export type FloodFormType = {
region: {
province: string;
district: string;
commune: string;
province: string[];
district: string[];
commune: string[];
};
interviewer: string;
disTyp: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ export const FloodFormValidation = ({
const { control, handleSubmit, reset } = useForm<FloodFormType>({
defaultValues: {
region: {
province: formattedForm.province,
district: formattedForm.district,
commune: formattedForm.commune,
province: [formattedForm.province],
district: [formattedForm.district],
commune: [formattedForm.commune],
},
interviewer: formattedForm.entryName,
disTyp: formattedForm.disTyp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ const FloodHeader = ({ control, isEditMode }: FloodHeaderProps) => {
render={({ field: { value, onChange } }) => (
<RegionFilters
value={{
province: [value.province],
district: [value.district],
commune: [value.commune],
province: value.province,
district: value.district,
commune: value.commune,
}}
onChange={onChange}
disableAll={!isEditMode}
disableMulti
/>
)}
/>
Expand Down

0 comments on commit 7bf79f5

Please sign in to comment.