diff --git a/frontend/src/lib/@api/admin.ts b/frontend/src/lib/@api/admin.ts index 6c9d64c..7972315 100644 --- a/frontend/src/lib/@api/admin.ts +++ b/frontend/src/lib/@api/admin.ts @@ -8,6 +8,22 @@ import type { import axios from 'axios'; import { API_URL, errorHandler, type Fetch } from '.'; +export const apiWhoami = async (fetchSsr?: Fetch): Promise => { + const apiFetch = fetchSsr || fetch; + try { + const res = await apiFetch(API_URL + '/admin/whoami', { + method: 'GET', + credentials: 'include' + }); + if (res.status != 200) { + throw Error(await res.text()); + } + return await res.json(); + } catch (e) { + throw errorHandler(e, 'Failed to fetch whoami'); + } +}; + // Login as admin /admin/login export const apiLogin = async (data: AdminLogin): Promise => { try { diff --git a/frontend/src/lib/components/Modal.svelte b/frontend/src/lib/components/Modal.svelte index 4148a0c..3c56481 100644 --- a/frontend/src/lib/components/Modal.svelte +++ b/frontend/src/lib/components/Modal.svelte @@ -33,7 +33,7 @@ } .modal { - @apply absolute; + @apply fixed; @apply p-4; @apply rounded-xl; @apply transform: diff --git a/frontend/src/lib/components/admin/table/Table.svelte b/frontend/src/lib/components/admin/table/Table.svelte index 97755b7..1916259 100644 --- a/frontend/src/lib/components/admin/table/Table.svelte +++ b/frontend/src/lib/components/admin/table/Table.svelte @@ -22,7 +22,7 @@ {#each candidates as candidate} - - + {:else} \ No newline at end of file diff --git a/frontend/src/routes/(admin)/admin/(authenticated)/+layout.server.ts b/frontend/src/routes/(admin)/admin/(authenticated)/+layout.server.ts index af8cb9a..ba6c51b 100644 --- a/frontend/src/routes/(admin)/admin/(authenticated)/+layout.server.ts +++ b/frontend/src/routes/(admin)/admin/(authenticated)/+layout.server.ts @@ -1,10 +1,19 @@ import type { LayoutServerLoad } from './$types'; import { redirect } from '@sveltejs/kit'; +import { apiWhoami } from '$lib/@api/admin'; -export const load: LayoutServerLoad = ({ cookies }) => { +export const load: LayoutServerLoad = async ({ cookies, fetch }) => { const isAuthenticated = cookies.get('id'); - if (!isAuthenticated) { - throw redirect(302, '/admin/auth/login'); + + if (isAuthenticated) { + const whoami = await apiWhoami(fetch).catch(() => { + throw redirect(302, '/admin/auth/logout'); + }); + return { + whoami: whoami + }; + } else { + throw redirect(302, '/admin/auth/logout'); } }; diff --git a/frontend/src/routes/(admin)/admin/(authenticated)/dashboard/+page.svelte b/frontend/src/routes/(admin)/admin/(authenticated)/dashboard/+page.svelte index fcbaf01..9738767 100644 --- a/frontend/src/routes/(admin)/admin/(authenticated)/dashboard/+page.svelte +++ b/frontend/src/routes/(admin)/admin/(authenticated)/dashboard/+page.svelte @@ -9,44 +9,52 @@ import Table from '$lib/components/admin/table/Table.svelte'; import bacgkround from '$lib/assets/background.jpg'; + import Logout from '$lib/components/icons/Logout.svelte'; + import { goto } from '$app/navigation'; export let data: PageServerData; let candidates: Array = data.preview; - const getCandidates = async (field?: string) => { + const getCandidates = async () => { try { - candidates = await apiListCandidates( - undefined, - field ?? activeFilter !== 'Vše' ? activeFilter : '' - ); + candidates = await apiListCandidates(undefined, activeFilter.filter); } catch { console.log('error'); } }; - type Filter = 'Vše' | 'KBB' | 'IT' | 'GYM'; + type Class = 'Vše' | 'KBB' | 'IT' | 'GYM'; - let filters: Array = ['Vše', 'KBB', 'IT', 'GYM']; + type Filter = { + class: Class; + filter: string | undefined; + }; + + let filters: Array = [ + { + class: 'Vše', + filter: undefined + }, + { + class: 'KBB', + filter: 'KB' + }, + { + class: 'IT', + filter: 'IT' + }, + { + class: 'GYM', + filter: 'G' + } + ]; let activeFilter: Filter = filters[0]; - const changeFilter = (filter: Filter) => { + const changeFilter = async (filter: Filter) => { activeFilter = filter; - switch (activeFilter) { - case 'Vše': - getCandidates(); - break; - case 'KBB': - getCandidates('KB'); - break; - case 'IT': - getCandidates('IT'); - break; - case 'GYM': - getCandidates('G'); - break; - } + await getCandidates(); }; let scrollTop = 0; @@ -88,6 +96,10 @@ console.log(e); } }; + + const logout = async () => { + goto('/admin/auth/logout'); + }; {#if createCandidateModal} @@ -98,7 +110,7 @@ {/if}
-
+
Background
@@ -106,12 +118,17 @@ {#each filters as filter}
- +
{/each}
-

Uchazeči

+
+

Uchazeči

+ +