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,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))); const studentLists = await Promise.all((assignmentData.groups as string[][]).map(async (group) => await fetchStudents(group)));
try {
const groupRepository = getGroupRepository(); const groupRepository = getGroupRepository();
await groupRepository.deleteAllByAssignment(assignment); await groupRepository.deleteAllByAssignment(assignment);
await Promise.all( await Promise.all(
studentLists.map(async (students) => { studentLists.map(async (students) => {
const newGroup = groupRepository.create({ const newGroup = groupRepository.create({
@ -121,6 +124,13 @@ export async function putAssignment(classid: string, id: number, assignmentData:
await groupRepository.save(newGroup); 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(
{
cid: assignmentQueryResult.data.value?.assignment.within,
an: assignmentQueryResult.data.value?.assignment.id,
data: assignmentDTO,
},
{
onSuccess: async (newData) => {
if (newData?.assignment) {
await assignmentQueryResult.refetch(); 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>