File

projects/storefrontlib/cms-components/myaccount/address-book/address-book.component.ts

Implements

OnInit

Metadata

selector cx-address-book
templateUrl ./address-book.component.html

Index

Properties
Methods

Constructor

constructor(service: AddressBookComponentService, translation: TranslationService)
Parameters :
Name Type Optional
service AddressBookComponentService No
translation TranslationService No

Methods

addAddressButtonHandle
addAddressButtonHandle()
Returns : void
addAddressCancel
addAddressCancel()
Returns : void
addAddressSubmit
addAddressSubmit(address: Address)
Parameters :
Name Type Optional
address Address No
Returns : void
cancelCard
cancelCard()
Returns : void
deleteAddress
deleteAddress(addressId: string)
Parameters :
Name Type Optional
addressId string No
Returns : void
editAddressButtonHandle
editAddressButtonHandle(address: Address)
Parameters :
Name Type Optional
address Address No
Returns : void
editAddressCancel
editAddressCancel()
Returns : void
editAddressSubmit
editAddressSubmit(address: Address)
Parameters :
Name Type Optional
address Address No
Returns : void
getCardContent
getCardContent(address: Address)
Parameters :
Name Type Optional
address Address No
Returns : any
ngOnInit
ngOnInit()
Returns : void
setAddressAsDefault
setAddressAsDefault(addressId: string)
Parameters :
Name Type Optional
addressId string No
Returns : void
setEdit
setEdit(addressId: string)
Parameters :
Name Type Optional
addressId string No
Returns : void

Properties

addresses$
Type : Observable<Address[]>
addressesStateLoading$
Type : Observable<boolean>
cards$
Type : Observable<Card[]>
currentAddress
Type : Address
editCard
Type : string
Public service
Type : AddressBookComponentService
showAddAddressForm
Default value : false
showEditAddressForm
Default value : false
import { Component, OnInit } from '@angular/core';
import { Address, TranslationService } from '@spartacus/core';
import { combineLatest, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Card } from '../../../shared/components/card';
import { AddressBookComponentService } from './address-book.component.service';

@Component({
  selector: 'cx-address-book',
  templateUrl: './address-book.component.html',
})
export class AddressBookComponent implements OnInit {
  addresses$: Observable<Address[]>;
  cards$: Observable<Card[]>;
  addressesStateLoading$: Observable<boolean>;
  currentAddress: Address;

  showAddAddressForm = false;
  showEditAddressForm = false;
  editCard: string;

  constructor(
    public service: AddressBookComponentService,
    protected translation: TranslationService
  ) {}

  ngOnInit(): void {
    this.addresses$ = this.service.getAddresses();
    this.addressesStateLoading$ = this.service.getAddressesStateLoading();
    this.service.loadAddresses();
  }

  addAddressButtonHandle(): void {
    this.showEditAddressForm = false;
    this.showAddAddressForm = true;
  }

  editAddressButtonHandle(address: Address): void {
    this.showAddAddressForm = false;
    this.showEditAddressForm = true;
    this.currentAddress = address;
  }

  addAddressSubmit(address: Address): void {
    this.showAddAddressForm = false;
    this.service.addUserAddress(address);
  }

  addAddressCancel(): void {
    this.showAddAddressForm = false;
  }

  editAddressSubmit(address: Address): void {
    this.showEditAddressForm = false;
    this.service.updateUserAddress(this.currentAddress['id'], address);
  }

  editAddressCancel(): void {
    this.showEditAddressForm = false;
  }

  getCardContent(address: Address) {
    return combineLatest([
      this.translation.translate('addressCard.default'),
      this.translation.translate('addressCard.setAsDefault'),
      this.translation.translate('common.delete'),
      this.translation.translate('common.edit'),
      this.translation.translate('addressBook.areYouSureToDeleteAddress'),
    ]).pipe(
      map(
        ([
          defaultText,
          setAsDefaultText,
          textDelete,
          textEdit,
          textVerifyDeleteMsg,
        ]) => {
          let region = '';

          if (address.region && address.region.isocode) {
            region = address.region.isocode + ', ';
          }

          const actions: { name: string; event: string }[] = [];
          if (!address.defaultAddress) {
            actions.push({ name: setAsDefaultText, event: 'default' });
          }
          actions.push({ name: textEdit, event: 'edit' });
          actions.push({ name: textDelete, event: 'delete' });

          return {
            textBold: address.firstName + ' ' + address.lastName,
            text: [
              address.line1,
              address.line2,
              address.town + ', ' + region + address.country.isocode,
              address.postalCode,
              address.phone,
            ],
            actions: actions,
            header: address.defaultAddress ? `✓ ${defaultText}` : '',
            deleteMsg: textVerifyDeleteMsg,
          };
        }
      )
    );
  }

  setAddressAsDefault(addressId: string): void {
    this.service.setAddressAsDefault(addressId);
  }

  deleteAddress(addressId: string): void {
    this.service.deleteUserAddress(addressId);
  }

  setEdit(addressId: string): void {
    if (this.editCard !== addressId) {
      this.editCard = addressId;
    } else {
      this.deleteAddress(addressId);
    }
  }

  cancelCard(): void {
    this.editCard = null;
  }
}
<div class="cx-section">
  <ng-container
    *ngIf="
      (addresses$ | async).length &&
      !(showAddAddressForm || showEditAddressForm)
    "
  >
    <div class="row">
      <div class="col-md-6">
        <button
          class="btn btn-block btn-action"
          (click)="addAddressButtonHandle()"
        >
          {{ 'addressBook.addNewAddress' | cxTranslate }}
        </button>
      </div>
    </div>

    <div
      class="row cx-address-deck"
      *ngIf="!(addressesStateLoading$ | async); else loading"
    >
      <div
        *ngFor="let address of addresses$ | async"
        class="col-md-6 cx-address-card"
      >
        <cx-card
          [border]="true"
          [fitToContainer]="true"
          [content]="getCardContent(address) | async"
          (editCard)="editAddressButtonHandle(address)"
          (setDefaultCard)="setAddressAsDefault(address.id)"
          (deleteCard)="setEdit(address.id)"
          [editMode]="address.id === editCard"
          (cancelCard)="cancelCard()"
        ></cx-card>
      </div>
    </div>
  </ng-container>

  <ng-container *ngIf="!(addresses$ | async).length || showAddAddressForm">
    <section>
      <p class="cx-section-msg">
        {{ 'addressBook.addNewShippingAddress' | cxTranslate }}
      </p>
      <cx-address-form
        class="cx-form"
        showTitleCode="true"
        [showCancelBtn]="!((addresses$ | async).length === 0)"
        actionBtnLabel="{{ 'addressBook.addAddress' | cxTranslate }}"
        cancelBtnLabel="{{ 'addressBook.backToAddressList' | cxTranslate }}"
        [setAsDefaultField]="!((addresses$ | async).length === 0)"
        (submitAddress)="addAddressSubmit($event)"
        (backToAddress)="addAddressCancel()"
        (cancelCard)="cancelCard()"
      ></cx-address-form>
    </section>
  </ng-container>

  <ng-container *ngIf="showEditAddressForm">
    <section>
      <p class="cx-section-msg">
        {{ 'addressBook.editShippingAddress' | cxTranslate }}
      </p>
      <cx-address-form
        showTitleCode="true"
        actionBtnLabel="{{ 'addressBook.updateAddress' | cxTranslate }}"
        cancelBtnLabel="{{ 'addressBook.backToAddressList' | cxTranslate }}"
        [addressData]="currentAddress"
        (submitAddress)="editAddressSubmit($event)"
        (backToAddress)="editAddressCancel()"
      ></cx-address-form>
    </section>
  </ng-container>
</div>

<ng-template #loading>
  <div class="col-md-12 cx-address-spinner">
    <cx-spinner></cx-spinner>
  </div>
</ng-template>
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""