|
6 | 6 | import { shopItems, irlShopItems, HCB_BASE_URL } from './constants.js'; |
7 | 7 | import { getPurchasesByEid } from './eidStats.js'; |
8 | 8 |
|
| 9 | +// Track current shop mode globally |
| 10 | +let currentShopMode = 'regular'; |
| 11 | + |
9 | 12 | /** |
10 | 13 | * Initialize shop functionality |
11 | 14 | */ |
12 | 15 | export function initShopFunctionality() { |
| 16 | + // Check if we're in IRL mode from URL |
| 17 | + const searchParams = new URLSearchParams(window.location.search); |
| 18 | + currentShopMode = searchParams.get('irl') === '1' ? 'irl' : 'regular'; |
| 19 | + |
13 | 20 | // Generate the shop table |
14 | | - generateShopTable(); |
| 21 | + generateShopTable(currentShopMode); |
15 | 22 |
|
16 | 23 | // Add event listener to checkout button |
17 | 24 | const checkoutBtn = document.getElementById('checkoutBtn'); |
@@ -58,21 +65,74 @@ export function processShopItems(itemsArray = shopItems) { |
58 | 65 | }); |
59 | 66 | } |
60 | 67 |
|
| 68 | +/** |
| 69 | + * Switch between shop types (IRL vs Regular) without page reload |
| 70 | + * @param {string} mode - The shop mode to switch to ('irl' or 'regular') |
| 71 | + */ |
| 72 | +export function switchShopType(mode) { |
| 73 | + // Show a brief loading message |
| 74 | + const container = document.getElementById('shopTableContainer'); |
| 75 | + if (container) { |
| 76 | + const loadingMsg = document.createElement('div'); |
| 77 | + loadingMsg.textContent = 'Loading...'; |
| 78 | + loadingMsg.style.padding = '10px'; |
| 79 | + loadingMsg.style.textAlign = 'center'; |
| 80 | + container.innerHTML = ''; |
| 81 | + container.appendChild(loadingMsg); |
| 82 | + } |
| 83 | + |
| 84 | + // Small delay to allow UI update |
| 85 | + setTimeout(() => { |
| 86 | + // Update current shop mode |
| 87 | + currentShopMode = mode === 'irl' ? 'irl' : 'regular'; |
| 88 | + |
| 89 | + // Update URL without reloading the page |
| 90 | + const url = new URL(window.location); |
| 91 | + if (currentShopMode === 'irl') { |
| 92 | + url.searchParams.set('irl', '1'); |
| 93 | + } else { |
| 94 | + url.searchParams.set('irl', '0'); |
| 95 | + } |
| 96 | + window.history.pushState({}, '', url); |
| 97 | + |
| 98 | + // Clear existing table |
| 99 | + if (container) { |
| 100 | + container.innerHTML = ''; |
| 101 | + } |
| 102 | + |
| 103 | + // Regenerate the shop table with the new mode |
| 104 | + generateShopTable(currentShopMode); |
| 105 | + |
| 106 | + // Recalculate total |
| 107 | + updateTotal(); |
| 108 | + }, 100); // Short delay for better user experience |
| 109 | +} |
| 110 | + |
61 | 111 | /** |
62 | 112 | * Generate shop table from items array |
| 113 | + * @param {string} mode - The shop mode ('irl' or 'regular') |
63 | 114 | */ |
64 | | -export function generateShopTable() { |
| 115 | +export function generateShopTable(mode = null) { |
65 | 116 | const container = document.getElementById('shopTableContainer'); |
66 | 117 | if (!container) return; |
67 | 118 |
|
68 | | - // Check if we're in IRL mode |
69 | | - const isIrlMode = new URLSearchParams(window.location.search).has('irl'); |
| 119 | + // If mode is not provided, use the current shop mode or determine from URL |
| 120 | + if (mode === null) { |
| 121 | + const searchParams = new URLSearchParams(window.location.search); |
| 122 | + mode = searchParams.get('irl') === '1' ? 'irl' : 'regular'; |
| 123 | + currentShopMode = mode; |
| 124 | + } |
| 125 | + |
| 126 | + const isIrlMode = mode === 'irl'; |
70 | 127 |
|
71 | 128 | // Get the items to display based on mode |
72 | | - let itemsToProcess = [...shopItems]; // Make a copy of the array |
| 129 | + let itemsToProcess = []; |
73 | 130 | if (isIrlMode) { |
74 | | - // In IRL mode, show IRL items first, then regular items |
75 | | - itemsToProcess = [...irlShopItems, ...shopItems]; |
| 131 | + // In IRL mode, show ONLY IRL items |
| 132 | + itemsToProcess = [...irlShopItems]; |
| 133 | + } else { |
| 134 | + // In regular mode, show ONLY regular shop items |
| 135 | + itemsToProcess = [...shopItems]; |
76 | 136 | } |
77 | 137 |
|
78 | 138 | // Process items to propagate values |
@@ -179,18 +239,32 @@ export function generateShopTable() { |
179 | 239 | table.appendChild(tbody); |
180 | 240 | container.appendChild(table); |
181 | 241 |
|
182 | | - // Add "Snack Shop" link if we're NOT already in IRL mode |
183 | | - if (!isIrlMode) { |
184 | | - const snackShopLink = document.createElement('a'); |
185 | | - snackShopLink.href = window.location.pathname + '?irl=1'; |
186 | | - snackShopLink.textContent = 'Snack Shop'; |
187 | | - snackShopLink.style.color = '#0066cc'; |
188 | | - snackShopLink.style.textDecoration = 'underline'; |
189 | | - snackShopLink.style.display = 'inline-block'; |
190 | | - snackShopLink.style.marginTop = '10px'; |
191 | | - snackShopLink.style.fontSize = '0.9em'; |
192 | | - container.appendChild(snackShopLink); |
| 242 | + // Add shop switch link |
| 243 | + const shopSwitchLink = document.createElement('a'); |
| 244 | + shopSwitchLink.href = '#'; // Use # to prevent page navigation |
| 245 | + |
| 246 | + if (isIrlMode) { |
| 247 | + shopSwitchLink.textContent = 'Switch to Online Shop'; |
| 248 | + shopSwitchLink.setAttribute('data-target-mode', 'regular'); |
| 249 | + } else { |
| 250 | + shopSwitchLink.textContent = 'Switch to Snack Shop'; |
| 251 | + shopSwitchLink.setAttribute('data-target-mode', 'irl'); |
193 | 252 | } |
| 253 | + |
| 254 | + // Add click event listener to switch shop type without page reload |
| 255 | + shopSwitchLink.addEventListener('click', function(e) { |
| 256 | + e.preventDefault(); // Prevent the default link behavior |
| 257 | + const targetMode = this.getAttribute('data-target-mode'); |
| 258 | + switchShopType(targetMode); |
| 259 | + }); |
| 260 | + |
| 261 | + shopSwitchLink.style.color = '#0066cc'; |
| 262 | + shopSwitchLink.style.textDecoration = 'underline'; |
| 263 | + shopSwitchLink.style.display = 'inline-block'; |
| 264 | + shopSwitchLink.style.marginTop = '10px'; |
| 265 | + shopSwitchLink.style.fontSize = '0.9em'; |
| 266 | + shopSwitchLink.style.cursor = 'pointer'; |
| 267 | + container.appendChild(shopSwitchLink); |
194 | 268 | } |
195 | 269 |
|
196 | 270 | /** |
|
0 commit comments