generated from dustin-jw/eleventy-starter-lite
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: randomize question order between builds
- set up data needed to intercept links - filter out irrelevant questions on category click - randomize and store the order of questions - clear question data on session end or landing on the home page - stub out script for updating pagination links - set data attr on pagination links for easy selection - update pagination links on page load - provide span to hook into for replacing the question number - update the displayed question number
- Loading branch information
Showing
12 changed files
with
214 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
permalink: /questions.json | ||
eleventyExcludeFromCollections: true | ||
--- | ||
|
||
<%- JSON.stringify(questionData.questions) -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const getQuestions = async () => { | ||
const response = await fetch('/questions.json'); | ||
const questions = await response.json(); | ||
|
||
return questions; | ||
}; | ||
|
||
const getRelevantQuestions = (questions, category, isMultipleChoice) => { | ||
return questions.filter((question) => { | ||
if (!isMultipleChoice && question['Multiple Choice Only']) { | ||
return false; | ||
} | ||
|
||
if (category !== 'All' && !question.Tags.some((tag) => tag === category)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}); | ||
}; | ||
|
||
(async () => { | ||
sessionStorage.removeItem('questions'); | ||
sessionStorage.removeItem('questionStatus'); | ||
const questions = await getQuestions(); | ||
const links = document.querySelectorAll('.cmp-categories__link'); | ||
|
||
links.forEach((link) => { | ||
link.addEventListener('click', (event) => { | ||
event.preventDefault(); | ||
|
||
const relevantQuestions = getRelevantQuestions( | ||
questions, | ||
link.dataset.category, | ||
link.dataset.type === 'Multiple Choice' | ||
); | ||
|
||
const questionOrder = Array.from( | ||
{ length: relevantQuestions.length }, | ||
(_item, index) => `${link.dataset.baseLink}${index + 1}/` | ||
).sort(() => (Math.random() > 0.5 ? 1 : -1)); | ||
|
||
sessionStorage.setItem('questions', JSON.stringify(questionOrder)); | ||
|
||
sessionStorage.setItem('currentQuestionIndex', '0'); | ||
|
||
window.location.href = questionOrder[0]; | ||
}); | ||
}); | ||
})(); |
Oops, something went wrong.