Skip to content

Commit

Permalink
Merge pull request #1102 from akvo/feature/1099-add-select-all-in-vil…
Browse files Browse the repository at this point in the history
…lage-dropdown

[#1099] select all village added for mobile user adding
  • Loading branch information
dedenbangkit committed Jan 31, 2024
2 parents 858d738 + 9aa3d19 commit fa0759f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
58 changes: 56 additions & 2 deletions frontend/src/components/filters/AdministrationDropdown.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect } from "react";
import React, { useEffect, useState } from "react";
import "./style.scss";
import { Select, Space } from "antd";
import { Select, Space, Checkbox, Row, Col } from "antd";
import PropTypes from "prop-types";
import { store, api } from "../../lib";
import { useCallback } from "react";
Expand All @@ -16,9 +16,12 @@ const AdministrationDropdown = ({
currentId = null,
onChange,
limitLevel = false,
isSelectAllVillage = false,
selectedAdministrations = [],
...props
}) => {
const { user, administration, levels } = store.useState((state) => state);
const [checked, setChecked] = useState(false);
/**
* Get lowest level administrator from maxLevel.
* otherwise, sort asc by level and get the last item from levels global state
Expand Down Expand Up @@ -49,6 +52,15 @@ const AdministrationDropdown = ({
fetchUserAdmin();
}, [fetchUserAdmin, persist]);

useEffect(() => {
const multiadministration = administration?.find(
(admLevel) => admLevel.level === lowestLevel.level - 1
)?.children;
if (multiadministration?.length === selectedAdministrations?.length) {
setChecked(true);
}
}, [administration, selectedAdministrations, lowestLevel.level]);

const handleChange = async (e, index) => {
if (!e) {
return;
Expand All @@ -73,6 +85,39 @@ const AdministrationDropdown = ({
}
};

const handleSelectAllVillage = (e) => {
if (e.target.checked) {
setChecked(true);
let admItems = null;
const multiadministration = administration?.find(
(admLevel) => admLevel.level === lowestLevel.level - 1
)?.children;
admItems = multiadministration;
if (selectedAdministrations.length === admItems.length) {
return;
}
store.update((s) => {
s.administration = s.administration.concat(admItems);
});
if (onChange) {
const _values = admItems.map((item) => item.id);
onChange(_values);
}
} else {
setChecked(false);
store.update((s) => {
s.administration = s.administration.filter(
(data) => data.level <= lowestLevel.level - 1
);
});
if (onChange) {
onChange(
administration.filter((data) => data.level <= lowestLevel.level - 1)
);
}
}
};

const handleClear = (index) => {
store.update((s) => {
s.administration.length = index + 1;
Expand Down Expand Up @@ -151,6 +196,15 @@ const AdministrationDropdown = ({
);
}
})}
{isSelectAllVillage && maxLevel === 5 && (
<Row className="form-row">
<Col span={24}>
<Checkbox onChange={handleSelectAllVillage} checked={checked}>
Select all village
</Checkbox>
</Col>
</Row>
)}
</Space>
);
}
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/pages/mobile-assignment/AddAssignment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ const AddAssignment = () => {
}}
persist={id ? true : false}
allowMultiple
isSelectAllVillage={true}
selectedAdministrations={selectedAdministrations}
/>
</Form.Item>
</div>
Expand Down

0 comments on commit fa0759f

Please sign in to comment.