Skip to content

Commit

Permalink
[fix] 登録されたデータの反映と更新のUseEffectを再修正
Browse files Browse the repository at this point in the history
  • Loading branch information
TkymHrt committed Mar 18, 2024
1 parent 49bd481 commit 46ddcb2
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions view/next-project/src/components/fund_information/EditModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,29 @@ export default function EditModal(props: ModalProps) {
});

const [departmentID, setDepartmentID] = useState<number | string>(1);
const [defaultValue, setDefaultValue] = useState<boolean>(false);

useEffect(() => {
const teacher = props.teachers.find((teacher) => teacher.departmentID === departmentID);
if (teacher && teacher.id) {
setFormData({ ...formData, teacherID: teacher.id });
}
}, [departmentID]);

useEffect(() => {
const selectedTeacher = props.teachers.find(
(teacher) => teacher.id === props.fundInformation.teacherID,
);
if (selectedTeacher) {
setDepartmentID(selectedTeacher.departmentID);
if (defaultValue == false) {
const selectedTeacher = props.teachers.find(
(teacher) => teacher.id === props.fundInformation.teacherID,
);
if (selectedTeacher?.id) {
setDepartmentID(selectedTeacher.departmentID);
setFormData({ ...formData, teacherID: selectedTeacher.id });
}
setTimeout(() => {
setDefaultValue(true);
});
} else {
const relatedTeachers = props.teachers.filter(
(teacher) => teacher.departmentID === departmentID,
);
if (relatedTeachers) {
setFormData({ ...formData, teacherID: relatedTeachers[0].id || 0 });
}
}
}, [props.teachers, props.fundInformation.teacherID]);
}, [departmentID, props.teachers]);

const handler =
(input: string) =>
Expand Down

0 comments on commit 46ddcb2

Please sign in to comment.