Skip to content

Commit 8d499bf

Browse files
committed
feat: add toggle switch for disabling error feedback and persist user settings
1 parent e4de2a8 commit 8d499bf

File tree

4 files changed

+126
-5
lines changed

4 files changed

+126
-5
lines changed

src/app.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ const state = {
99
currentLessonIndex: 0,
1010
modules: [],
1111
userProgress: {}, // Format: { moduleId: { completed: [0, 2, 3], current: 4 } }
12-
userCodeBeforeValidation: "" // Track user code state before validation
12+
userCodeBeforeValidation: "", // Track user code state before validation
13+
userSettings: {
14+
disableFeedbackErrors: false
15+
}
1316
};
1417

1518
// DOM elements
@@ -36,7 +39,8 @@ const elements = {
3639
lessonContainer: document.querySelector(".lesson-container"),
3740
editorContent: document.querySelector(".editor-content"),
3841
codeEditor: document.querySelector(".code-editor"),
39-
validationIndicators: document.querySelector(".validation-indicators-container")
42+
validationIndicators: document.querySelector(".validation-indicators-container"),
43+
disableFeedbackToggle: document.getElementById("disable-feedback-toggle")
4044
};
4145

4246
// Initialize the lesson engine
@@ -55,6 +59,32 @@ function saveUserProgress() {
5559
localStorage.setItem("codeCrispies.Progress", JSON.stringify(state.userProgress));
5660
}
5761

62+
function loadUserSettings() {
63+
const savedSettings = localStorage.getItem("codeCrispies.settings");
64+
if (savedSettings) {
65+
try {
66+
const settings = JSON.parse(savedSettings);
67+
state.userSettings = { ...state.userSettings, ...settings };
68+
69+
// Apply saved settings to UI
70+
elements.disableFeedbackToggle.checked = !state.userSettings.disableFeedbackErrors;
71+
} catch (e) {
72+
console.error("Error loading user settings:", e);
73+
}
74+
}
75+
}
76+
77+
function saveUserSettings() {
78+
localStorage.setItem("codeCrispies.settings", JSON.stringify(state.userSettings));
79+
}
80+
81+
function initFeedbackToggle() {
82+
elements.disableFeedbackToggle.addEventListener("change", (e) => {
83+
state.userSettings.disableFeedbackErrors = !e.target.checked;
84+
saveUserSettings();
85+
});
86+
}
87+
5888
// Initialize the module list
5989
async function initializeModules() {
6090
try {
@@ -102,9 +132,9 @@ function updateModuleSelectorButtonProgress() {
102132
position: absolute;
103133
bottom: 0;
104134
left: 0;
105-
height: 3px;
135+
height: 2px;
106136
width: ${percentComplete}%;
107-
background-color: var(--success-color);
137+
background-color: var(--primary-color);
108138
border-radius: 0 3px 3px 0;
109139
`;
110140

@@ -545,7 +575,9 @@ function handleTabKey(e) {
545575
// Initialize the application
546576
function init() {
547577
loadUserProgress();
548-
initializeModules();
578+
loadUserSettings();
579+
initializeModules().catch(console.error);
580+
initFeedbackToggle();
549581

550582
// Event listeners
551583
elements.prevBtn.addEventListener("click", prevLesson);
@@ -562,6 +594,12 @@ function init() {
562594
elements.codeInput.focus();
563595
});
564596

597+
// Load user settings
598+
elements.disableFeedbackToggle.addEventListener("change", (e) => {
599+
state.userSettings.disableFeedbackErrors = !e.target.checked;
600+
saveUserSettings();
601+
});
602+
565603
// Add tab key handler for the code input
566604
elements.codeInput.addEventListener("keydown", handleTabKey);
567605

src/helpers/renderer.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ export function showFeedback(isSuccess, message) {
9494
// Clear any existing feedback
9595
clearFeedback();
9696

97+
// Check if error feedback is disabled in user settings
98+
if (!isSuccess) {
99+
const disableFeedbackErrors = !document.getElementById("disable-feedback-toggle").checked;
100+
if (disableFeedbackErrors) {
101+
// Skip showing error feedback if disabled
102+
return;
103+
}
104+
}
105+
97106
// Create feedback element
98107
feedbackElement = document.createElement("div");
99108
feedbackElement.classList.add(isSuccess ? "feedback-success" : "feedback-error");

src/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ <h1>CODE<br /><span>CRISPIES</span></h1>
1616
</div>
1717
<nav class="main-nav">
1818
<ul>
19+
<li class="toggle-container">
20+
<label class="toggle-switch" title="Toggle error feedback">
21+
<input type="checkbox" id="disable-feedback-toggle" checked />
22+
<span class="toggle-slider"></span>
23+
<span class="toggle-label">Show Hints</span>
24+
</label>
25+
</li>
1926
<li><button id="module-selector-btn" class="btn">Progress</button></li>
2027
<li><button id="reset-btn" class="btn">Reset Progress</button></li>
2128
<li><button id="help-btn" class="btn">Help</button></li>

src/main.css

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,3 +641,70 @@ code {
641641
background-color: var(--success-color-dark);
642642
border-color: var(--success-color-dark);
643643
}
644+
645+
/**
646+
* CSS to style the toggle switch in the header
647+
* Add this to your main.css file
648+
*/
649+
650+
/* Toggle Switch Styles */
651+
.toggle-container {
652+
display: flex;
653+
align-items: center;
654+
margin-left: 10px;
655+
}
656+
657+
.toggle-switch {
658+
position: relative;
659+
display: inline-block;
660+
height: 24px;
661+
display: flex;
662+
align-items: center;
663+
cursor: pointer;
664+
}
665+
666+
.toggle-switch input {
667+
opacity: 0;
668+
width: 0;
669+
height: 0;
670+
}
671+
672+
.toggle-slider {
673+
position: relative;
674+
display: inline-block;
675+
width: 40px;
676+
height: 20px;
677+
background-color: #ccc;
678+
border-radius: 34px;
679+
transition: 0.4s;
680+
margin-right: 8px;
681+
}
682+
683+
.toggle-slider:before {
684+
position: absolute;
685+
content: "";
686+
height: 16px;
687+
width: 16px;
688+
left: 2px;
689+
bottom: 2px;
690+
background-color: white;
691+
border-radius: 50%;
692+
transition: 0.4s;
693+
}
694+
695+
input:checked + .toggle-slider {
696+
background-color: var(--primary-color);
697+
}
698+
699+
input:focus + .toggle-slider {
700+
box-shadow: 0 0 1px var(--primary-color);
701+
}
702+
703+
input:checked + .toggle-slider:before {
704+
transform: translateX(20px);
705+
}
706+
707+
.toggle-label {
708+
font-size: 14px;
709+
color: var(--text-color);
710+
}

0 commit comments

Comments
 (0)