fix: snackbar message bij groep aanpassen

This commit is contained in:
Gabriellvl 2025-05-20 13:50:24 +02:00
parent 3dae015e3e
commit 1c118a42f3
2 changed files with 44 additions and 27 deletions

View file

@ -103,6 +103,7 @@ function hasDuplicates(arr: string[]): boolean {
export async function putAssignment(classid: string, id: number, assignmentData: Partial<AssignmentDTO>): Promise<AssignmentDTO> { export async function putAssignment(classid: string, id: number, assignmentData: Partial<AssignmentDTO>): Promise<AssignmentDTO> {
const assignment = await fetchAssignment(classid, id); const assignment = await fetchAssignment(classid, id);
if (assignmentData.groups) { if (assignmentData.groups) {
if (hasDuplicates(assignmentData.groups.flat() as string[])) { if (hasDuplicates(assignmentData.groups.flat() as string[])) {
throw new BadRequestException('Student can only be in one group'); throw new BadRequestException('Student can only be in one group');
@ -110,17 +111,26 @@ export async function putAssignment(classid: string, id: number, assignmentData:
const studentLists = await Promise.all((assignmentData.groups as string[][]).map(async (group) => await fetchStudents(group))); const studentLists = await Promise.all((assignmentData.groups as string[][]).map(async (group) => await fetchStudents(group)));
const groupRepository = getGroupRepository(); try {
await groupRepository.deleteAllByAssignment(assignment); const groupRepository = getGroupRepository();
await Promise.all( await groupRepository.deleteAllByAssignment(assignment);
studentLists.map(async (students) => {
const newGroup = groupRepository.create({ await Promise.all(
assignment: assignment, studentLists.map(async (students) => {
members: students, const newGroup = groupRepository.create({
}); assignment: assignment,
await groupRepository.save(newGroup); members: students,
}) });
); await groupRepository.save(newGroup);
})
);
} catch(e: unknown) {
if (e instanceof ForeignKeyConstraintViolationException || e instanceof PostgreSqlExceptionConverter) {
throw new ConflictException('Cannot update assigment with questions or submissions');
} else {
throw e;
}
}
delete assignmentData.groups; delete assignmentData.groups;
} }

View file

@ -130,13 +130,28 @@
return `/learningPath/${lp.hruid}/${lp.language}/${lp.startNode.learningobjectHruid}?forGroup=${groupNo}&assignmentNo=${props.assignmentId}&classId=${props.classId}`; return `/learningPath/${lp.hruid}/${lp.language}/${lp.startNode.learningobjectHruid}?forGroup=${groupNo}&assignmentNo=${props.assignmentId}&classId=${props.classId}`;
} }
const { mutate, data, isSuccess } = useUpdateAssignmentMutation(); const updateAssignmentMutate = useUpdateAssignmentMutation();
watch([isSuccess, data], async ([success, newData]) => { function updateAssignment(assignmentDTO) {
if (success && newData?.assignment) { updateAssignmentMutate.mutate(
await assignmentQueryResult.refetch(); {
} cid: assignmentQueryResult.data.value?.assignment.within,
}); an: assignmentQueryResult.data.value?.assignment.id,
data: assignmentDTO,
},
{
onSuccess: async (newData) => {
if (newData?.assignment) {
await assignmentQueryResult.refetch();
}
},
onError: (err: any) => {
const message = err.response?.data?.error || err.message || t("unknownError");
showSnackbar(t("failed") + ": " + message, "error");
},
}
);
}
async function saveChanges(): Promise<void> { async function saveChanges(): Promise<void> {
const { valid } = await form.value.validate(); const { valid } = await form.value.validate();
@ -149,22 +164,14 @@
deadline: deadline.value ?? null, deadline: deadline.value ?? null,
}; };
mutate({ updateAssignment(assignmentDTO);
cid: assignmentQueryResult.data.value?.assignment.within,
an: assignmentQueryResult.data.value?.assignment.id,
data: assignmentDTO,
});
} }
async function handleGroupsUpdated(updatedGroups: string[][]): Promise<void> { async function handleGroupsUpdated(updatedGroups: string[][]): Promise<void> {
const assignmentDTO: AssignmentDTO = { const assignmentDTO: AssignmentDTO = {
groups: updatedGroups, groups: updatedGroups,
}; };
mutate({ updateAssignment(assignmentDTO);
cid: assignmentQueryResult.data.value?.assignment.within,
an: assignmentQueryResult.data.value?.assignment.id,
data: assignmentDTO,
});
} }
</script> </script>