Dummy Data & LocalStorage in layout.ts Load function
This commit is contained in:
parent
c0b4d64e49
commit
5632ab72d2
1 changed files with 76 additions and 0 deletions
76
src/routes/graphs/+layout.ts
Normal file
76
src/routes/graphs/+layout.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue