Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make All Questions dynamic to reduce redundant files #178

Merged
merged 3 commits into from
Feb 28, 2024

Conversation

dustin-jw
Copy link
Contributor

Description

These changes modify how we generate pages for "All Questions" as a category. Rather than having separate templates that are mostly redundant, this treats "All Questions" like a tag (without requiring us to set an "All Questions" tag on every question in Airtable). The SEO descriptions are slightly different to avoid odd wording, and the URLs for questions are different now, but I think these are acceptable differences (seems unlikely someone would have these pages bookmarked).

To Validate

  1. Make sure all PR Checks have passed
  2. Pull down this branch
  3. Run npm start
  4. Visit the home page and confirm that it matches the live site (if you have VIEW_DRAFTS set to true in your .env file, you may see extra categories, which is fine)
  5. Click on the "All Questions" links for Flash Cards, Multiple Choice, and Short Answer game types, and confirm that the headings match the live site ("All Categories", "Question 1 of n", where Multiple Choice has more questions than the other game types)
  6. Click through other categories and game types, making sure behavior is otherwise unaffected

Copy link

netlify bot commented Feb 27, 2024

Deploy Preview for triviaccessibility ready!

Name Link
🔨 Latest commit 9f596b9
🔍 Latest deploy log https://app.netlify.com/sites/triviaccessibility/deploys/65de2b0761c19b0008eecb19
😎 Deploy Preview https://deploy-preview-178--triviaccessibility.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

@marissahuysentruyt marissahuysentruyt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going through these PRs is helping me understand this project better 🥳 Most of my comments are just me paraphrasing what's going on in the code (I hope).

I really only had one suggestion about the SEO 👍

Comment on lines +58 to +66
return [
'All Questions',
...new Set(
questions
.map((question) => question.Tags)
.flat()
.sort()
),
];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, we're returning a new category, All Questions, and then returning the question Tags (which are the categories) as before...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few complicated array actions here that I think are worth calling out, so I'll try to break it down. The end result that we want is an array with each unique category we find in alphabetical order (except for making sure "All Questions" is the first item).

  • ...new Set(arrayWithDuplicateItems) lets us remove duplicate items
  • You know about .map, but since question.Tags is an array, we end up with an array of arrays, like [["ARIA"], ["Color Contrast", "Design"]]. By using .flat, we take those inner arrays and flatten them out so we end up with a one-dimensional array, like ["ARIA", "Color Contrast", "Design"]
  • Combining all of these things with .sort gives us the list of categories that we need, All Questions, ARIA, Assistive Technologies, etc. as shown on the home page

Comment on lines +71 to +79
questions.forEach((question, index) => {
questionGroups.push({
category: 'All Questions',
tagName: 'all-questions',
pageNumber: index + 1,
questionTotal: questions.length,
question,
});
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...then this is where every question gets the category all-questions added. And pushed to questionGroups...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, exactly! It felt less brittle to do it in code than to have to remember to add an "All Questions" tag to the data in Airtable.

Comment on lines +81 to +104
categories.forEach((category) => {
let pageNumber = 1;
const questionTotal = questions.reduce((sum, question) => {
if (question.Tags.includes(category)) {
return sum + 1;
}

return sum;
}, 0);

questions.forEach((question) => {
if (question.Tags.includes(category)) {
questionGroups.push({
category,
tagName: slugify(category),
pageNumber,
questionTotal,
question,
});

pageNumber += 1;
}
});
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...then this is still doing what it was doing before (I think).

  • checking the categories and determining how many questions are in that category (starting with a sum of 0)
  • looping through each of the questions in that category, and pushing that object to questionGroups, and increasing pageNumber (which is what the URL is built with)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this part is handling the other categories, and most of the data here is used for the permalinks. This is sort of a drawback with using Eleventy compared to something like Next.js that handles routing for you a little more easily. This type of side project is perfect for doing things the hard, boring, stupid way, though!

@@ -10,7 +10,7 @@ eleventyExcludeFromCollections: true
{% extends 'layout.njk' %}

{% set seoTitle %}Question {{ questionGroup.pageNumber }} of {{ questionGroup.questionTotal }} | Multiple Choice - {{ questionGroup.category }} | Trivia11y{% endset %}
{% set seoDescription %}Answer question {{ questionGroup.pageNumber }} of {{ questionGroup.questionTotal }} about {{ questionGroup.category }} (Multiple Choice).{% endset %}
{% set seoDescription %}Answer question {{ questionGroup.pageNumber }} of {{ questionGroup.questionTotal }}: {{ questionGroup.category }} (Multiple Choice).{% endset %}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mentioned in the validation instructions that the SEO would change, which I see here. What made you land on the colon?

It sounded like maybe you weren't super happy with this change, so what about like, "regarding" or "relating to"? Does that work better for all questions and a category?

"Answer question 14 of 40 regarding all questions?" "Answer question 14 of 40 relating to all questions?" Maybe that's not really any more clear, just a thought 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanted something other than "Answer question 14 of 40 about All Questions (Multiple Choice)". The colon felt like a good compromise that worked equally well for "All Questions" as well as something like "Forms".

Thinking about the context, though, the most likely case for someone reading the description is if the link is shared on social media. I wonder if what we have makes sense for that, or maybe it would be better to just have a generic description about Trivia11y as a whole 🤔

I'm happy to call that out of scope for this, though!

@dustin-jw dustin-jw merged commit 14db9a2 into main Feb 28, 2024
6 checks passed
@dustin-jw dustin-jw deleted the refactor--remove-redundant-files branch February 28, 2024 21:42
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.

2 participants