feat: implement student registration on frontend

This commit is contained in:
2025-10-21 17:16:39 +02:00
parent 631c9f9a86
commit 2c07b0bc0f

View File

@@ -1,5 +1,8 @@
<script setup lang="ts">
import { FetchError } from 'ofetch';
import { NewRole } from '~/types/role';
import type { NewUser } from '~/types/user';
const client = useSanctumClient();
useSeoMeta({
title: "Registrácia študenta | ISOP",
@@ -9,11 +12,11 @@ useSeoMeta({
});
const rules = {
required: (v: string) => (!!v && v.trim().length > 0) || 'Povinné pole',
email: (v: string) =>
/.+@.+\..+/.test(v) || 'Zadajte platný email',
optionalEmail: (v: string) =>
(!v || /.+@.+\..+/.test(v)) || 'Zadajte platný email',
required: (v: any) => (!!v && String(v).trim().length > 0) || 'Povinné pole',
student_email: (v: string) =>
(/.+@.+\..+/.test(v) && v.endsWith("@student.ukf.sk")) || 'Zadajte platný študentský email',
personal_email: (v: string) =>
/.+@.+\..+/.test(v) || 'Zadajte platný osobný email',
phone: (v: string) =>
(!v || /^[0-9 +()-]{6,}$/.test(v)) || 'Zadajte platné telefónne číslo',
mustAgree: (v: boolean) => v === true || 'Je potrebné súhlasiť',
@@ -23,15 +26,15 @@ const programs = [
];
const isValid = ref(false);
const form = reactive({
title: '',
const form = ref({
firstName: '',
lastName: '',
address: '',
studentEmail: '',
altEmail: '',
personalEmail: '',
phone: '',
studyProgram: null as (string | null),
studyProgram: programs[0] as string,
year_of_study: 1,
consent: false,
});
@@ -42,12 +45,30 @@ async function handleRegistration() {
error.value = null;
loading.value = true;
try {
// TODO: implement
} catch (e) {
if (e instanceof FetchError && e.response?.status === 422) {
error.value = e.response?._data.message;
const newUser: NewUser = {
email: form.value.studentEmail,
first_name: form.value.firstName,
last_name: form.value.lastName,
phone: form.value.phone,
role: NewRole.STUDENT,
company_data: undefined,
student_data: {
user_id: undefined,
address: form.value.address,
personal_email: form.value.personalEmail,
study_field: form.value.studyProgram
}
};
try {
await client("/register", {
method: 'POST',
body: newUser
});
navigateTo("/");
} catch (e: any) {
error.value = e.data?.message as string;
} finally {
loading.value = false;
}
@@ -61,35 +82,37 @@ async function handleRegistration() {
<!-- Chybová hláška -->
<v-alert v-if="error !== null" density="compact" :text="error" title="Chyba" type="error"
id="login-error-alert" class="mx-auto"></v-alert>
id="login-error-alert" class="mx-auto alert"></v-alert>
<!-- Čakajúca hláška -->
<v-alert v-if="loading" density="compact" text="Prosím čakajte..." title="Spracovávam" type="info"
id="login-error-alert" class="mx-auto"></v-alert>
id="login-error-alert" class="mx-auto alert"></v-alert>
<v-form v-else v-model="isValid" @submit.prevent="handleRegistration">
<v-text-field v-model="form.title" label="Tituly pred:" variant="outlined" density="comfortable" />
<v-text-field v-model="form.firstName" :rules="[rules.required]" label="Meno:" variant="outlined"
density="comfortable" />
<v-text-field v-model="form.lastName" :rules="[rules.required]" label="Priezvisko:" variant="outlined"
density="comfortable" />
<v-text-field v-model="form.address" label="Adresa:" variant="outlined" density="comfortable" />
<v-text-field v-model="form.address" :rules="[rules.required]" label="Adresa:" variant="outlined"
density="comfortable" />
<v-text-field v-model="form.studentEmail" :rules="[rules.required, rules.email]"
<v-text-field v-model="form.studentEmail" :rules="[rules.required, rules.student_email]"
label="Študentský email:" variant="outlined" density="comfortable" />
<v-text-field v-model="form.altEmail" :rules="[rules.optionalEmail]" label="Alternatívny email:"
variant="outlined" density="comfortable" />
<v-text-field v-model="form.personalEmail" :rules="[rules.required, rules.personal_email]"
label="Alternatívny email:" variant="outlined" density="comfortable" />
<v-text-field v-model="form.phone" :rules="[rules.phone]" label="Telefón:" variant="outlined"
density="comfortable" />
<v-text-field v-model="form.phone" :rules="[rules.required, rules.phone]" label="Telefón:"
variant="outlined" density="comfortable" />
<v-select v-model="form.studyProgram" :items="programs" :rules="[rules.required]"
label="Študijný odbor:" variant="outlined" density="comfortable" />
<v-number-input control-variant="split" v-model="form.year_of_study" :rules="[rules.required]"
label="Ročník:" :min="1" :max="5"></v-number-input>
<v-checkbox v-model="form.consent" :rules="[rules.mustAgree]"
label="Súhlasím s podmienkami spracúvania osobných údajov" density="comfortable" />
@@ -102,6 +125,10 @@ async function handleRegistration() {
</template>
<style scoped>
.alert {
margin-bottom: 10px;
}
.page-container {
max-width: 1120px;
margin: 0 auto;