style: refactor het script om overzichtelijkere volgorde te creeeren
This commit is contained in:
parent
d2a7eed4ad
commit
524de10d73
1 changed files with 72 additions and 58 deletions
|
@ -5,13 +5,14 @@
|
||||||
import { validate, version } from "uuid";
|
import { validate, version } from "uuid";
|
||||||
import type { TeacherDTO } from "@dwengo-1/common/interfaces/teacher";
|
import type { TeacherDTO } from "@dwengo-1/common/interfaces/teacher";
|
||||||
import type { ClassDTO } from "@dwengo-1/common/interfaces/class";
|
import type { ClassDTO } from "@dwengo-1/common/interfaces/class";
|
||||||
import { useStudentClassesQuery } from "@/queries/students";
|
import { useCreateJoinRequestMutation, useStudentClassesQuery } from "@/queries/students";
|
||||||
import type { StudentDTO } from "@dwengo-1/common/interfaces/student";
|
import type { StudentDTO } from "@dwengo-1/common/interfaces/student";
|
||||||
import { StudentController } from "@/controllers/students";
|
import { StudentController } from "@/controllers/students";
|
||||||
|
|
||||||
const { t } = useI18n();
|
const { t } = useI18n();
|
||||||
const studentController: StudentController = new StudentController();
|
const studentController: StudentController = new StudentController();
|
||||||
|
|
||||||
|
// TODO: remove and use correct type
|
||||||
interface Invitation {
|
interface Invitation {
|
||||||
id: string;
|
id: string;
|
||||||
class: ClassDTO;
|
class: ClassDTO;
|
||||||
|
@ -19,38 +20,76 @@
|
||||||
receiver: TeacherDTO;
|
receiver: TeacherDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
const classes: ComputedRef<ClassDTO[]> = computed(() => {
|
// determine if role is student or teacher to render correct view
|
||||||
const result: ClassDTO[] = [];
|
const role: string = authState.authState.activeRole!;
|
||||||
if (!classesResponse.value) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
if (classesResponse.value.classes.length === 0) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
if (typeof classesResponse.value.classes[0] === "string") {
|
|
||||||
for (const classid of classesResponse.value.classes) {
|
|
||||||
// TODO when class queries are ready
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
return classesResponse.value.classes as ClassDTO[];
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
|
// username of logged in student or teacher
|
||||||
const username = ref<string | undefined>(undefined);
|
const username = ref<string | undefined>(undefined);
|
||||||
|
|
||||||
const { data: classesResponse, isLoading, error } = useStudentClassesQuery(username);
|
// find the username of the logged in user so it can be used to fetch other information
|
||||||
|
// when loading the page
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
const userObject = await authState.loadUser();
|
const userObject = await authState.loadUser();
|
||||||
username.value = userObject?.profile?.preferred_username ?? undefined;
|
username.value = userObject?.profile?.preferred_username ?? undefined;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Get role of user
|
// fetch all classes of the logged in student
|
||||||
const role: string = authState.authState.activeRole!;
|
const { data: classesResponse, isLoading, error } = useStudentClassesQuery(username);
|
||||||
|
|
||||||
// TODO: change to DTO
|
// empty list when classes are not yet loaded, else the list of classes of the user
|
||||||
const invitations = ref<Invitation[]>([]);
|
const classes: ComputedRef<ClassDTO[]> = computed(() => {
|
||||||
|
|
||||||
|
// the classes are not yet fetched
|
||||||
|
if (!classesResponse.value) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
// the user has no classes
|
||||||
|
if (classesResponse.value.classes.length === 0) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
if (typeof classesResponse.value.classes[0] === "string") {
|
||||||
|
// should not occur because value of *full* is true
|
||||||
|
// must be caught because typescript can't know the type
|
||||||
|
// i chose to return an empty list if this occurs
|
||||||
|
// it is also possible to fetch all classes from the id's returned
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
return classesResponse.value.classes as ClassDTO[];
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
// students of selected class are shown when logged in student presses on the member count
|
||||||
|
const selectedClass = ref<ClassDTO | null>(null);
|
||||||
|
const students = ref<StudentDTO[]>([]);
|
||||||
|
|
||||||
|
// Boolean that handles visibility for dialogs
|
||||||
|
// For students: clicking on membercount will show a dialog with all members
|
||||||
|
// For teachers: creating a class will generate a popup with the generated code
|
||||||
|
const dialog = ref(false);
|
||||||
|
|
||||||
|
// Function to display all members of a class in a dialog
|
||||||
|
async function openDialog(c: ClassDTO) {
|
||||||
|
selectedClass.value = c;
|
||||||
|
|
||||||
|
// Clear previous value
|
||||||
|
students.value = [];
|
||||||
|
dialog.value = true;
|
||||||
|
|
||||||
|
// fetch students from their usernames to display their full names
|
||||||
|
const studentDTOs: (StudentDTO | null)[] = await Promise.all(
|
||||||
|
c.students.map(async (uid) => {
|
||||||
|
try {
|
||||||
|
const res = await studentController.getByUsername(uid);
|
||||||
|
return res.student;
|
||||||
|
} catch (_) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
// only show students that are not fetched ass *null*
|
||||||
|
students.value = studentDTOs.filter(Boolean) as StudentDTO[];
|
||||||
|
}
|
||||||
|
|
||||||
// For students: code that they give in when sending a class join request
|
// For students: code that they give in when sending a class join request
|
||||||
// For teachers: code that they get when they create a new class
|
// For teachers: code that they get when they create a new class
|
||||||
|
@ -65,46 +104,21 @@
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const { mutate, isError } = useCreateJoinRequestMutation();
|
||||||
|
|
||||||
// Function called when a student submits a code to join a class
|
// Function called when a student submits a code to join a class
|
||||||
function submitCode() {
|
function submitCode() {
|
||||||
// Check if the code is valid
|
// Check if the code is valid
|
||||||
if (code.value !== undefined && validate(code.value) && version(code.value) === 4) {
|
if (code.value !== undefined && validate(code.value) && version(code.value) === 4) {
|
||||||
// TODO: temp function that does not use the backend
|
// TODO: uncomment when fixed
|
||||||
|
// mutate( { username : username.value! , classId : code.value });
|
||||||
|
|
||||||
console.log("Code submitted:", code.value);
|
console.log("Code submitted:", code.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Boolean that handles visibility for dialogs
|
// TODO: implement correctly
|
||||||
// For students: clicking on membercount will show a dialog with all members
|
const invitations = ref<Invitation[]>([]);
|
||||||
// For teachers: creating a class will generate a popup with the generated code
|
|
||||||
const dialog = ref(false);
|
|
||||||
|
|
||||||
// List of students in the selected class
|
|
||||||
const students = ref<StudentDTO[]>([]);
|
|
||||||
|
|
||||||
// Selected class itself
|
|
||||||
const selectedClass = ref<ClassDTO | null>(null);
|
|
||||||
|
|
||||||
// Function to display all members of a class
|
|
||||||
async function openDialog(c: ClassDTO) {
|
|
||||||
selectedClass.value = c;
|
|
||||||
|
|
||||||
// Clear previous value
|
|
||||||
students.value = [];
|
|
||||||
dialog.value = true;
|
|
||||||
|
|
||||||
const studentDTOs: (StudentDTO | null)[] = await Promise.all(
|
|
||||||
c.students.map(async (uid) => {
|
|
||||||
try {
|
|
||||||
const res = await studentController.getByUsername(uid);
|
|
||||||
return res.student;
|
|
||||||
} catch (_) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
students.value = studentDTOs.filter(Boolean) as StudentDTO[];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function to handle a accepted invitation request
|
// Function to handle a accepted invitation request
|
||||||
function acceptRequest() {
|
function acceptRequest() {
|
||||||
|
@ -118,7 +132,7 @@
|
||||||
console.log("request denied");
|
console.log("request denied");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Catch the value a teacher inserts when making a class
|
// teacher should be able to set a displayname when making a class
|
||||||
const className = ref<string>("");
|
const className = ref<string>("");
|
||||||
|
|
||||||
// The name can only contain dash, underscore letters and numbers
|
// The name can only contain dash, underscore letters and numbers
|
||||||
|
@ -147,7 +161,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the unique code is copied, set this to true so it's being displayed for the user
|
// show the teacher, copying of the code was a successs
|
||||||
const copied = ref(false);
|
const copied = ref(false);
|
||||||
|
|
||||||
// Copy the generated code to the clipboard
|
// Copy the generated code to the clipboard
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue