HCL Commerce Version 9.1.8.0 or later

Creating Redux Store components

Redux and Redux Saga components store the results in the Redux store.

About this task

Procedure

  1. Open the reducerStateInterface.ts file from src/redux/reducers directory.
  2. Declare and export the reducer state interface for the wishlist.
    export interface WishListReducerState {
      list: any;
    }
  3. Save the file.
  4. Open the initStates.ts file from src/redux/reducers directory.
  5. Import the WishListReducerState file that was created in step 2 .
  6. Create the default state for WishListReducerState and export it.
  7. Now, create the reducers functions to update this state. Create a wishList.ts file in src/redux/reducers.
  8. Create a wishListReducer function, which is used by Redux saga workers to update the Redux State store. To create the wishListReducer, update the following content in the file:
    //Standard libraries
    import { createReducer, AnyAction } from "@reduxjs/toolkit";
    //Redux
    import initStates from "./initStates";
    import * as ACTIONS from "../action-types/wishList";
    import { WISHLIST_RESET_ACTION } from "../actions/wishList";
    
    /**
     * Wish List Reducer
     * handles states used by wish list related components
     * @param state State object managed by wish list reducer
     * @param action The dispatched action
     */
    const WishListReducer = createReducer(initStates.wishList, (builder) => {
      builder.addCase(ACTIONS.WISHLIST_GET_SUCCESS, (state, action: AnyAction) => {
        state.list = action.response.GiftList;
      });
      builder.addcase(WISHLIST_RESET_ACTION, (state, action: AnyAction) => {
        state.list = null;
      });
    });
    export default wishListReducer;
  9. Save the file.
  10. Export the WishListReducerState via RootReducerState interfaces and wishListReducer function in the index.ts file from src/redux/reducers directory. After exporting the function, the code will be updated as shown below:
    import wishList from "./wishList";
    import {
      ErrorReducerState,
      AccountReducerState,
      CatalogReducerState,
      UserReducerState,
      OrderReducerState,
      SEOReducerState,
      ContractReducerState,
      SearchReducerState,
      OrganizationReducerState,
      ContextReducerState,
      SuccessMessageReducerState,
      ConfirmationReducerState,
      RecurringOrderReducerState,
      OrderDetailsMapReducerState,
      SiteReducerState,
      ApiReducerState,
      WishListReducerState,
    } from "./reducerStateInterface";
    export * from "./reducerStateInterface";
    export interface RootReducerState {
      account: AccountReducerState;
      api: ApiReducerState;
      catalog: CatalogReducerState;
      user: UserReducerState;
      order: OrderReducerState;
      error: ErrorReducerState;
      seo: SEOReducerState;
      contract: ContractReducerState;
      search: SearchReducerState;
      organization: OrganizationReducerState;
      context: ContextReducerState;
      success: SuccessMessageReducerState;
      confirmation: ConfirmationReducerState;
      recurringOrder: RecurringOrderReducerState;
      orderDetails: OrderDetailsMapReducerState;
      site: SiteReducerState;
      wishList: WishListReducerState;
    }
    
    
    const reducers = {
      account,
      api,
      catalog,
      user,
      order,
      error,
      seo,
      contract,
      search,
      context,
      organization,
      success,
      confirmation,
      recurringOrder,
      orderDetails,
      site,
      wishList,
    };
  11. Save the file and create the selector for the wish list as explained in steps 12 and 13.
  12. Create a wishList.ts file in the src/redux/selectors directory.
  13. Add the following content for fetching the wishlist Redux store state:
    //Redux
    import { RootReducerState } from "../reducers";
    
    const GetWishListSelector = (state: RootReducerState) => state.wishList.list;
    
    export { GetWishListSelector );
  14. Save and close the file after updating the code.
  15. Since a new wishList has been added in the Redux state, src/mocks/data.ts directory will throw an error.
  16. To correct the error, add a default wishList state in the data.ts file. This is a mock file used for unit testing. To add the default state, update the file with the following code:
    wishList: {
        list: [],
    },
  17. Save and close the file.

Results

All the Redux components and mock data file are created for the wish list.