Web Dev Solutions

Catalin Mititiuc

From 85f5abb4e260a37bd4be8d2f566b51b8b3f4adad Mon Sep 17 00:00:00 2001 From: Catalin Mititiuc Date: Thu, 30 May 2024 18:06:46 -0700 Subject: Fix storing scenario in local storage --- src/modules/map_select_dialog.js | 13 ++++++------- src/modules/scenarios.js | 15 ++++++++++++--- 2 files changed, 18 insertions(+), 10 deletions(-) (limited to 'src/modules') diff --git a/src/modules/map_select_dialog.js b/src/modules/map_select_dialog.js index 24215f6..359477d 100644 --- a/src/modules/map_select_dialog.js +++ b/src/modules/map_select_dialog.js @@ -6,17 +6,17 @@ export function init() { selectEl = mapDialog.querySelector('select'), confirmBtn = mapDialog.querySelector('#confirm-btn'); - Object.keys(scenarios).forEach(scenario => { + Object.keys(scenarios).reverse().forEach(scenario => { const option = document.createElement('option'); option.setAttribute('value', scenario); - option.textContent = scenarios[scenario]; - selectEl.appendChild(option); + option.textContent = scenarios[scenario].title; + selectEl.prepend(option); }); return { selectCurrentOptionOnPageLoad() { - mapDialog.querySelectorAll('option').forEach(option => - option.value === localStorage.getItem('map') && (option.selected = true) + mapDialog.querySelectorAll('option').forEach((option, index) => + option.selected = option.value === localStorage.getItem('map') || index === 0 ); return this; @@ -43,8 +43,7 @@ export function init() { e.preventDefault(); localStorage.removeItem('pan-zoom'); localStorage.setItem('map', selectEl.value); - // loadFn(`assets/images/${selectEl.value}.svg`); - loadFn(selectEl.value); + loadFn(scenarios[selectEl.value]?.hashed || `assets/images/${selectEl.value}.svg`); mapDialog.close(); }); } diff --git a/src/modules/scenarios.js b/src/modules/scenarios.js index afe375f..2a4af26 100644 --- a/src/modules/scenarios.js +++ b/src/modules/scenarios.js @@ -3,7 +3,16 @@ import dragonHunting from './assets/images/scenario-dragon_hunting.svg'; import raceAgainstTime from './assets/images/scenario-race_against_time.svg'; export const scenarios = { - [sideShow]: 'BattleTroops Scenario 1: Side Show', - [dragonHunting]: 'BattleTroops Scenario 2: Dragon Hunting', - [raceAgainstTime]: 'BattleTroops Scenario 3: Race Against Time', + 'scenario-side_show': { + hashed: sideShow, + title: 'BattleTroops Scenario 1: Side Show', + }, + 'scenario-dragon_hunting': { + hashed: dragonHunting, + title: 'BattleTroops Scenario 2: Dragon Hunting', + }, + 'scenario-race_against_time': { + hashed: raceAgainstTime, + title: 'BattleTroops Scenario 3: Race Against Time', + }, } -- cgit v1.2.3