Lab - Add a Redux Slice
Lab - Add a Redux Slice
Lab - Add a Redux Slice
In this exercise, we’ll practice the use of Redux state in the Buyer Portal. The state we’ll be adding and testing with won’t be useful just yet, but we’ll make use of it in a future exercise.
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.
In a future exercise, we’ll be simulating fetching support case data from an external CRM. What we’ll ultimately need in this scenario is an authentication token for that separate API, which is appropriate to store globally in the user’s session state, just like the B2B GraphQL token.
In these steps, we’ll set up a custom Redux slice to store this value, as well as test the appropriate entry points for setting it when the application is initialized and retrieving it in the right component.
Remember, all file paths referenced in these exercises are relative to apps/storefront/src in your Buyer Portal project.
store/slices/crm.ts and add the appropriate interfaces and slice definition.Our slice is very simple, containing only a single string value: crmToken.
The code we’ve put in place is doing a few things:
createSlice is used to create our unique slice with:
selectCrmToken, used to retrieve the current state value.redux-persist library is used in the reducer export to persist this slice’s state to the browser’s session storage.Immutable State
Recall that global state must be immutable, meaning that the state value cannot be changed directly. The syntax in the setCrmToken reducer appears to be doing just that, but this is part of what Redux does under the hood. Redux handles cloning a brand new state object with the new value, allowing us to use a more straightforward syntax in the reducer.
Now we need to update a couple of global files to include our new slice with the rest of the app’s slices.
store/index.ts and add an export for the crm slice. This will allow any code to import the slice’s actions and selectors directly from @/store.store/reducer.ts and add crm to the combineReducers call.We can use our slice’s selector anywhere in our code, along with the useAppSelector hook from @/store, to retrieve the current value.
We’re going to take this a step further by encapsulating this in our own unique hook. Our code will then be able to use this hook without concerning itself with the details of how the state is retrieved.
shared/service/crm-bff/useCrmToken.ts and implement the hook.The details of fetching the CRM token will be handled in a future exercise, but for now we’ll use a static value in the appropriate entry points for setting and retrieving the state.
App.tsx. This is the central entry point for the application and where we will ultimately initialize the CRM token.return, add a React side effect to set the test value.Notice we’re using storeDispatch, the global store dispatch function, along with our unique action.
pages/Overview/components/RecentOrders.tsx. This is the component where we’ll eventually be using the token.You should be able to observe the functionality with your browser developer tools, with the Overview page open: