File

feature-libs/checkout/components/components/checkout-login/checkout-login.component.ts

Implements

OnDestroy

Metadata

selector cx-checkout-login
templateUrl ./checkout-login.component.html

Index

Properties
Methods

Constructor

constructor(formBuilder: FormBuilder, authRedirectService: AuthRedirectService, activeCartService: ActiveCartService)
Parameters :
Name Type Optional
formBuilder FormBuilder No
authRedirectService AuthRedirectService No
activeCartService ActiveCartService No

Methods

ngOnDestroy
ngOnDestroy()
Returns : void
onSubmit
onSubmit()
Returns : void

Properties

checkoutLoginForm
Type : FormGroup
Default value : this.formBuilder.group( { email: ['', [Validators.required, CustomFormValidators.emailValidator]], emailConfirmation: ['', [Validators.required]], }, { validators: CustomFormValidators.emailsMustMatch( 'email', 'emailConfirmation' ), } )
sub
Type : Subscription
import { Component, OnDestroy } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ActiveCartService, AuthRedirectService } from '@spartacus/core';
import { CustomFormValidators } from '@spartacus/storefront';
import { Subscription } from 'rxjs';

@Component({
  selector: 'cx-checkout-login',
  templateUrl: './checkout-login.component.html',
})
export class CheckoutLoginComponent implements OnDestroy {
  checkoutLoginForm: FormGroup = this.formBuilder.group(
    {
      email: ['', [Validators.required, CustomFormValidators.emailValidator]],
      emailConfirmation: ['', [Validators.required]],
    },
    {
      validators: CustomFormValidators.emailsMustMatch(
        'email',
        'emailConfirmation'
      ),
    }
  );
  sub: Subscription;

  constructor(
    protected formBuilder: FormBuilder,
    protected authRedirectService: AuthRedirectService,
    protected activeCartService: ActiveCartService
  ) {}

  onSubmit() {
    if (this.checkoutLoginForm.valid) {
      const email = this.checkoutLoginForm.get('email')?.value;
      this.activeCartService.addEmail(email);

      if (!this.sub) {
        this.sub = this.activeCartService.getAssignedUser().subscribe(() => {
          if (this.activeCartService.isGuestCart()) {
            this.authRedirectService.redirect();
          }
        });
      }
    } else {
      this.checkoutLoginForm.markAllAsTouched();
    }
  }

  ngOnDestroy() {
    if (this.sub) {
      this.sub.unsubscribe();
    }
  }
}
<form (ngSubmit)="onSubmit()" [formGroup]="checkoutLoginForm">
  <div class="form-group">
    <label>
      <span class="label-content">{{
        'checkoutLogin.emailAddress.label' | cxTranslate
      }}</span>
      <input
        aria-required="true"
        type="email"
        name="email"
        class="form-control"
        formControlName="email"
        placeholder="{{
          'checkoutLogin.emailAddress.placeholder' | cxTranslate
        }}"
      />
      <cx-form-errors
        aria-live="assertive"
        aria-atomic="true"
        [control]="checkoutLoginForm.get('email')"
      ></cx-form-errors>
    </label>
  </div>

  <div class="form-group">
    <label>
      <span class="label-content">{{
        'checkoutLogin.confirmEmail.label' | cxTranslate
      }}</span>
      <input
        aria-required="true"
        type="email"
        name="emailConfirmation"
        class="form-control"
        formControlName="emailConfirmation"
        placeholder="{{
          'checkoutLogin.confirmEmail.placeholder' | cxTranslate
        }}"
      />
      <cx-form-errors
        aria-live="assertive"
        aria-atomic="true"
        [control]="checkoutLoginForm.get('emailConfirmation')"
      ></cx-form-errors>
    </label>
  </div>

  <button type="submit" class="btn btn-block btn-primary">
    {{ 'checkoutLogin.continue' | cxTranslate }}
  </button>
</form>
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""