Building the TUA Spartacus Storefront Using 1.2 Libraries

Note: Spartacus 4.x is no longer maintained. Please upgrade to the latest version.

Note: Spartacus 4.x was tested with SAP Commerce Cloud versions 1905 to 2205. Spartacus 4.x has not been verified to work with (and is not guaranteed to work with) SAP Commerce Cloud 2211 or later releases.

The following instructions describe how to build a TUA storefront application using published TUA Spartacus 1.2 libraries.

Note: If you are building TUA Spartacus from source, see Contributor Setup.


Table of Contents


Prerequisites

Before carrying out the procedures below, ensure that you meet the following front-end and back-end requirements.

Front-End Development Requirements

  • Angular CLI: 8.2.5 or later, < 9.0.0
  • node.js: 10.14.1 or later, < 13.0. The most recent 12.x version is recommended
  • yarn: 1.15 or later

Installing or Updating the Prerequisite Development Tools

There are a few ways you can install Angular and other prerequisite software. The following example installs yarn, node.js, and Angular CLI using Homebrew, which was created for MacOS, but also works on Linux and Windows 10 (through the Linux subsystem feature).

To install the prerequisite development tools, install Homebrew, and then run the following commands:

brew install yarn
brew install node@10
brew install angular-cli

To update existing installations, use brew upgrade instead of brew install.

Note:

  • If you have a later version of node.js installed in addition to v10, you can set v10 to be used with the following command: brew link --force --overwrite node@10.
  • Installing Homebrew and the prerequisites is beyond the scope of this document. You can also install the prerequisites using their individual installers.

Back End Server Requirements

TUA Spartacus uses SAP Commerce Cloud and Telco & Utilities Accelerator for its back end, and makes use of the sample data from the Telco & Utilities Accelerator storefront in particular.

  • SAP Commerce Cloud version: Release 1905 (latest patch is recommended).
  • Telco & Utilities Accelerator version: Release 2003 (latest patch) is required.

For more information, see Installing SAP Commerce Cloud for use with TUA Spartacus.

Creating a New Angular App

To create a new Angular application with the name mystore, follow the procedure:

  1. Open a terminal or command prompt window at the location of your choice.

  2. Using the Angular CLI, generate a new Angular application with the following command:

    ng new mystore --style=scss
    

    When prompted if you would like to add Angular routing, enter n for ‘no’.

    The mystore folder and the new app are created.

  3. Access the newly created mystore folder with the following command:

   cd mystore

Project Setup

The easiest way to start a new project and to quickly set up your application:

Run yarn start.

For a full list of available parameters, see the Spartacus schematics documentation.

Setting up a Storefront Manually

Although we recommend using Schematics, there might be situations when you want to build your application from scratch.

The dependencies in this procedure are required by the TUA Spartacus storefront.

  1. Inspect the mystore/src/app/app.module.ts file for any changes you want to make for your setup. For example, you might want to change the baseUrl to point to your server and the basesite to corresond with the WCMS site. You likely also want to specify the compatibility version by changing features.level, as the default might not be the latest version.

    To make use of the modules shipped with tua-spa library, the app.module.ts must have the following structure:

    
       import { BrowserModule } from '@angular/platform-browser';
       import { NgModule } from '@angular/core';
       import { AppComponent } from './app.component';
       import { translationChunksConfig, translations } from '@spartacus/assets';
       import { ConfigModule } from '@spartacus/core';
       import { TmaB2cStorefrontModule, tmaTranslations } from '@spartacus/tua-spa';
    
       @NgModule({
         declarations: [
           AppComponent
         ],
         imports: [
           BrowserModule,
           TmaB2cStorefrontModule.withConfig({
             backend: {
               tmf: {
                 baseUrl: 'https://localhost:9002',
                 prefix: '/b2ctelcotmfwebservices/v2/'
               },
               occ: {
                 baseUrl: 'https://localhost:9002',
                 prefix: '/rest/v2/'
               },
               tmf_resource_pool_management: {
                 baseUrl: 'http://localhost:8080',
                 prefix: '/tmf-api',
               },
               tmf_appointment: {
                 baseUrl: 'http://localhost:8080',
                 prefix: '/tmf-api'
               }
             },
             context: {
               urlParameters: ['baseSite', 'language', 'currency'],
               baseSite: ['telcospa']
             },
             i18n: {
               resources: translations,
               chunks: translationChunksConfig,
               fallbackLang: 'en'
             },
             features: {
               level: '1.5',
               saveForLater: false
             },
             journeyChecklist: {
               journeyChecklistSteps: ['APPOINTMENT', 'MSISDN'],
                 msisdn_reservation: {
                     msisdn_qty: 1,
                     msisdn_capacity_amount_demand: 1,
                     msisdn_applied_capacity_amount: 5,
                     applied_capacity_amount_for_msisdn_reservation: 1,
                 },
               appointment: {
                   requested_number_of_timeslots: 5,
                   end_date_of_timeslots: 3,
                 },
               },
           }),
           ConfigModule.withConfig({
             i18n: {
               resources: tmaTranslations
             }
           })
         ],
         providers: [],
         bootstrap: [AppComponent]
       })
       export class AppModule { }
    
    
  2. Replace the entire contents of mystore/src/app/app.component.html with <cx-storefront>Loading...</cx-storefront> with:

    <cx-storefront>Loading...</cx-storefront>
    
  3. Open mystore/package.json using a text editor.

  4. Add the following dependencies to the end of the dependencies section of package.json.

     "@angular/pwa": "^0.803.2",
     "@angular/service-worker": "~8.2.5",
     "@ng-bootstrap/ng-bootstrap": "5.1.0",
     "@ng-select/ng-select": "^3.0.7",
     "@ngrx/effects": "~8.3.0",
     "@ngrx/router-store": "~8.3.0",
     "@ngrx/store": "~8.3.0",
     "ngx-infinite-scroll": "^8.0.0",
     "bootstrap": "4.2.1",
     "i18next": "^15.0.6",
     "i18next-xhr-backend": "^2.0.1",
     "material-design-icons": "^3.0.1",
    
     "@spartacus/core": "~1.5.0",
     "@spartacus/styles": "~1.5.0",
     "@spartacus/storefront": "~1.5.0",
     "@spartacus/assets": "~1.5.0",
     "@spartacus/tua-spa": "~1.2.0",
     "ng2-charts": "^2.3.2",
     "chart.js": "^2.9.3",
     "ngx-spinner": "^8.0.3"
    
    

    Note: Make sure to add a comma to the end of the last dependency statement listed in this section. For example, the last statement in your new app might be "zone.js": "~0.9.1" so you need to add a comma after 0.9.1".

  5. Make sure that the following import is found in the mystore/src/styles.scss:

    @import '~@spartacus/styles/index';
    @import '~@spartacus/tua-spa/storefrontstyles/index';
    
  6. From the terminal window, within mystore, install the dependencies by running the following command:

    yarn install
    
  7. Start the angular client app. From the terminal window, within mystore start the application by running the following command:

    yarn start
    
  8. Make sure your backend server is up and running (SAP Commerce with TUA). When the backend server is properly started, point your browser to http://localhost:4200/telcospa/en/USD/.

Note:

(1) Using ~ instructs yarn to use the latest patch version (x.y.z); whereas, using ^ instructs yarn to use the latest minor version (x.y.0).

(2) If you are updating an existing app, and changing dependencies, it is recommended to delete the node_modules folder before running the install command.

Adding Import Declarations and Storefront Configuration Settings

Note: Some statements in the preceding example were generated by Angular when you first created the app.

About the Import Statements

The import statements import either modules or default data needed by TUA Spartacus.

Building and Starting

This section describes how to validate your back-end installation and start the application with the storefront enabled.

Validating the SAP Commerce Back End

Note: The Chrome browser is recommended and used in the following example, but other browsers can be used as long as they recognize and allow you to continue even though a site is using a self-signed certificate.

  1. Use a web browser to access the OCC endpoint of your backend.

    The default is available at: {server-base-url}/rest/v2/electronics/cms/pages

    For example, you can access: https://localhost:9002/rest/v2/telcospa/cms/pages with a back-end instance running from https://localhost:9002.

  2. Accept the security exception in your browser if you are running a development instance with a self-signed HTTPS certificate.

    When the request works, you will see an XML response in your browser.

Starting the Storefront Application

To start your TUA Spartacus storefront, enter the following command from mystore in your terminal window:

yarn start

When the app server is properly started, point your browser to http://localhost:4200.

Or, to start your TUA Spartacus storefront securely, enter the following command:

yarn start --ssl

Then point your browser to https://localhost:4200.