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 driver usage to improve Venia portability #1217

Merged
merged 10 commits into from
May 11, 2019

Conversation

zetlen
Copy link
Contributor

@zetlen zetlen commented May 7, 2019

This PR is a cherry-pick from #1198 to make that PR more digestible.

It is number 3 of 6 in the chain of cherry-picked PRs. It needs to be merged before the next 3:

  1. Graphql playground autodetects queries #1219
  2. Unified system for environment-based configuration #1224
  3. Create PWA tool #1198

Description

When trying to use Venia components in a context with another app, it's easy to get React errors claiming you're trying to use a Link element without a Router in context. This mostly happens because we forgot a couple of spots where we are hardcoding a particular instance of React Router instead of pulling in the instance provided by @magento/venia-drivers. This PR does the refactoring and prop drilling necessary to pass drivers around, so that an app can share routing with other components.

Related Issue

Closes #ISSUE_NUMBER.

Verification Steps

  1. Ensure yarn build is successful
  2. Run yarn watch:venia and load up your local Venia in your browser.
  3. Ensure the main (CMS) page works.
  4. Click through to the Create Account page, ensure that works.
  5. Execute a search and ensure that works.
  6. Hit a page that doesn't exist (ex: /noexist.html) and ensure you see the "Something went wrong. Please try again" message.
  7. Sign In and ensure you can pull up your account by clicking on your user information:

Screen Shot 2019-05-09 at 10 13 16 AM

How Have YOU Tested this?

Screenshots / Screen Captures (if appropriate):

Proposed Labels for Change Type/Package

  • major (e.g x.0.0 - a breaking change)
  • minor (e.g 0.x.0 - a backwards compatible addition)
  • patch (e.g 0.0.x - a bug fix)

Checklist:

  • I have updated the documentation accordingly, if necessary.
  • I have added tests to cover my changes, if necessary.

@vercel
Copy link

vercel bot commented May 7, 2019

This pull request is automatically deployed with Now.
To access deployments, click Details below or on the icon next to each push.

Latest deployment for this branch: https://venia-git-zetlen-refactor-router-drivers-for-portability.magento-research1.now.sh

@zetlen zetlen changed the base branch from develop to zetlen/remove-unused-buildpack-tools May 7, 2019 19:23
@zetlen zetlen force-pushed the zetlen/refactor-router-drivers-for-portability branch from e6d1d43 to 917853b Compare May 7, 2019 19:27
@coveralls
Copy link

coveralls commented May 7, 2019

Coverage Status

Coverage remained the same at 78.195% when pulling d309335 on zetlen/refactor-router-drivers-for-portability into 8f64f51 on zetlen/remove-unused-buildpack-tools.

"@babel/plugin-transform-runtime": "~7.3.4",
"@babel/runtime": "~7.3.4",
"@babel/plugin-transform-runtime": "~7.4.4",
"@babel/runtime": "~7.4.2",
Copy link
Contributor

Choose a reason for hiding this comment

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

Since it's listed in peerDependencies do we need @babel/runtime here?

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, for development.

Copy link
Contributor

Choose a reason for hiding this comment

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

@supernova-at Entries in peerDependencies aren't installed when you run yarn (or npm install). They're needed for tests, though, so we have to list them in devDependencies as well.

@@ -47,14 +44,14 @@
"apollo-link-http": "~1.5.11",
"braintree-web-drop-in": "~1.16.0",
"graphql-cli": "~3.0.11",
"graphql-cli-validate-magento-pwa-queries": "~1.0.0",
"graphql-cli-validate-magento-pwa-queries": "~1.1.0-beta.1",
Copy link
Contributor

Choose a reason for hiding this comment

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

Was this change for testing purposes or do we actually want to commit it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For testing. Removing it.

<Route render={() => <Page>{renderRoutingError}</Page>} />
<Route
render={() => (
<Page using={{ Route, Router }}>{renderRoutingError}</Page>
Copy link
Contributor

Choose a reason for hiding this comment

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

More of a curiosity question: is it possible for the Route component being rendered here and the Route being passed in the using prop to Page to be different? Would you ever actually want to do that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's possible. And honestly it's easier to just drill the props through. <Page /> is its own component and could be used separately by something in the future, so rather than try to make a new ContextProvider and think about all those use cases, I thought props would be simpler code.

@@ -144,6 +145,6 @@ class App extends Component {
[`Page.js`]: https://github.com/magento-research/pwa-studio/blob/master/packages/peregrine/src/Page/Page.js
[`react-router`]: https://github.com/ReactTraining/react-router
[React Context]: https://reactjs.org/docs/context.html
[ErrorView]:t://github.com/magento-research/pwa-studio/blob/master/packages/venia-concept/src/components/ErrorView/errorView.js
[ErrorView]: https://github.com/magento-research/pwa-studio/blob/master/packages/venia-concept/src/components/ErrorView/errorView.js
Copy link
Contributor

Choose a reason for hiding this comment

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

image

supernova-at
supernova-at previously approved these changes May 8, 2019
Copy link
Contributor

@supernova-at supernova-at left a comment

Choose a reason for hiding this comment

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

Assuming my comments don't need to be addressed, 👍

@supernova-at supernova-at self-assigned this May 8, 2019
@zetlen zetlen changed the base branch from zetlen/remove-unused-buildpack-tools to develop May 8, 2019 22:22
@zetlen
Copy link
Contributor Author

zetlen commented May 9, 2019

@supernova-at Thanks for reminding me about the beta version I left in there. I removed it and answered your question about passing Route around. I'd love it if you took another look (at this one before my others if possible) :)

supernova-at
supernova-at previously approved these changes May 9, 2019
@dpatil-magento
Copy link
Contributor

@zetlen Home page does not load https://venia-e2aw4xl3e.now.sh/ console - Uncaught TypeError: Cannot read property 'location' of undefined

@supernova-at
Copy link
Contributor

supernova-at commented May 9, 2019

@zetlen Home page does not load https://venia-e2aw4xl3e.now.sh/ console - Uncaught TypeError: Cannot read property 'location' of undefined

I successfully verified on my local machine. Not sure where the disconnect is there. location is likely React Router's location.

@zetlen
Copy link
Contributor Author

zetlen commented May 9, 2019

@dpatil-magento I've refactored this to be much simpler. It appears to be working now in staging--please validate if you have the time!

@supernova-at @jimbo No more passing the Route and Router pair around. I figured out what was causing the realm mismatch: it was just bad versioning and withRouter.

@zetlen zetlen requested a review from dpatil-magento May 9, 2019 21:08
@dpatil-magento
Copy link
Contributor

@zetlen Thanks, testing this one now.

Copy link
Contributor

@jimbo jimbo 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 to me. I've left a couple of comments.

@@ -1,5 +1,5 @@
export { Query } from 'react-apollo';
export { Link, Redirect, Route } from 'react-router-dom';
export { Link, Redirect, Route, Switch, withRouter } from 'react-router-dom';
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider export * here, maybe? This is such a major dependency—I think whitelisting only some of its exports qualifies as a leaky abstraction.

If a consumer of venia-concept wanted to import useRouter(), for example, they'd encounter the same "conflicting context" issues that this change is addressing.

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'd rather export these things one by one to assist in tree shaking.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I ordinarily wouldn't recommend export * for that reason. I think this counts as a serious flaw in this approach, though. If we're not going to export *, then we should whitelist all of its exports. And we should consider doing the same for the other third-party libraries being exported here as well.

packages/pwa-buildpack/package.json Outdated Show resolved Hide resolved
Copy link
Contributor

@dpatil-magento dpatil-magento left a comment

Choose a reason for hiding this comment

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

QA Pass.

…ub.com:magento-research/pwa-studio into zetlen/refactor-router-drivers-for-portability
@zetlen zetlen merged commit 1e1fef1 into develop May 11, 2019
@zetlen zetlen deleted the zetlen/refactor-router-drivers-for-portability branch July 25, 2019 20:05
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.

5 participants