Skip to content

Commit

Permalink
Merge pull request #940 from akvo/bug/932-muliple-administration-is-n…
Browse files Browse the repository at this point in the history
…ot-showing-when-edit

[#932] multiple administration not fetching in edit mobile users fixed
  • Loading branch information
dedenbangkit authored Jan 16, 2024
2 parents 642e764 + 75ed8b5 commit 8c726a6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
9 changes: 3 additions & 6 deletions frontend/src/components/filters/AdministrationDropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,9 @@ const AdministrationDropdown = ({
}
let admItems = null;
if (Array.isArray(e)) {
const multiadministration = await Promise.all(
e.map(async (ID) => {
const apiResponse = await api.get(`administration/${ID}`);
return apiResponse.data;
})
);
const multiadministration = administration
?.find((admLevel) => admLevel.level === lowestLevel.level - 1)
?.children.filter((admItem) => e.includes(admItem.id));
admItems = multiadministration;
} else {
const { data: selectedAdm } = await api.get(`administration/${e}`);
Expand Down
43 changes: 25 additions & 18 deletions frontend/src/pages/mobile-assignment/AddAssignment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ const AddAssignment = () => {
};

const onSelectLevel = async (val) => {
store.update((s) => {
s.administration.length = val;
});
setLevel(val);
};

Expand Down Expand Up @@ -149,45 +152,49 @@ const AddAssignment = () => {
};

const fetchData = useCallback(async () => {
if (id && preload && editAssignment?.id) {
if (id && preload && editAssignment?.id && selectedAdm) {
setPreload(false);
form.setFieldsValue({
...editAssignment,
administrations: editAssignment.administrations.map((a) => a?.id),
forms: editAssignment.forms.map((f) => f?.id),
});

const { data: selectedAdm } = await api.get(
`administration/${editAssignment.administrations
.map((a) => a?.id)
.join(",")}`
const selectedAdministration = await Promise.all(
(editAssignment.administrations.map((a) => a?.id) ?? [])
.filter((p) => p)
.map(async (pID) => {
const apiResponse = await api.get(`administration/${pID}`);
return apiResponse.data;
})
);
if (selectedAdm) {
setLevel(selectedAdm.level + 1);
form.setFieldsValue({ level_id: selectedAdm.level + 1 });
if (selectedAdministration) {
setLevel(selectedAdministration[0].level + 1);
form.setFieldsValue({ level_id: selectedAdministration[0].level + 1 });
}
const parentAdm = await Promise.all(
(selectedAdm?.path?.split(".") ?? [])
(selectedAdministration?.[0]?.path?.split(".") ?? [])
.filter((p) => p)
.map(async (pID) => {
const apiResponse = await api.get(`administration/${pID}`);
return apiResponse.data;
})
);
store.update((s) => {
s.administration = [...parentAdm, selectedAdm]?.map((a, ax) => {
const childLevel = levels.find((l) => l?.level === ax + 1);
return {
...a,
childLevelName: childLevel?.name || null,
};
});
s.administration = [...parentAdm, ...selectedAdministration]?.map(
(a, ax) => {
const childLevel = levels.filter((l) => l?.level === ax + 1);
return {
...a,
childLevelName: childLevel?.name || null,
};
}
);
});
}
if (!id && preload) {
setPreload(false);
}
}, [id, preload, form, editAssignment, levels]);
}, [id, preload, form, editAssignment, levels, selectedAdm]);

useEffect(() => {
fetchData();
Expand Down

0 comments on commit 8c726a6

Please sign in to comment.