Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Create shared contexts handle for inner blocks #2568

Merged
merged 2 commits into from
May 28, 2020

Conversation

mikejolley
Copy link
Member

@mikejolley mikejolley commented May 26, 2020

#2399 is a mammoth PR so I'm breaking out some fixes and changes to review separately.

Background behind these changes can be seen in this comment from @nerrad #2399 (comment)

Changes:

  • Shared contexts so Atomic blocks are functional (see Single Product Block #2399 (comment)) This ensures contexts work when wrapping atomic blocks in the editor and on frontend.
  • Combines useProductLayoutContext and useInnerBlockConfigurationContext since both are used at the same time in all contexts. This allows the parent block to be passed, as well as a prefix for the CSS classnames.
  • Introduces ProductDataContext to share product data with atomic blocks. This is not in use yet; it is for the single product block.

How to test the changes in this Pull Request:

This isn't in use by multiple blocks yet, so to test please just Smoke Test the All Products block and ensure there are no regressions in the layout (front and backend).

@mikejolley mikejolley added status: needs review type: refactor The issue/PR is related to refactoring. labels May 26, 2020
@mikejolley mikejolley added this to the 2.7.0 milestone May 26, 2020
@mikejolley mikejolley requested a review from a team as a code owner May 26, 2020 17:51
@mikejolley mikejolley self-assigned this May 26, 2020
@mikejolley mikejolley requested review from haszari and nerrad and removed request for a team May 26, 2020 17:51
@github-actions
Copy link
Contributor

github-actions bot commented May 26, 2020

Size Change: +618 B (0%)

Total Size: 1.99 MB

Filename Size Change
build/active-filters-frontend.js 7.23 kB +3 B (0%)
build/active-filters.js 7.89 kB -3 B (0%)
build/all-products-frontend.js 71.5 kB -298 B (0%)
build/all-products.js 18.9 kB -293 B (1%)
build/all-reviews.js 10.6 kB +4 B (0%)
build/attribute-filter-frontend.js 16.7 kB +3 B (0%)
build/attribute-filter.js 11.5 kB -7 B (0%)
build/block-error-boundary.js 774 B -1 B
build/blocks.js 2.92 kB -2 B (0%)
build/cart-frontend.js 113 kB +3 B (0%)
build/cart.js 32.3 kB +14 B (0%)
build/checkout-frontend.js 129 kB -1 B
build/checkout.js 37.9 kB +16 B (0%)
build/featured-category.js 146 kB -3 B (0%)
build/featured-product.js 9.8 kB -5 B (0%)
build/handpicked-products.js 7.15 kB +2 B (0%)
build/price-filter-frontend.js 14.1 kB +3 B (0%)
build/price-filter.js 9.97 kB -1 B
build/product-best-sellers.js 7.23 kB -2 B (0%)
build/product-categories.js 3.21 kB -3 B (0%)
build/product-category.js 8.14 kB +1 B
build/product-on-sale.js 7.79 kB -3 B (0%)
build/product-search.js 3.36 kB -3 B (0%)
build/product-tag.js 6.33 kB +1 B
build/product-top-rated.js 7.36 kB -2 B (0%)
build/products-by-attribute.js 8.11 kB -2 B (0%)
build/reviews-by-category.js 12.7 kB +2 B (0%)
build/reviews-by-product.js 14.2 kB +1 B
build/reviews-frontend.js 8.81 kB +5 B (0%)
build/vendors.js 473 kB +3 B (0%)
build/wc-blocks-data.js 7.08 kB +5 B (0%)
build/wc-blocks-middleware.js 932 B +1 B
build/wc-blocks-registry.js 1.78 kB -3 B (0%)
build/wc-settings.js 2.14 kB +1 B
build/wc-shared-context.js 1.18 kB +1.18 kB (100%) 🆘
ℹ️ View Unchanged
Filename Size Change
build/all-reviews-legacy.js 10.3 kB 0 B
build/block-error-boundary-legacy.js 774 B 0 B
build/blocks-legacy.js 2.92 kB 0 B
build/custom-select-control-style-legacy.js 782 B 0 B
build/custom-select-control-style.js 782 B 0 B
build/editor-legacy-rtl.css 12.5 kB 0 B
build/editor-legacy.css 12.6 kB 0 B
build/editor-rtl.css 13.5 kB 0 B
build/editor.css 13.5 kB 0 B
build/featured-category-legacy.js 146 kB 0 B
build/featured-product-legacy.js 9.49 kB 0 B
build/handpicked-products-legacy.js 6.88 kB 0 B
build/panel-style-legacy.js 773 B 0 B
build/panel-style.js 773 B 0 B
build/product-best-sellers-legacy.js 6.96 kB 0 B
build/product-categories-legacy.js 3.22 kB 0 B
build/product-category-legacy.js 7.88 kB 0 B
build/product-list-style-legacy.js 775 B 0 B
build/product-new-legacy.js 7.12 kB 0 B
build/product-new.js 7.39 kB 0 B
build/product-on-sale-legacy.js 7.49 kB 0 B
build/product-search-legacy.js 3.12 kB 0 B
build/product-tag-legacy.js 6.07 kB 0 B
build/product-top-rated-legacy.js 7.09 kB 0 B
build/products-by-attribute-legacy.js 7.85 kB 0 B
build/reviews-by-category-legacy.js 12.3 kB 0 B
build/reviews-by-product-legacy.js 13.8 kB 0 B
build/reviews-frontend-legacy.js 7.99 kB 0 B
build/snackbar-notice-style-legacy.js 778 B 0 B
build/snackbar-notice-style.js 778 B 0 B
build/spinner-style-legacy.js 775 B 0 B
build/spinner-style.js 771 B 0 B
build/style-legacy-rtl.css 5.62 kB 0 B
build/style-legacy.css 5.63 kB 0 B
build/style-rtl.css 16.6 kB 0 B
build/style.css 16.5 kB 0 B
build/vendors-legacy.js 376 kB 0 B
build/vendors-style-legacy-rtl.css 1.65 kB 0 B
build/vendors-style-legacy.css 1.65 kB 0 B
build/vendors-style-legacy.js 105 B 0 B
build/vendors-style-rtl.css 1.65 kB 0 B
build/vendors-style.css 1.65 kB 0 B
build/vendors-style.js 105 B 0 B
build/wc-payment-method-cheque.js 794 B 0 B
build/wc-payment-method-paypal.js 830 B 0 B
build/wc-payment-method-stripe.js 11.9 kB 0 B

compressed-size-action

Copy link
Member

@haszari haszari left a comment

Choose a reason for hiding this comment

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

Looks like we maybe have a problem - I'm seeing this error on front end:

Screen Shot 2020-05-27 at 12 15 58 PM

By the way, this is a tricky PR to review. I'd really appreciate a bit more rationale/detail about the intention in this PR description. I realise that this may be covered in the linked comment but it's easy to get rabbit holed. It's ideal if we can split of a chunk of a PR and an average team member can review it in isolation, and understand what the goal is :)

This takes extra time and effort for sure – abstraction is hard, docs are hard – but I think it's worth the effort :)

#2399 is a mammoth PR so I'm breaking out some fixes and changes to review separately.

This is a pretty mammoth change on its own! 😅

@mikejolley
Copy link
Member Author

mikejolley commented May 27, 2020

... @haszari that error is identical to the one you posted in #2441 so it seems to me it's unrelated to the changes in this PR.

I'd really appreciate a bit more rationale/detail about the intention in this PR description. I realise that this may be covered in the linked comment but it's easy to get rabbit holed

The linked comment seems clear enough. I'm not sure what more explanation I can offer on top of the comment from @nerrad. In case you missed it, this is the key bit of information about the change to be aware of:

Essentially the problem was that the single product block was using an instance of InnerBlockConfigurationProvider that was different than the instance built in the all-products bundle (which is where the atomic blocks were registered). Thus the components being rendered were from the atomic blocks in the all-products bundle and their usage of useInnerBlockConfigurationContext was assuming the same instance of the context defined in the all-products bundle.

The fix, is to push the InnerBlockConfigurationProvider and it's hook into a separately built shared library. That way we're ensuring both blocks are using the correct instance.

Sounds like you were just derailed by the error message?

This is a pretty mammoth change on its own!

¯_(ツ)_/¯ It's mostly moving code around.

@haszari
Copy link
Member

haszari commented May 27, 2020

Sounds like you were just derailed by the error message?

Yep totally – apologies, I guess this is a risk when we have known issues like this.

Thanks for highlighting the "context" (pun intended!) - still not fully clear on the details. For example, I'm not familiar with InnerBlockConfigurationProvider, so need to research to understand what it provides, and what breaks when different context instances are used across different blocks. I'll do a general smoke test of All Products & friends. I see you have @nerrad tagged to review too - I'm sure he'll give this a good code review :)

