Dummy Data & LocalStorage in layout.ts Load function

This commit is contained in:
Neshura 2023-08-13 03:24:50 +02:00
parent c0b4d64e49
commit 5632ab72d2
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C

View file

@ -0,0 +1,76 @@
import type { PageLoad } from "../$types";
import ChellarisDataStore from '$lib/stores/ChellarisData';
import SelectedGameStore from '$lib/stores/GameFilter';
import type { ChellarisInfo, ChellarisGame, ChellarisGameGroup } from '../../lib/types/chellaris';
import SelectedGameGroupsStore from "$lib/stores/GameGroupFilter";
export const load: PageLoad = async () => {
// Chellaris Data Code
// Game Dummy Data
const dummyData: ChellarisInfo = {
games: new Map<string, ChellarisGame>(),
ethics: [],
portraits: []
}
dummyData.games.set("1", {
name: "Game 16",
groups: new Map<string, ChellarisGameGroup>(),
empires: [],
});
dummyData.games.set("2", {
name: "Game 17",
groups: new Map<string, ChellarisGameGroup>(),
empires: [],
});
// Group Dummy Data
dummyData.games.get("1")?.groups.set("1", {name: "A"});
dummyData.games.get("1")?.groups.set("2", {name: "B"});
dummyData.games.get("1")?.groups.set("3", {name: "None"});
dummyData.games.get("2")?.groups.set("4", {name: "A"});
dummyData.games.get("2")?.groups.set("5", {name: "B"});
dummyData.games.get("2")?.groups.set("6", {name: "None2"});
//
ChellarisDataStore.set(dummyData)
// Local Storage Code
let store: string | null;
if (typeof localStorage !== 'undefined') {
// Game Selection
store = localStorage.getItem('gameSelection');
let selectedGame = "";
if (typeof store == 'string') {
selectedGame = JSON.parse(store);
SelectedGameStore.set(selectedGame);
}
// Game Groups Selection
store = localStorage.getItem('gameGroupSelection');
if (typeof store == 'string') {
const gameGroupSelectionMap = new Map<string, Array<string>>(JSON.parse(store));
const tmp = gameGroupSelectionMap.get(selectedGame);
let selectedGameGroups: Array<string> = [];
if (typeof tmp !== 'undefined') {
selectedGameGroups = [...tmp.values()];
} else {
const tmpGameData = dummyData.games.get(selectedGame);
// If this fails an empty array is precisely what we want
if (typeof tmpGameData !== "undefined") {
// Default to all available groups
selectedGameGroups = [...tmpGameData.groups.keys()];
// Set Local Storage to default Values if not previously defined
localStorage.setItem('gameGroupSelection', JSON.stringify(Array.from(selectedGameGroups.entries())));
}
}
gameGroupSelectionMap.set(selectedGame, selectedGameGroups);
SelectedGameGroupsStore.set(gameGroupSelectionMap);
}
}
}