mentorenwahl/frontend_old/src/routes/index.svelte

182 lines
4.5 KiB
Svelte
Raw Normal View History

2022-02-13 08:40:06 +00:00
<script lang="ts" context="module">
export function load({ session }: { session: App.Session }) {
if (!session.user.token) {
return {
status: 302,
redirect: "/login",
};
}
return {
props: {
session,
},
};
}
</script>
<script lang="ts">
2022-02-15 19:59:56 +00:00
import { operationStore, query, gql, mutation } from "@urql/svelte";
2022-11-04 20:23:36 +00:00
// import * as svelteForms from "svelte-forms";
// import * as validators from "svelte-forms/validators";
2022-02-13 08:40:06 +00:00
2022-11-04 20:23:36 +00:00
import type { User } from "$lib/graphql";
2022-02-13 08:40:06 +00:00
import { UserRole } from "$lib/graphql";
2022-11-04 20:23:36 +00:00
import StudentHome from "$lib/StudentHome.svelte";
2022-02-13 08:40:06 +00:00
interface Data {
me: User;
}
const meStore = operationStore<Data>(gql`
query Me {
me {
2022-03-03 17:51:41 +00:00
firstName
2022-02-13 08:40:06 +00:00
role
admin
student {
id
vote {
id
}
}
teacher {
id
}
}
}
`);
query(meStore);
2022-02-15 19:59:56 +00:00
2022-10-31 08:47:26 +00:00
// const teacherRegisterFormMaxStudents = svelteForms.field("maxStudents", 0, [
// validators.required(),
// validators.min(0),
// ]);
// const teacheRegisterForm = svelteForms.form(teacherRegisterFormMaxStudents);
// interface RegisterTeacherData {
// registerTeacher: Teacher;
// }
// interface RegisterTeacherVars {
// maxStudents: number;
// }
// const registerTeacherStore = operationStore<
// RegisterTeacherData,
// RegisterTeacherVars
// >(gql`
// mutation RegisterTeacher($maxStudents: Int!) {
// registerTeacher(input: { maxStudents: $maxStudents }) {
// id
// }
// }
// `);
// const registerTeacherMutation = mutation(registerTeacherStore);
// async function registerTeacher(): Promise<void> {
// await registerTeacherMutation({
// maxStudents: $teacherRegisterFormMaxStudents.value,
// });
// if (!$registerTeacherStore.error && $registerTeacherStore.data) {
// location.reload();
// }
// }
// interface RegisterStudentData {
// registerStudent: Student;
// }
// const registerStudentStore = operationStore<RegisterStudentData>(gql`
// mutation RegisterStudent() {
// registerStudent() {
// id
// }
// }
// `);
// const registerStudentMutation = mutation(registerStudentStore);
// async function registerStudent(): Promise<void> {
// await registerStudentMutation();
// if (!$registerStudentStore.error && $registerStudentStore.data) {
// location.reload();
// }
// }
2022-02-13 08:40:06 +00:00
</script>
{#if $meStore.error}
<p style="color: red;">{$meStore.error.message}</p>
{:else if $meStore.fetching}
<p>Laden...</p>
{:else}
2022-03-03 17:51:41 +00:00
<h1>Hey {$meStore.data.me.firstName}!</h1>
2022-02-13 08:40:06 +00:00
<hr />
{#if $meStore.data.me.role === UserRole.TEACHER}
2022-10-31 08:47:26 +00:00
<!-- {#if !$meStore.data.me.teacher}
2022-02-15 19:59:56 +00:00
<p>Registriere dich jetzt als Lehrer:</p>
<form on:submit|preventDefault={registerTeacher}>
<label for="maxStudents">Maximale Schüler:</label>
<br />
<input
type="number"
name="maxStudents"
min="0"
bind:value={$teacherRegisterFormMaxStudents.value}
/>
<br />
<label for="skif">SKIF:</label>
<br />
<input
type="checkbox"
name="skif"
bind:checked={$teacherRegisterFormSkif.value}
/>
<br /><br />
<button type="submit" disabled={!$teacheRegisterForm.valid}
>Registrieren</button
>
</form>
{#if $registerTeacherStore.error}
<p style="color: red;">{$registerTeacherStore.error.message}</p>
{:else if $registerTeacherStore.fetching}
<p>Laden...</p>
{:else if $registerTeacherStore.data}
<p>Registrierung erfolgreich!</p>
{/if}
2022-10-31 08:47:26 +00:00
{/if} -->
2022-02-13 08:40:06 +00:00
{:else if $meStore.data.me.role === UserRole.STUDENT}
2022-10-31 08:47:26 +00:00
<!-- {#if !$meStore.data.me.student}
2022-02-15 19:59:56 +00:00
<p>Registriere dich jetzt als Schüler:</p>
<form on:submit|preventDefault={registerStudent}>
<label for="skif">SKIF:</label>
<br />
<input
type="checkbox"
name="skif"
bind:checked={$registerStudentSkif.value}
/>
<br /><br />
<button type="submit" disabled={!$registerStudentForm.valid}
>Registrieren</button
>
</form>
{#if $registerStudentStore.error}
<p style="color: red;">{$registerStudentStore.error.message}</p>
{:else if $registerStudentStore.fetching}
<p>Laden...</p>
{:else if $registerStudentStore.data}
<p>Registrierung erfolgreich!</p>
{/if}
2022-10-31 08:47:26 +00:00
{/if} -->
2022-11-04 20:23:36 +00:00
<StudentHome />
2022-02-13 08:40:06 +00:00
{/if}
{/if}