File

src/app/plugins/profile/components/account-recovery-info/account-recovery-info.component.ts

Implements

OnInit OnDestroy

Metadata

Index

Properties
Methods
Inputs
Outputs

Constructor

constructor(resourceService: ResourceService, profileService: ProfileService, userService: UserService, matDialog: MatDialog, toasterService: ToasterService, otpService: OtpService, configService: ConfigService)
Parameters :
Name Type Optional
resourceService ResourceService No
profileService ProfileService No
userService UserService No
matDialog MatDialog No
toasterService ToasterService No
otpService OtpService No
configService ConfigService No

Inputs

dialogProps
Type : any
mode
Type : string

to take the mode of operaion (edit or add of recovery id) from profile page

userProfile
Type : any

Outputs

close
Type : EventEmitter

Methods

closeMatDialog
closeMatDialog()
Returns : void
closeModal
closeModal()
Returns : void
generateOTP
generateOTP(request)
Parameters :
Name Optional
request No
Returns : void
handleSubmitButton
handleSubmitButton()

to handle enable/disable functionality of submit button

Returns : void
initializeFormFields
initializeFormFields()

to initialize form fields

Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void
onItemChange
onItemChange()

to initialize form fields each time when radio button will be selected/changed

Returns : void
setTelemetryData
setTelemetryData()

To prepare telemetry data

Returns : void
updateRecoveryId
updateRecoveryId()

to add/update the recovery id

Returns : void
userVerificationSuccess
userVerificationSuccess()
Returns : void
validateAndEditContact
validateAndEditContact()
Returns : void

Properties

accountRecoveryForm
Type : UntypedFormGroup
contactType
Type : string

to store the type of contact (email or phone)

duplicateRecoveryId
Type : boolean
enableSubmitButton
Default value : false
otpData
Type : any
Public otpService
Type : OtpService
Public profileService
Type : ProfileService
request
Type : literal type

to store the request

Public resourceService
Type : ResourceService
showOTPForm
Type : boolean
submitInteractEdata
Type : IInteractEventEdata
telemetryCdata
Type : Array<literal type>
Default value : []
telemetryInteractObject
Type : IInteractEventObject

telemetry

Public toasterService
Type : ToasterService
Public userService
Type : UserService
import { UserService, OtpService } from '@sunbird/core';
import { IInteractEventObject, IInteractEventEdata } from '@sunbird/telemetry';
import { UntypedFormGroup, UntypedFormControl, Validators } from '@angular/forms';
import { Component, OnInit, Output, EventEmitter, Input, OnDestroy } from '@angular/core';
import { ResourceService, ToasterService, ServerResponse, ConfigService } from '@sunbird/shared';
import { ProfileService } from './../../services';
import * as _ from 'lodash-es';
import { MatDialog } from '@angular/material/dialog';
@Component({
  selector: 'app-account-recovery-info',
  templateUrl: './account-recovery-info.component.html',
  styleUrls: ['./account-recovery-info.component.scss']
})
export class AccountRecoveryInfoComponent implements OnInit, OnDestroy {
  @Output() close = new EventEmitter<any>();
  @Input() dialogProps;

  /** to take the mode of operaion (edit or add of recovery id) from profile page */
  @Input() mode: string;
  @Input() userProfile: any;
  accountRecoveryForm: UntypedFormGroup;
  enableSubmitButton = false;

  /** to store the type of contact (email or phone) */
  contactType: string;

  /** to store the request */
  request: {};

  /** telemetry */
  telemetryInteractObject: IInteractEventObject;
  submitInteractEdata: IInteractEventEdata;
  telemetryCdata: Array<{}> = [];
  duplicateRecoveryId: boolean;
  showOTPForm: boolean;
  otpData: any;

  constructor(
    public resourceService: ResourceService,
    public profileService: ProfileService,
    public userService: UserService,
    private matDialog: MatDialog,
    public toasterService: ToasterService,
    public otpService: OtpService,
    private configService: ConfigService) { }

  ngOnInit() {
    this.contactType = 'emailId';
    this.validateAndEditContact();
  }

