From 5632ab72d2919b1f865696912c498122b8cff80d Mon Sep 17 00:00:00 2001 From: Neshura Date: Sun, 13 Aug 2023 03:24:50 +0200 Subject: [PATCH] Dummy Data & LocalStorage in layout.ts Load function --- src/routes/graphs/+layout.ts | 76 ++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/routes/graphs/+layout.ts diff --git a/src/routes/graphs/+layout.ts b/src/routes/graphs/+layout.ts new file mode 100644 index 0000000..e16c8be --- /dev/null +++ b/src/routes/graphs/+layout.ts @@ -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(), + ethics: [], + portraits: [] + } + + dummyData.games.set("1", { + name: "Game 16", + groups: new Map(), + empires: [], + }); + + dummyData.games.set("2", { + name: "Game 17", + groups: new Map(), + 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>(JSON.parse(store)); + const tmp = gameGroupSelectionMap.get(selectedGame); + let selectedGameGroups: Array = []; + 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); + } + } +} \ No newline at end of file