This repository has been archived on 2024-08-06. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
chellaris-sign-up-site/src/routes/admin/+page.ts
2023-09-07 22:04:51 +02:00

60 lines
No EOL
1.7 KiB
TypeScript

import { apiBaseUrl } from "$lib/components/consts";
import AdminSelectedEmpireStore from "$lib/stores/admin-page/EmpireStore";
import AdminSelectedGameStore from "$lib/stores/admin-page/GameStore";
import AuthTokenStore from "$lib/stores/AuthTokenStore";
import type { ChellarisGameInfo } from "$lib/types/chellaris";
import { redirect } from "@sveltejs/kit";
import AdminSelectedGroupStore from '../../lib/stores/admin-page/GroupStore';
export async function load ({ fetch }) {
let authToken = "";
AuthTokenStore.subscribe(token => {
authToken = token;
});
const auth = await (await fetch(apiBaseUrl + "/v3/auth",{
headers: {
'Content-Type': 'application/json',
'x-api-key': authToken
}
})).json();
if (!auth.admin && !auth.moderator) {
throw redirect(303, '/401');
}
const gameList: { [key: number]: ChellarisGameInfo } = await (await fetch(apiBaseUrl + "/v3/games")).json();
let store: string | null;
if (typeof localStorage !== 'undefined') {
// Game Selection
store = localStorage.getItem('adminGameSelection');
if (typeof store === 'string') {
AdminSelectedGameStore.set(JSON.parse(store));
}
// Group Selection
store = localStorage.getItem('adminGroupSelection');
if (typeof store === 'string' && store != "\"\"") {
AdminSelectedGroupStore.set(JSON.parse(store));
}
else if (typeof store === 'string') {
AdminSelectedGroupStore.set({});
}
// Empire Selection
store = localStorage.getItem('adminEmpireSelection');
if (typeof store === 'string' && store != "\"\"") {
AdminSelectedEmpireStore.set(JSON.parse(store));
}
else if (typeof store === 'string') {
AdminSelectedEmpireStore.set({});
}
}
return { games: gameList };
}