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

Add CustomContext prop in page wrappers #506

Merged
merged 1 commit into from
Feb 2, 2021
Merged

Conversation

klzns
Copy link
Contributor

@klzns klzns commented Jan 27, 2021

What problem is this solving?

Make it possible to create custom global context in pages.

How to use?

Step 1: Create a React component that accepts children.

  1. Create a React component in the root directory of the react folder
    Example:
/* MyCustomContext.js */
import React from 'react'

import MyContext from './modules/MyContext'

function MyCustomContext({ children }) {
  return <MyContext.Provider value={123}>{children}</MyContext.Provider>
}

export default MyCustomContext
  1. Create an interface for it in the store/interfaces.json file.
    Example:
{
  "my-custom-context": {
    "component": "MyCustomContext",
    "composition": "children"
  }
}

Step 2: Use this new block as a slot of a page wrapper

There are 3 page wrappers available:

Wrapper Pages available Contexts available
storeWrapper All pages useOrderForm, useOrderItems
productWrapper Product pages useOrderForm, useOrderItems, and useProduct
searchWrapper Search pages useOrderForm, useOrderItems, useSearchPage, useSearchPageState, and useSearchPageStateDispatch.

Example of usage:

  1. Simple usage:
 {
   "store.product": {
     "children": [
       // PDP blocks
       // ...
     ]
   },
+  "productWrapper": {
+    "props": {
+      "CustomContext": "my-custom-context"
+    }
+  }
 }
  1. Varying context based on page

You can use the parent feature to change the block based on page. That's similar to the solution of a custom header described here: https://developers.vtex.com/vtex-developer-docs/docs/vtex-io-documentation-customizing-the-header-and-footer-blocks-by-page

 {
   "store.home": {
+    "parent": {
+      "storeWrapper": "storeWrapper#home"
+    },
     "children": [
       // Home blocks
       // ...
     ]
   },
+  "storeWrapper#home": {
+    "props": {
+      "CustomContext": "my-custom-context"
+    }
+  }
 }
 {
   "store.product": {
+   "parent": {
+      "storeWrapper": "storeWrapper#product"
+    },
     "children": [
       // PDP blocks
       // ...
     ]
   },
+  "storeWrapper#product": {
+    "props": {
+      "CustomContext": "my-custom-context"
+    }
+  }
 }
  1. Using more than one page wrapper
 {
   "store.product": {
     "children": [
       // PDP blocks
       // ...
     ]
   },
+  "storeWrapper": {
+    "props": {
+      "CustomContext": "my-custom-context"
+    }
+  },
+  "productWrapper": {
+    "props": {
+      "CustomContext": "my-custom-context"
+    }
+  }
 }
  1. You can mix it all together

Why not? 🤷

 {
   "store.product": {
+    "parent": {
+      "storeWrapper": "storeWrapper#custompdpcontext",
+      "productWrapper": "productWrapper#custompdpcontext"
+    },
     "children": [
       // PDP blocks
       // ...
     ]
   },
+  "storeWrapper#custompdpcontext": {
+    "props": {
+      "CustomContext": "my-custom-context"
+    }
+  },
+  "productWrapper#custompdpcontext": {
+    "props": {
+      "CustomContext": "my-custom-context"
+    }
+  }
 }

Remember that my-custom-context is a normal block, so you can also pass props to it.

Example:

 {
   "store.product": {
     "children": [
       // PDP blocks
       // ...
     ]
   },
   "productWrapper": {
     "props": {
       "CustomContext": "my-custom-context"
     }
   },
+  "my-custom-context": {
+    "props": {
+      "exampleProp": "abc"
+    }
+  }
 }

How to test it?

https://context--storecomponents.myvtex.com/blouse-with-knot/p?skuId=2000544

Screenshots or example usage:

Describe alternatives you've considered, if any.

Related to / Depends on

Depends on vtex-apps/render-runtime#601

How does this PR make you feel? 🔗

@klzns klzns requested review from a team, icazevedo, vitorflg and victorhmp and removed request for a team January 27, 2021 22:28
@vtex-io-ci-cd
Copy link
Contributor

vtex-io-ci-cd bot commented Jan 27, 2021

Hi! I'm VTEX IO CI/CD Bot and I'll be helping you to publish your app! 🤖

Please select which version do you want to release:

  • Patch (backwards-compatible bug fixes)

  • Minor (backwards-compatible functionality)

  • Major (incompatible API changes)

And then you just need to merge your PR when you are ready! There is no need to create a release commit/tag.

  • No thanks, I would rather do it manually 😞

@vtex-io-docs-bot
Copy link

vtex-io-docs-bot bot commented Jan 27, 2021

Beep boop 🤖

I noticed you didn't make any changes at the docs/ folder

  • There's nothing new to document 🤔
  • I'll do it later 😞

In order to keep track, I'll create an issue if you decide now is not a good time

  • I just updated 🎉🎉

Copy link
Contributor

@icazevedo icazevedo left a comment

Choose a reason for hiding this comment

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

LGTM

@gustavopvasconcellos
Copy link
Contributor

👏 👏 👏 👏 👏 👏 👏 👏 👏 👏 👏 👏 👏 👏 👏 👏 👏 👏 👏 <3

@victorhmp victorhmp merged commit d3454fe into master Feb 2, 2021
@victorhmp victorhmp deleted the feature/custom-context branch February 2, 2021 19:58
@vtex-io-docs-bot
Copy link

Beep boop 🤖 That's ok, I created an issue for this so we don't forget

@vtex-io-ci-cd
Copy link
Contributor

vtex-io-ci-cd bot commented Feb 2, 2021

Your PR has been merged! App is being published. 🚀
Version 2.110.1 → 2.111.0

After the publishing process has been completed (check #vtex-io-releases) and doing A/B tests with the new version, you can deploy your release by running:

vtex deploy vtex.store@2.111.0

After that your app will be updated on all accounts.

For more information on the deployment process check the docs. 📖

@azabraao
Copy link

azabraao commented Feb 3, 2021

Works like a charm guys! Thanks a lot for making it! 🎉

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.

6 participants