Copy link
Member

@haszari haszari left a comment

Choose a reason for hiding this comment

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

Gave All Products & friends a good test on front end and in editor.

  • Existing blocks loaded fine in editor.
  • Existing blocks loaded fine and worked correctly on front end (filtering).
  • Changed All Products options - rows, align etc - persisted and worked on front end.
  • Changed All Products inner blocks, worked correctly on front end.
  • Quick test of front end in Safari & Firefox.

Didn't see any issues :)

Regarding the context / provider issue - my understanding (from reading the comments) is that we should have a single context instance across all blocks (within a page load) (aka singleton pattern?), and the issue here is that because of the way we were building/bundling, we ended up with two contexts for different kinds of block.

Is there a pattern/convention we could use to prevent/detect issues like this happening in future? How do you spot this kind of problem in a review, or how would it show up as bugs in e2e or manual testing?

I see in @nerrad's comment:

Anytime we share context across bundles that implement the context wrapping atomic blocks, we should put that context in the new shared context build I did in the commit.

Though it looks like we're still working out the best/right approach here, and there are quite a few considerations - tricky :)

@mikejolley
Copy link
Member Author

I'm not familiar with InnerBlockConfigurationProvider, so need to research to understand what it provides

I think we'll need to document these more after Inner Blocks work is concluded. There is a readme file for each context however. This one provides a classname for contained inner block components (so they all get the same prefix) and a parent block name.

we ended up with two contexts for different kinds of block.

Yup :)

Is there a pattern/convention we could use to prevent/detect issues like this happening in future? How do you spot this kind of problem in a review, or how would it show up as bugs in e2e or manual testing?

It's difficult - I spotted this when my context values were not picked up by the nested components. This occurs because each frontend block has it's own script file.

@mikejolley mikejolley merged commit 0fc3ea1 into master May 28, 2020
@mikejolley mikejolley deleted the update/shared-context branch May 28, 2020 10:02
@Aljullu Aljullu added the skip-changelog PRs that you don't want to appear in the changelog. label Jun 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
skip-changelog PRs that you don't want to appear in the changelog. type: refactor The issue/PR is related to refactoring.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants