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> {
const assignment = await fetchAssignment(classid, id);
if (assignmentData.groups) {
if (hasDuplicates(assignmentData.groups.flat() as string[])) {
throw new BadRequestException('Student can only be in one group');
@ -110,8 +111,10 @@ export async function putAssignment(classid: string, id: number, assignmentData:
const studentLists = await Promise.all((assignmentData.groups as string[][]).map(async (group) => await fetchStudents(group)));
try {
const groupRepository = getGroupRepository();
await groupRepository.deleteAllByAssignment(assignment);
await Promise.all(
studentLists.map(async (students) => {
const newGroup = groupRepository.create({
@ -121,6 +124,13 @@ export async function putAssignment(classid: string, id: number, assignmentData:
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;
}

View file

@ -130,13 +130,28 @@
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]) => {
if (success && newData?.assignment) {
function updateAssignment(assignmentDTO) {
updateAssignmentMutate.mutate(
{
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> {
const { valid } = await form.value.validate();
@ -149,22 +164,14 @@
deadline: deadline.value ?? null,
};
mutate({
cid: assignmentQueryResult.data.value?.assignment.within,
an: assignmentQueryResult.data.value?.assignment.id,
data: assignmentDTO,
});
updateAssignment(assignmentDTO);
}
async function handleGroupsUpdated(updatedGroups: string[][]): Promise<void> {
const assignmentDTO: AssignmentDTO = {
groups: updatedGroups,
};
mutate({
cid: assignmentQueryResult.data.value?.assignment.within,
an: assignmentQueryResult.data.value?.assignment.id,
data: assignmentDTO,
});
updateAssignment(assignmentDTO);
}
</script>