Skip to content

Commit

Permalink
refactor: move/rename game selection functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin-jw committed Oct 18, 2023
1 parent efac8ae commit 81f1ef3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pages/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ description: A quiz game and study tool to help those at all skill levels to lea
{% endblock %}

{% block scripts %}
<script src="/js/intercept-category-navigation.js" type="module"></script>
<script src="/js/game-selection.js" type="module"></script>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -47,4 +28,6 @@ const getRelevantQuestions = (questions, category, isMultipleChoice) => {
window.location.href = questionOrder[0];
});
});
})();
};

interceptCategoryNavigation();
20 changes: 20 additions & 0 deletions src/js/helpers/questions.js
Original file line number Diff line number Diff line change
@@ -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;
});
};

0 comments on commit 81f1ef3

Please sign in to comment.