feat: query in link, laad klas code in + fix: regex fix

This commit is contained in:
Gabriellvl 2025-05-14 11:02:25 +02:00
parent c0b0e01eea
commit 5bd5748706

View file

@ -2,7 +2,7 @@
import { useI18n } from "vue-i18n"; import { useI18n } from "vue-i18n";
import authState from "@/services/auth/auth-service.ts"; import authState from "@/services/auth/auth-service.ts";
import { computed, onMounted, ref } from "vue"; import { computed, onMounted, ref } from "vue";
import { validate, version } from "uuid"; import { useRoute } from "vue-router";
import type { ClassDTO } from "@dwengo-1/common/interfaces/class"; import type { ClassDTO } from "@dwengo-1/common/interfaces/class";
import { useCreateJoinRequestMutation, 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";
@ -15,6 +15,7 @@
import "../../assets/common.css"; import "../../assets/common.css";
const { t } = useI18n(); const { t } = useI18n();
const route = useRoute();
// Username of logged in student // Username of logged in student
const username = ref<string | undefined>(undefined); const username = ref<string | undefined>(undefined);
@ -38,6 +39,11 @@
} finally { } finally {
isLoading.value = false; isLoading.value = false;
} }
const queryCode = route.query.code as string | undefined;
if (queryCode) {
code.value = queryCode;
}
}); });
// Fetch all classes of the logged in student // Fetch all classes of the logged in student
@ -75,11 +81,15 @@
// The code a student sends in to join a class needs to be formatted as v4 to be valid // The code a student sends in to join a class needs to be formatted as v4 to be valid
// These rules are used to display a message to the user if they use a code that has an invalid format // These rules are used to display a message to the user if they use a code that has an invalid format
function codeRegex(value: string){
return /^[a-zA-Z0-9]{6}$/.test(value)
}
const codeRules = [ const codeRules = [
(value: string | undefined): string | boolean => { (value: string | undefined): string | boolean => {
if (value === undefined || value === "") { if (value === undefined || value === "") {
return true; return true;
} else if (value !== undefined && validate(value) && version(value) === 4) { } else if (codeRegex(value)) {
return true; return true;
} }
return t("invalidFormat"); return t("invalidFormat");
@ -92,7 +102,7 @@
// 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(): void { function submitCode(): void {
// 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 && codeRegex(code.value)) {
mutate( mutate(
{ username: username.value!, classId: code.value }, { username: username.value!, classId: code.value },
{ {
@ -260,7 +270,7 @@
<v-text-field <v-text-field
label="CODE" label="CODE"
v-model="code" v-model="code"
placeholder="XXXXXXXX-XXXX-4XXX-XXXX-XXXXXXXXXXXX" placeholder="XXXXXX"
:rules="codeRules" :rules="codeRules"
variant="outlined" variant="outlined"
></v-text-field> ></v-text-field>