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

Allowing 'as' prop in Link and fixing 'href' prop #45

Merged
merged 5 commits into from
Dec 12, 2018

Conversation

capellini
Copy link
Contributor

Previously, if a user already passed in an 'as' prop to the i18n Link
component, it would overwrite the 'as' prop to the next/link Link component.
This checks to see if an 'as' prop has been passed in to the i18n link
component and, if so, modifies it, rather than overwriting it.

Also, if the 'href' prop happened to already have query parameters, the 'lng'
parameter would be incorrectly added (e.g. if 'href=/foo?bar', then it would
be modified to 'href=/foo?bar?lng=en'). This commit also checks to see if
query parameters already exist and if, so, appends the lng parameter properly.

Lastly, this commit adds some initial test infrastructure using enzyme and
jest with code coverage and adds tests for the Link component.

@isaachinman
Copy link
Contributor

Hi @capellini, thanks for a great contribution.

However, although the matter of testing is something we desperately need (I asked for help awhile ago in #18), I'd like if you could separate these two issues into two distinct PRs:

  1. Fix Link implementation of as and href
  2. Add basic test setup, with tests for Link

Does that sound alright? It will make it easier to review, but more important make it easier to see where/how our testing setup began, without being mixed into actual component logic.

@capellini
Copy link
Contributor Author

However, although the matter of testing is something we desperately need (I asked for help awhile ago in #18), I'd like if you could separate these two issues into two distinct PRs:

Yes, of course. I should have thought to do that myself. I'll take out the testing stuff from this PR and add a separate PR for that.

@isaachinman
Copy link
Contributor

Okay thanks, that's great. Can you just tag me here again when you're comfortable with this PR being reviewed?

@capellini
Copy link
Contributor Author

@isaachinman -- ready for review

children: PropTypes.node.isRequired,
href: PropTypes.string.isRequired,
}

Link.defaultProps = { as: null }
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add line breaks and space this as a normal object.

Copy link
Contributor

Choose a reason for hiding this comment

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

How about adding Prettier to the project to avoid such issues in future? I'm using it everywhere and it saves a lot of times, especially in big teams. I can help with setting that up if there is interest.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll add it in the future as we have more contributors. Not a huge priority right now as eslint will catch the important things.

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 originally had it that way, but CodeClimate failed because the file would have been over 25 lines long. Any chance that we can increase that limit on CodeClimate?

Copy link
Contributor

Choose a reason for hiding this comment

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

We're not going to make decisions like this based on CodeClimate. Don't worry if such a thing causes it to "fail".

)
}
return <NextLink href={href}>{children}</NextLink>

return <NextLink href={href} as={as}>{children}</NextLink>
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it safe to pass null into as?

Copy link
Contributor Author

@capellini capellini Dec 12, 2018

Choose a reason for hiding this comment

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

I had left off the defaultProps entirely, which would have left the 'as' prop as undefined, but that triggered the react/require-default-props linting rule. Perhaps I can disable that rule for this file, as I think it's valid to leave 'as' without a default prop. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

It was a genuine question. If it's alright to pass null into as, I have no problems here. I suspect it is.

Otherwise, just put undefined as the default prop. I don't want to disable the react/require-default-props rule - it's important.

let lng = null
if (Array.isArray(i18n.languages) && i18n.languages.length > 0) {
[lng] = i18n.languages
}
if (localeSubpaths && lng && lng !== defaultLanguage) {
const hrefWithLang = href.includes('?') ? `${href}&lng=${lng}` : `${href}?lng=${lng}`
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if this is the safest way to do query-checking. There would be no harm in pulling in a third-party lib to do query mutations - I would actually prefer that.

Regardless, can you please use if/else statements instead of a ternary? It's especially confusing in this case, due to the '?'.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

The if/else may also push us over our CodeClimate lines of code limit. Let me see what I can do.

@capellini
Copy link
Contributor Author

@isaachinman - requested changes made...I "tested" the new url-parse functionality by using the node REPL. Is there a better way to do this until I can submit the testing PR?

Previously, if a user already passed in an 'as' prop to the i18n Link
component, it would overwrite the 'as' prop to the next/link Link component.
This checks to see if an 'as' prop has been passed in to the i18n link
component and, if so, modifies it, rather than overwriting it.

Also, if the 'href' prop happened to already have query parameters, the 'lng'
parameter would be incorrectly added (e.g. if 'href=/foo?bar', then it would
be modified to 'href=/foo?bar?lng=en').  This commit also checks to see if
query parameters already exist and if, so, appends the lng parameter properly.

Lastly, this commit adds some initial test infrastructure using enzyme and
jest with code coverage and adds tests for the Link component.
@isaachinman
Copy link
Contributor

@capellini Perhaps play around with the example codebase and try running yarn run-example:prod from the root.

I've never used the lib you brought in, only query-string.

I've updated the CodeClimate settings - do you mind doing a force push to trigger a build?

@capellini
Copy link
Contributor Author

Perhaps play around with the example codebase and try running yarn run-example:prod from the root.

I played around a bit and it seems to work fine.

I've never used the lib you brought in, only query-string.

LMK if you would prefer that I use query-string and I can change the code...no problem.

@isaachinman isaachinman merged commit b9ece25 into i18next:master Dec 12, 2018
@capellini capellini deleted the link-as-prop branch January 6, 2019 22:41
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.

3 participants