カスタム・ページのコンポーネントおよびリソースの作成

カスタム・ページのコンポーネントおよびリソースを作成する方法について説明します。 この演習では、必要なリソースを準備し、そのページのパンくずリストを作成します。

手順

  1. 端末を開き、 devtoolkit_docker/orderhub-code/buc-app-inventory/packages/inventory-search-results/src-custom/app/features ディレクトリーに移動します。
  2. 以下のコマンドを実行して、新規コンポーネントを作成します。
    ng g c create-reservation

    このコマンドは、カスタム・コンポーネントを自動的に生成し、 packages/inventory-search-results/src-custom/app/features/ext-search.module.ts ファイルを更新します。

  3. 生成したカスタム・コンポーネント (create-reservation) は IBM ユーティリティーおよびライブラリーを使用する必要があるため、以下のステップを実行して、コンポーネントを app-customization.impl.ts ファイルに移動します。
    1. 以下の変更を加えて、 src-custom/app/features/ext-search.module.ts ファイルを編集します。
      • import {CreateReservationComponent} ステートメントを削除します。
      • 消去CreateReservationComponentからdeclarations配列。
      結果のコードは以下のようになります。
      import { NgModule } from '@angular/core';
      import { CommonModule } from '@angular/common';
      
      @NgModule({
        declarations: [
        ],
        imports: [
          CommonModule
        ]
      })
      export class ExtSearchModule { }
    2. 以下のコードを使用して、 src-custom/app/app-customization.impl.ts ファイルを編集します。
      import { RouterModule, Routes } from '@angular/router';
      import { InventoryModuleUrlGuardServiceFn } from '@buc/inventory-shared';
      import { BucCommonClassesConnectorResourcePermissionsGuard } from '@buc/svc-angular';
      import { CreateReservationComponent } from './features/create-reservation/create-reservation.component';
      
      export const customRoutes: Routes = [
         {
          canActivate: [InventoryModuleUrlGuardServiceFn, BucCommonClassesConnectorResourcePermissionsGuard],
          path: 'create-reservation',
          component: CreateReservationComponent,
          data: {
            id: 'CUSINV0001' // New resourceId to control permissions
          }
        }
      ];
      export class AppCustomizationImpl {
          static readonly components = [CreateReservationComponent];
          static readonly providers = [];
          static readonly imports = [RouterModule.forChild(customRoutes)];
          static readonly exports = [RouterModule];
      }
      
      • このコードは、CreateReservationComponentコンポーネント配列に追加し、新しいコンポーネントに移動するためのカスタムルートを追加します。予約を作成ページ。
      • 経路内の data.id は、この resourceId 値を検査して、ユーザーがこのページを表示する権限を持っているかどうかを判別します。
  4. アプリケーション・コンソールで、前のステップで定義したカスタム・リソースを作成します。 (id: 'CUSINV0001')
    カスタム・リソースの作成方法について詳しくは、 カスタム・ページおよびアクションのリソース許可の作成の「ステップ 1」を参照してください。 リソース ID が CUSINV0001に設定されていることを確認します。
    「予約の作成」ページの新規エンティティーを含む Channel Apps Manager の画面キャプチャー
  5. カスタム・ユーザー・グループを更新して、 「予約の作成」 ページを表示する権限を付与します。 詳しくは、 Order Hub のリソース許可の定義を参照してください。
    「予約の作成」ページの新規エンティティーを含む Order Hub 許可モジュールの画面キャプチャー
  6. モジュールが正常にコンパイルされたら、 Order Hub に移動し、オーバーフロー・メニューをクリックして、新しく追加されたアクションを表示します。
    オーバーフロー・メニューの画面キャプチャーが開き、「予約の作成」ボタンが赤いボックスで強調表示されます。
  7. 「予約の作成」をクリックします。
    セットアップが成功すると、新しいページに「create-reservation works!」というテキストが表示されます。
  8. src-custom/app/features/create-reservation ディレクトリーに custom-constants.ts ファイルを作成します。
  9. 以下のコードを追加します。
    export class CustomConstants {
        static readonly ACTION_CREATE_RESERVATION = 'createReservation';
        static readonly ROUTE_CREATE_RESERVATION = '/buc-app-inventory/inventory-search-results/inventory/create-reservation';
        static readonly ACTION_IDS_CREATE_RESERVATION = 'create';
      }
    
  10. 以下のコードを使用して、 src-custom/app/app-customization.impl.ts ファイルを編集します。
    • インポート配列を更新して、ユーザーが 「予約の作成」 ページにナビゲートしたときにパンくずリストと入力ボックスの表示をサポートするモジュールを組み込みます。
      static readonly imports = [
      	RouterModule.forChild(customRoutes),
             BucCommonComponentsModule,
             BucFeatureComponentsModule,
             BucIconsModule,
             SharedModule];
      
    • 対応するモジュールをインポートします。
      import { BucCommonComponentsModule, BucFeatureComponentsModule, BucIconsModule } from '@buc/common-components';
      import { SharedModule } from '@buc/inventory-shared';
      
  11. src-custom/app/features/create-reservation/create-reservation.component.html ファイルを開きます。
  12. パンくずリストとタイトル・テキストを追加します。
    
    <div class="screen-loading" *ngIf="!initialized">
        <buc-loading [isActive]="!initialized"></buc-loading>
      </div>
      
      <ng-container *ngIf="initialized">
        <div class="screen-header">
          <!-- Bread crumb  -->
          <buc-page-header *ngIf="breadCrumbList" class="screen-breadcrumb" [breadcrumbList]="breadCrumbList" [headerOrder]="['breadcrumb']">
          </buc-page-header>
        </div>
      
        <div class="screen">
            <div class="cds--row">
              <div class="cds--col">
                <!-- Reservation form  -->
              
              </div>
            </div>
          <div class="cds--row">
                  <div class="cds--col">
                <!-- Reservation table -->
          
              </div>
            </div>
          </div>
          <div class="screen-footer">
         <!-- Cancel Save buttons footer -->
        
          </div>
        </ng-container>
    
  13. 以下の変換キーを src-custom/assets/custom/i18n/en.json ファイルに追加します。
    {
        "custom": {
        "LABEL_CREATE_RESERVATION":"Create reservation",
        "SUCCESS_RESERVATION":"Reservation successful",
        "ERROR_RESERVATION":"Reservation failed:"
        }
      }
    
  14. 以下の変更を src-custom/app/features/create-reservation/create-reservation.component.ts ファイルに追加します。
    import { Component, OnInit } from '@angular/core';
    import { ActivatedRoute } from '@angular/router';
    import { PostReservationRequest } from '@buc/common-components';
    import { BreadcrumbService } from '@buc/inventory-shared';
    import { TranslateService } from '@ngx-translate/core';
    import { CustomConstants } from './custom-constants';
    
    @Component({
      selector: 'buc-create-reservation',
      templateUrl: './create-reservation.component.html',
      styleUrls: ['./create-reservation.component.scss']
    })
    export class CreateReservationComponent implements OnInit {
      public initialized: boolean = false;
      public breadCrumbList = []; // breadCrumb list
      public placeholder = '';
      private nlsMap: any = {
        'custom.LABEL_CREATE_RESERVATION': '',
        'custom.LABEL_RESERVATION_EXPIRY': '',
        'custom.ERROR_INVALID_EXPIRY': ''
      };
    
      constructor(
        private breadcrumbSvc: BreadcrumbService,
        private translateSvc: TranslateService,
        private route: ActivatedRoute
      ) { }
    
      ngOnInit(): void {
        this._initTranslations();
        this._initBC();
        this.initialized = true;
    
      }
    
      /** Fetch translations for the page */
      private async _initTranslations() {
        const keys = Object.keys(this.nlsMap);
        const json = await this.translateSvc.get(keys).toPromise();
        keys.forEach(k => this.nlsMap[k] = json[k]);
      }
    
      /** Initialize breadcrumbs */
      private _initBC() {
        this.breadcrumbSvc.updateLast(this.nlsMap['custom.LABEL_CREATE_RESERVATION'], CustomConstants.ROUTE_CREATE_RESERVATION);
        this.breadCrumbList = this.breadcrumbSvc.get();
      }
    
    }
    
  15. 「Order Hub」 に戻り、フレームを再ロードします。
    「予約の作成」 ページに移動して、パンくずリストが表示されていることを確認します。
    パンくずリスト