  /** to initialize form fields */
  initializeFormFields() {
    if (this.contactType === 'emailId') {
      this.accountRecoveryForm = new UntypedFormGroup({
        email: new UntypedFormControl('', [Validators.required, Validators.pattern(/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[a-z]{2,4}$/)]),
      });
    } else if (this.contactType === 'phoneNo') {
      this.accountRecoveryForm = new UntypedFormGroup({
        phone: new UntypedFormControl('', [Validators.required, Validators.pattern(/^[6-9]\d{9}$/)]),
      });
    }
    this.handleSubmitButton();
    this.setTelemetryData();
  }

  validateAndEditContact() {
    if (this.userProfile) {
        const request: any = {
            key: this.userProfile.email || this.userProfile.phone || this.userProfile.recoveryEmail,
            userId: this.userProfile.userId,
            templateId: this.configService.appConfig.OTPTemplate.updateContactTemplate,
            type: ''
        };
        if ((this.userProfile.email && !this.userProfile.phone) ||
        (!this.userProfile.email && !this.userProfile.phone && this.userProfile.recoveryEmail)) {
            request.type = 'email';
        } else if (this.userProfile.phone || this.userProfile.recoveryPhone) {
            request.type = 'phone';
        }
        this.otpData = {
            'type': request.type,
            'value': request.key,
            'instructions': this.resourceService.frmelmnts.lbl.otpcontactinfo,
            'retryMessage': request.type === 'phone' ?
              this.resourceService.frmelmnts.lbl.unableToUpdateMobile : this.resourceService.frmelmnts.lbl.unableToUpdateEmail,
            'wrongOtpMessage': request.type === 'phone' ? this.resourceService.frmelmnts.lbl.wrongPhoneOTP :
              this.resourceService.frmelmnts.lbl.wrongEmailOTP
        };
        this.showOTPForm = true;
        this.generateOTP({ request });
    }
  }

  generateOTP(request) {
    console.log('Request', request);
    this.otpService.generateOTP(request).subscribe(
      (data: ServerResponse) => {
      },
      (err) => {
        const failedgenerateOTPMessage = (err.error.params.status === 'PHONE_ALREADY_IN_USE') ||
          (err.error.params.status === 'EMAIL_IN_USE') ? err.error.params.errmsg : this.resourceService.messages.fmsg.m0051;
        this.toasterService.error(failedgenerateOTPMessage);
        this.closeModal();
      }
    );
  }

  userVerificationSuccess() {
    this.initializeFormFields();
    this.showOTPForm = false;
  }

  /** to add/update the recovery id */
  updateRecoveryId() {
    this.enableSubmitButton = false;
    if (this.contactType === 'emailId') {
      this.request = {
        'recoveryEmail': this.accountRecoveryForm.get('email').value,
        'recoveryPhone': ''
      };
    } else if (this.contactType === 'phoneNo') {
      this.request = {
        'recoveryPhone': this.accountRecoveryForm.get('phone').value,
        'recoveryEmail': ''
      };
    }
    this.profileService.updateProfile(this.request).subscribe((data) => {
      this.closeModal();
    }, (error) => {
      if (_.get(error, 'error.params.err') === 'RECOVERY_PARAM_MATCH_EXCEPTION') {
        this.duplicateRecoveryId = true;
        this.accountRecoveryForm.reset();
      } else {
        this.toasterService.error(this.resourceService.messages.fmsg.m0051);
        this.closeModal();
      }
    });
  }

  /** to handle enable/disable functionality of submit button */
  handleSubmitButton() {
    this.enableSubmitButton = false;
    this.accountRecoveryForm.valueChanges.subscribe(val => {
      this.enableSubmitButton = (this.accountRecoveryForm.status === 'VALID');
    });
  }

  ngOnDestroy() {
    this.closeMatDialog();
  }

  /** to initialize form fields each time when radio button will be selected/changed */
  onItemChange() {
    this.duplicateRecoveryId = false;
    this.initializeFormFields();
  }

  closeModal() {
    this.closeMatDialog();
    this.close.emit();
  }

  /** To prepare telemetry data */
  setTelemetryData() {
    const id = this.contactType === 'phoneNo' ?
      'submit-phone-recovery' : 'submit-emailId-recovery';
    this.submitInteractEdata = {
      id: id,
      type: 'click',
      pageid: 'profile-read'
    };

    this.telemetryCdata = [
      {
        id: 'user:account:recovery',
        type: 'Feature'
      },
      {
        id: 'SC-1288',
        type: 'Task'
      }
    ];

    this.telemetryInteractObject = {
      id: this.userService.userid,
      type: 'User',
      ver: '1.0'
    };
  }
  closeMatDialog() {
    const dialogRef = this.dialogProps && this.dialogProps.id && this.matDialog.getDialogById(this.dialogProps.id);
    dialogRef && dialogRef.close();
  }
}
<div class="sb-mat__modal">

  <div mat-dialog-title class="mb-0">
    <button aria-label="close dialog" mat-dialog-close class="close-btn"></button>
  </div>

  <div *ngIf="!showOTPForm">

    <!--Content-->

    <mat-dialog-content>

      <div class="sb-mat__modal__content">

        <!--Header-->
        <div class="sb-modal-header font-weight-bold fmedium">
          <span *ngIf="mode === 'edit'">{{resourceService?.frmelmnts?.lbl?.updateRecoveryId}}</span>
          <span *ngIf="mode === 'add'">{{resourceService?.frmelmnts?.lbl?.addRecoveryId}}</span>
        </div>
        <!--/Header-->

        <p>{{resourceService?.frmelmnts?.lbl?.accountRecoveryDescription}}</p>
        <div class="d-flex flex-w-wrap">
          <div class="sb-radio-btn-checkbox sb-radio-btn-primary">
            <input type="radio" id="email" tabindex="0" (change)="onItemChange()" [(ngModel)]="contactType"
              name="contactType" value="emailId" checked>
            <label for="email">{{resourceService?.frmelmnts?.lbl?.emailAddress}}</label>
          </div>
          <div class="sb-radio-btn-checkbox sb-radio-btn-primary">
            <input type="radio" tabindex="0" id="phone" (change)="onItemChange()" [(ngModel)]="contactType"
              name="contactType" value="phoneNo">
            <label for="phone">{{resourceService?.frmelmnts?.lbl?.phoneNumber}}</label>
          </div>
        </div>
        <form [formGroup]="accountRecoveryForm" class="sb-form">
          <div class="sb-field" *ngIf="contactType === 'emailId'">
            <input class="sb-form-control mb-8" formControlName="email" name="email" id="email" type="email">
            <span class="sb-color-error fnormal mt-8"
              *ngIf="accountRecoveryForm.controls.email.dirty && accountRecoveryForm.controls['email'].errors">
              {{resourceService?.frmelmnts?.lbl?.validEmail}}
            </span>
            <span *ngIf="duplicateRecoveryId" class="sb-color-error fnormal mt-8">
              {{resourceService?.frmelmnts?.lbl?.duplicateEmailAddress}}
            </span>
          </div>

          <div class="sb-field" *ngIf="contactType === 'phoneNo'">
            <input maxlength="10" class="sb-form-control mb-8" formControlName="phone" type="tel">
            <span class="sb-color-error fnormal mt-8"
              *ngIf="accountRecoveryForm.controls.phone.dirty && accountRecoveryForm.controls['phone'].errors">
              {{resourceService?.frmelmnts?.prmpt?.enterphoneno}}
            </span>
            <span *ngIf="duplicateRecoveryId" class="sb-color-error fnormal mt-8">
              {{resourceService?.frmelmnts?.lbl?.duplicatePhoneNumber}}
            </span>
          </div>
        </form>
      </div>
    </mat-dialog-content>
    <!--/Content-->

    <!--Actions-->
    <div class="sb-mat__modal__actions">
      <button [ngClass]="{'sb-btn-disabled': !enableSubmitButton, 'sb-btn-primary': enableSubmitButton}"
        class="sb-btn sb-btn-normal" tabindex="0" (click)="updateRecoveryId()" appTelemetryInteract
        [telemetryInteractObject]="telemetryInteractObject" [disabled]="!enableSubmitButton"
        [telemetryInteractEdata]="submitInteractEdata" [telemetryInteractCdata]="telemetryCdata"
        [attr.disabled]="!enableSubmitButton ? 'disabled' : null">
        {{resourceService?.frmelmnts?.btn?.submit}}
      </button>
    </div>
    <!--/Actions-->
  </div>


  <app-otp-popup *ngIf="showOTPForm" [otpData]="otpData" [redirectToLogin]="false"
    (verificationSuccess)="userVerificationSuccess($event)" (closeContactForm)="closeModal()">
  </app-otp-popup>

</div>

./account-recovery-info.component.scss

Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""