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