document.addEventListener('DOMContentLoaded', () => { loadJobs(); loadSavedJobs(); setupEventListeners(); }); // Feature 1 & 3: Global State & Event Listeners function setupEventListeners() { const searchInput = document.getElementById('jobSearch'); const categorySelect = document.getElementById('categoryFilter'); searchInput.addEventListener('input', debounce(() => { loadJobs(searchInput.value, categorySelect.value); }, 500)); categorySelect.addEventListener('change', () => { loadJobs(searchInput.value, categorySelect.value); }); // Listen for save events from job cards document.addEventListener('job-saved', (e) => { saveJob(e.detail); }); // Listen for remove events document.addEventListener('job-removed', (e) => { removeJob(e.detail.id); }); } // Feature 3: Save Job Logic function saveJob(jobData) { let saved = JSON.parse(localStorage.getItem('warriorSavedJobs')) || []; // Avoid duplicates if (saved.some(j => j.url === jobData.url)) { alert('Target already in Hitlist.'); return; } jobData.savedAt = new Date().toISOString(); saved.push(jobData); localStorage.setItem('warriorSavedJobs', JSON.stringify(saved)); loadSavedJobs(); // Refresh UI } function removeJob(url) { let saved = JSON.parse(localStorage.getItem('warriorSavedJobs')) || []; saved = saved.filter(j => j.url !== url); localStorage.setItem('warriorSavedJobs', JSON.stringify(saved)); loadSavedJobs(); // Refresh UI } function loadSavedJobs() { const container = document.getElementById('saved-jobs-list'); const saved = JSON.parse(localStorage.getItem('warriorSavedJobs')) || []; if (saved.length === 0) { container.innerHTML = `
No targets locked yet. Click the heart on job cards to save them.
${job.company}
Scanning market...
No targets match your filters.
'; return; } filtered.forEach(job => { // Check if already saved const saved = JSON.parse(localStorage.getItem('warriorSavedJobs')) || []; const isSaved = saved.some(j => j.url === job.url); const card = document.createElement('job-card'); card.setAttribute('title', job.title); card.setAttribute('company', job.company_name); card.setAttribute('salary', job.salary || 'Competitive'); card.setAttribute('type', job.job_type); card.setAttribute('url', job.url); card.setAttribute('category', job.category); card.setAttribute('saved', isSaved); // Pass state grid.appendChild(card); }); } } catch (error) { console.error('Error:', error); grid.innerHTML = `Connection lost.
No targets found at this moment.
'; } } catch (error) { console.error('Error fetching jobs:', error); grid.innerHTML = `Connection lost to the network.