2023-08-30 04:17:16 +02:00
import { apiBaseUrl } from "$lib/components/consts" ;
import AdminSelectedEmpireStore from "$lib/stores/admin-page/EmpireStore" ;
import AdminSelectedGameStore from "$lib/stores/admin-page/GameStore" ;
2023-09-07 22:04:51 +02:00
import AuthTokenStore from "$lib/stores/AuthTokenStore" ;
2023-08-30 04:17:16 +02:00
import type { ChellarisGameInfo } from "$lib/types/chellaris" ;
2023-09-07 22:04:51 +02:00
import { redirect } from "@sveltejs/kit" ;
2023-08-31 01:09:05 +02:00
import AdminSelectedGroupStore from '../../lib/stores/admin-page/GroupStore' ;
2023-08-30 04:17:16 +02:00
export async function load ( { fetch } ) {
2023-09-07 22:04:51 +02:00
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' ) ;
}
2023-08-31 01:09:05 +02:00
const gameList : { [ key : number ] : ChellarisGameInfo } = await ( await fetch ( apiBaseUrl + "/v3/games" ) ) . json ( ) ;
2023-08-30 04:17:16 +02:00
let store : string | null ;
if ( typeof localStorage !== 'undefined' ) {
// Game Selection
store = localStorage . getItem ( 'adminGameSelection' ) ;
if ( typeof store === 'string' ) {
AdminSelectedGameStore . set ( JSON . parse ( store ) ) ;
}
2023-08-31 01:09:05 +02:00
// Group Selection
store = localStorage . getItem ( 'adminGroupSelection' ) ;
if ( typeof store === 'string' && store != "\"\"" ) {
AdminSelectedGroupStore . set ( JSON . parse ( store ) ) ;
}
else if ( typeof store === 'string' ) {
AdminSelectedGroupStore . set ( { } ) ;
}
2023-08-30 04:17:16 +02:00
// 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 } ;
}