Skip to content

feat: persist exam answers to localStorage across navigation - #171

Merged
kristofer merged 6 commits into
mainfrom
copilot/save-exam-page-state
Jul 21, 2026
Merged

kristofer merged 6 commits into
mainfrom
copilot/save-exam-page-state

Conversation

Copilot AI commented Jul 20, 2026 •

Copy link
Copy Markdown

Students lose all answer selections if they navigate away from an in-progress exam before submitting. Answers now persist in localStorage and are restored automatically on return.

Changes

State storage (exam.html)

  • getExamStateKey() — derives a localStorage key from the URL token (or attemptId as fallback); key format validated with [a-zA-Z0-9_-]+ pattern before use
  • persistExamData() — writes exam metadata (examData + attemptId) to ${key}_meta once on render; not called on every answer change
  • saveExamState() — writes only the lightweight {questionId → answerId} map to ${key}_answers on each radio change
  • restoreExamAnswers() — re-checks saved radio inputs after render; validates IDs are integers before querySelector interpolation; expires state older than EXAM_STATE_EXPIRY_MS (7 days)
  • clearExamState() — removes both _meta and _answers keys on successful submission

Integration points

  • renderExam() calls persistExamData() then restoreExamAnswers() after building the question list
  • onAnswerChange(skipSave?) accepts a flag to skip the write when called from restore (avoids redundant re-serialize)
  • init() falls back to activeExamStateKey (with regex validation + schema check) when no token or currentExam is present, enabling recovery for non-token-URL exam sessions
// On every answer change — only the small answers map is written
function saveExamState() {
    localStorage.setItem(`${key}_answers`, JSON.stringify({ answers, savedAt: Date.now() }));
}

// On render — full exam data written once
function persistExamData() {
    localStorage.setItem(`${key}_meta`, JSON.stringify({ examData, attemptId, savedAt: Date.now() }));
    localStorage.setItem('activeExamStateKey', key);
}

Copilot AI linked an issue Jul 20, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Add state saving for exam taking page feat: persist exam answers to localStorage across navigation Jul 20, 2026
Copilot AI requested a review from kristofer July 20, 2026 20:40
@kristofer
kristofer marked this pull request as ready for review July 21, 2026 15:56
@kristofer
kristofer merged commit c6f7041 into main Jul 21, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exam taking page needs state saving.

2 participants