Lab - Add Header Links
Lab - Add Header Links
Lab - Add Header Links
Let’s take the first steps toward enhancing the B2B-enabled storefront with the mini-app you previously set up to integrate with the Buyer Portal.
This exercise continues where you left off previously. Make sure the dev server is started in your mini-app project:
Remember!
Script Manager in your store control panel must contain the appropriate script to load the mini-app from your local environment on the appropriate port.
Revisit the initial setup lab for full details on setting up your environment.
If it’s helpful to compare your own code as you go, you can clone or download the completed version of this labs as a reference.
Our mini-app is being injected via the entry point script main.tsx. For our initial test implementation, a typical pattern is being used to create an HTML element and render a root React component into it:
As we flesh the app out with real content, we’ll essentially be doing the same thing at multiple entry points in the DOM. Our app doesn’t have a central “App” root but will render a root React component into each DOM location where we want to inject our content.
The lab boilerplate code includes the createRootElement utility function to simplify the injection of the root elements. This relies on configuration in src/utils/dom/config.ts:
For unique keys corresponding with the components we’ll be building, this config specifies a DOM selector and how the injected element is related to it, as well as the type and classname of the injected root element.
For the HeaderLinks component we’ll be building in this lab, this config specifies that its root element should be injected as a child of the ul.navUser-section element, before any existing children, and that the root element should be an li element with the classname navUser-item.
This configuration assumes the BigCommerce default Cornerstone theme for Stencil. Feel free to adjust this config if the DOM in your theme is different.
An empty HeaderLinks component already exists in the boilerplate code. Let’s start by injecting it in main.tsx.
main.tsx and remove the existing test implementation.Remember that the selector this injection is based on assumes the BigCommerce default Cornerstone theme for Stencil.
Navigate to your B2B storefront. While nothing will be rendered on the page yet, the rendering of the React root is working correctly if you see the error “Uncaught Error: HeaderLinks component not implemented” in your browser developer tools console.
Our HeaderLinks component will be making use of the window.b2b.utils.openPage method. We need to update the interface used in our hook, so that TypeScript understands the details of openPage.
src/hooks/useB2B.ts and update the definition of B2BSdk.src/components/HeaderLinks/index.tsx, remove the error message, and add the useB2B hook.Remember, the useB2B hook returns the window.b2b object as soon as it’s available. When this occurs, the updated state value means HeaderLinks will re-render.
button elements for the links.Navigate to your B2B storefront. You should now see the “Orders” and “Invoices” links in the header.

Make sure you’re logged in as a company user and try out the links. The Buyer Portal should open to the appropriate page.
Currently, the custom links render on the page regardless of the user’s permissions, or even whether they’re logged in at all. Let’s add an appropriate inspection of the current user’s role.
Once again, we’re making use of a new method from b2b.utils (the getProfile method of b2b.utils.user) and need to update the appropriate interface.
src/hooks/useB2B.ts and update the definition of B2BUser.We’ll be using the current user’s role to determine whether they should see the “Orders” and “Invoices” links. Unfortunately, we don’t have a way of retrieving the enumeration values that correspond with different roles, so we’ll define these manually in our app. (These values can be found in the Buyer Portal source code.)
We’ll allow users with Admin and Senior Buyer roles to see the links, as well as a few special roles for which we’re not diving into the details here.
src/components/HeaderLinks/index.tsx and add allowedRoles.b2b is available.Note that we no longer need the original if (!b2b) check after useB2B, since the side effect will now handle this.
Now try alternating between logging in as a user with the Junior Buyer role and a user with a more elevated role. The rendering of the links should only occur for users with the appropriate role.
Practice the end-to-end process of injecting a new component at a different entry point and integrating that component with B2B data. This will involve updating src/utils/dom/config.ts and main.tsx in addition to the details of your new component. Don’t forget to update the B2BSdk interface for any new B2B object data you’re interacting with. As a starter idea for data that could be incorporated into a global component, consider that b2b.utils.quote contains the method getCurrent, which will return the details of the B2B quote in progress.