Lab - Add Dynamic Data
Lab - Add Dynamic Data
Lab - Add Dynamic Data
The “Recent Orders” section on our custom Overview page is currently populated with mock data. In this exercise, we’ll replace this with real data fetched via the B2B GraphQL API.
This exercise continues where you left off previously. Make sure the dev server is started in your Buyer Portal project:
If you need a fresh start, you can follow the instructions below to set up a new project complete with previous exercise code.
Remember!
Script Manager in your store control panel must contain appropriate header/footer scripts to load the Buyer Portal 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.
Remember, all file paths referenced in these exercises are relative to apps/storefront/src in your Buyer Portal project.
The data.ts file in our page component’s directory already contains GraphQL logic for several sections we’ll be adding in this exercise. This file doesn’t yet contain the proper GraphQL for recent orders.
pages/Overview/data.ts and add a response interface and GraphQL query string.It’s not strictly important where in the file this code is added. RecentOrdersResponse defines what we expect the response to our GraphQL query to look like. Note that each node is expected to be an OverviewOrder, which is another interface already defined with fields such as orderId, createdAt, and poNumber.
The fields of OverviewOrder should be familiar, as our “Recent Orders” table is already configured with columns matching these keys. The mock data currently populating the table matches this schema.
RecentOrdersQuery is the GraphQL query itself, using the allOrders field to fetch a list of records. Note the query is written to support sorting and paging variables, making it flexible for reuse.
getRecentOrders function.Our code doesn’t need to concern itself with how the user token used to authenticate the GraphQL request is managed or retrieved, with B3Request handling this for us.
Note the use of map to form a flattened array of orders from each node in allOrders.edges in the response. getRecentOrders will return an array where each item has an orderId, poNumber, etc.
Our use of the RecentOrdersResponse type assertion tells TypeScript what to expect but doesn’t actually validate that the response data matches this. Our code currently lacks any actual error handling for the GraphQL fetch. Zod, which was used in the previous “mini-app” exercises, can be used in the same way within the Buyer Portal as well.
pages/Overview/components/RecentOrders.tsx, update the import from ../data to include the new function, and remove the mock data array.useEffect callback, replace the current setOrders call with a call to getRecentOrders.The orders state will be updated when the response is received from the GraphQL request.
Now that we’re loading real data, let’s complete the “spinner” implementation by adding a piece of React state to track when the data is loading.
useEffect callback and JSX accordingly.Remember that we’ve already configured our component to defer triggering this data fetching until the accordion is expanded.
Revisit your Overview page and see the Recent Orders fetch in action. Try using the Network tab in your browser’s developer tools to inspect the GraphQL request and response.
As this is an Overview page, we want to show more than just recent orders. There are already components and GraphQL queries in place in our boilerplate code for invoices, shopping lists, and quotes. Let’s add these remaining sections.
pages/Overview/index.tsx and add imports for the other components.ordersOpen state we’re using to track when the “Recent Orders” accordion is open, add state values for the other sections.Remember that we’ve used selective permission checking to control whether “Recent Orders” is displayed. We’ll need to add similar logic for the other sections.
Your Overview page should now have fully functioning sections for all data types. Observe the GraphQL requests that are triggered by expanding each accordion.
