Skip to content

Commit

Permalink
Added dark mode persistence, escaped HTML in form submission, and upd…
Browse files Browse the repository at this point in the history
…ated theme toggle logic
  • Loading branch information
Cypher-PB committed Oct 12, 2024
1 parent 0bebf8a commit 23e1cc3
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ class TechMaterialsApp {

async init() {
try {
const storedDarkMode = localStorage.getItem('darkMode');
if (storedDarkMode === 'true') {
this.isDarkMode = true;
this.updateTheme();
}
await this.loadMaterials();
this.setupEventListeners();
this.renderMaterials();
this.updateTheme();
this.updateTheme();
} catch (error) {
this.showError('Failed to initialize the application');
}
Expand Down Expand Up @@ -43,15 +48,15 @@ class TechMaterialsApp {

// Theme toggle button
const toggleButton = document.getElementById('toggleTheme');
toggleButton.addEventListener('click', () => {
this.toggleTheme();
});
toggleButton.addEventListener('click', () => {
this.toggleTheme();
});
}

handleFormSubmission() {
const contributor = document.getElementById('contributor').value;
const resourceName = document.getElementById('resourceName').value;
const link = document.getElementById('link').value;
const contributor = this.escapeHtml(document.getElementById('contributor').value);
const resourceName = this.escapeHtml(document.getElementById('resourceName').value);
const link = this.escapeHtml(document.getElementById('link').value);
const tags = document.getElementById('tags').value;

const newMaterial = {
Expand Down Expand Up @@ -117,6 +122,11 @@ class TechMaterialsApp {
.replace(/'/g, "'");
}

//in cases where escapeHtml is used for href attributes
escapeHtmlAttribute(unsafe){
return unsafe.replace(/"/g, """).replace(/'/g, "'");
} // now links can be used like this::: <a href="${this.escapeHtmlAttribute(material.link)}" target="_blank">${this.escapeHtml(material.link)}</a>

showError(message) {
console.error(message);
// You could implement a more user-friendly error display here
Expand All @@ -125,26 +135,28 @@ class TechMaterialsApp {
toggleTheme() {
this.isDarkMode = !this.isDarkMode;
this.updateTheme();
localStorage.setItem('darkMode', this.isDarkMode);
}

updateTheme() {
const body = document.body;
const addMaterialSection = document.querySelector('.add-material');
const materialCards = document.querySelectorAll('.material-card');
const header = document.querySelector('h1'); // Select the header
const labels = document.querySelectorAll('label'); // Select all labelsed
const header = document.querySelector('h1');
const labels = document.querySelectorAll('label');

if (this.isDarkMode) {
body.classList.add('dark-mode');
addMaterialSection.classList.add('dark-mode');
materialCards.forEach(card => card.classList.add('dark-mode'));
header.classList.add('dark-mode'); // Add dark-mode class to header
labels.forEach(label => label.classList.add('dark-mode')); // Add dark-mode class to all labels
header.classList.add(' dark-mode');
labels.forEach(label => label.classList.add('dark-mode'));
} else {
body.classList.remove('dark-mode');
addMaterialSection.classList.remove('dark-mode');
materialCards.forEach(card => card.classList.remove('dark-mode'));
header.classList.remove('dark-mode'); // Remove dark-mode class from header
labels.forEach(label => label.classList.remove('dark-mode')); // Remove dark-mode class from all labels
header.classList.remove('dark-mode');
labels.forEach(label => label.classList.remove('dark-mode'));
}
}
}
Expand Down

0 comments on commit 23e1cc3

Please sign in to comment.