From 81f1ef3db3f0fe9ef3e93b6619541d183ecd868c Mon Sep 17 00:00:00 2001 From: dustin-jw Date: Wed, 18 Oct 2023 11:20:48 -0600 Subject: [PATCH] refactor: move/rename game selection functions --- pages/index.njk | 2 +- ...tegory-navigation.js => game-selection.js} | 27 ++++--------------- src/js/helpers/questions.js | 20 ++++++++++++++ 3 files changed, 26 insertions(+), 23 deletions(-) rename src/js/{intercept-category-navigation.js => game-selection.js} (63%) create mode 100644 src/js/helpers/questions.js diff --git a/pages/index.njk b/pages/index.njk index bd5990e..269842d 100644 --- a/pages/index.njk +++ b/pages/index.njk @@ -36,5 +36,5 @@ description: A quiz game and study tool to help those at all skill levels to lea {% endblock %} {% block scripts %} - + {% endblock %} diff --git a/src/js/intercept-category-navigation.js b/src/js/game-selection.js similarity index 63% rename from src/js/intercept-category-navigation.js rename to src/js/game-selection.js index e401cd1..4378c95 100644 --- a/src/js/intercept-category-navigation.js +++ b/src/js/game-selection.js @@ -1,25 +1,6 @@ -const getQuestions = async () => { - const response = await fetch('/questions.json'); - const questions = await response.json(); +import { getQuestions, getRelevantQuestions } from './helpers/questions'; - 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 () => { +const interceptCategoryNavigation = async () => { sessionStorage.removeItem('questions'); sessionStorage.removeItem('questionStatus'); const questions = await getQuestions(); @@ -47,4 +28,6 @@ const getRelevantQuestions = (questions, category, isMultipleChoice) => { window.location.href = questionOrder[0]; }); }); -})(); +}; + +interceptCategoryNavigation(); diff --git a/src/js/helpers/questions.js b/src/js/helpers/questions.js new file mode 100644 index 0000000..ccb85aa --- /dev/null +++ b/src/js/helpers/questions.js @@ -0,0 +1,20 @@ +export const getQuestions = async () => { + const response = await fetch('/questions.json'); + const questions = await response.json(); + + return questions; +}; + +export 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; + }); +};