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

chore(www): Factor out prev-and-next calculation to its own util #20978

Merged
merged 3 commits into from
Feb 9, 2020
Merged

chore(www): Factor out prev-and-next calculation to its own util #20978

merged 3 commits into from
Feb 9, 2020

Conversation

tesseralis
Copy link
Contributor

@tesseralis tesseralis commented Jan 29, 2020

Description

Move calculation of previous and next articles to its own utility file.

This annoyed me enough that I finally decided to do it. The logic is fairly complicated and it cluttered up gatsby-node.

Related Issues and PRs

This can be closed if #21064 gets in first.

@tesseralis tesseralis requested a review from a team as a code owner January 29, 2020 05:08
)
}

function getPrevAndNext(slug) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

everything is copy-pasta'd, except I renamed it from nextAndPrev to prevAndNext to make it line up with the prev-and-next component (and because it makes more sense).


// add values to page context for next and prev page
let prevAndNext = {}
if (docIndex > -1) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

could probably do some refactoring here but I'm too lazy and don't wanna break things.

Copy link
Contributor

Choose a reason for hiding this comment

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

We should be doing things intentionally, can you expand more on the trade-offs you're seeing here?

Copy link
Contributor Author

@tesseralis tesseralis Jan 30, 2020

Choose a reason for hiding this comment

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

From a code clarity perspective, it was hard to wrap my head around what this function is doing: "find the previous and next article in the overall list this article belongs in." We can make it more DRY (although that's a loaded term these days):

// Find the list that this slug belongs in
const list = [flattenedDocs, flattenedTutorials, flattenedContributing].find(list => list.findIndex(findDoc, { link: slug }) > -1)
const index = list.findIndex(findDoc, { link: slug })

// get the previous and next article in that list
const prevAndNext = {
  prev: getSibling(index, list, 'prev')
  next: getSibling(index, list, 'next')
}

As I said, it's not a big deal and I'm okay leaving the code as is. It's just my refactor brain going "wait I can make this cleaner!"

@@ -465,7 +369,7 @@ exports.createPages = ({ graphql, actions, reporter }) => {
context: {
slug: node.fields.slug,
locale,
...nextAndPrev,
...getPrevAndNext(node.fields.slug),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now that I think about it, what is the advantage of passing in this data in context instead of passing it the slug to the <PrevAndNext> component and calling getPrevAndNext from there? I don't think it relies on anything needed in gatsby-node.

Copy link
Contributor

Choose a reason for hiding this comment

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

That seems like something for a followup PR, probably? Keep this scoped to moving the util?

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, that sounds good.

@danabrit danabrit requested a review from madalynrose January 29, 2020 20:51
@wardpeet wardpeet changed the title chore(www) Factor out prev-and-next calculation to its own util chore(www): Factor out prev-and-next calculation to its own util Jan 31, 2020
Copy link
Contributor

@sidharthachatterjee sidharthachatterjee left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you for the clean up

@sidharthachatterjee sidharthachatterjee added the bot: merge on green Gatsbot will merge these PRs automatically when all tests passes label Feb 9, 2020
@gatsbybot gatsbybot merged commit 38dd9ce into gatsbyjs:master Feb 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: merge on green Gatsbot will merge these PRs automatically when all tests passes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants