src/app/plugins/profile/components/update-contact-details/update-contact-details.component.ts
OnInit
OnDestroy
selector | app-update-contact-details |
styleUrls | ./update-contact-details.component.scss |
templateUrl | ./update-contact-details.component.html |
Properties |
|
Methods |
Inputs |
Outputs |
constructor(resourceService: ResourceService, userService: UserService, otpService: OtpService, toasterService: ToasterService, profileService: ProfileService, matDialog: MatDialog, configService: ConfigService)
|
||||||||||||||||||||||||
Parameters :
|
contactType | |
Type : string
|
|
dialogProps | |
Type : any
|
|
userProfile | |
Type : any
|
|
close | |
Type : EventEmitter
|
|
closeMatDialog |
closeMatDialog()
|
Returns :
void
|
closeModal |
closeModal()
|
Returns :
void
|
enableSubmitButton |
enableSubmitButton()
|
Returns :
void
|
generateOTP | ||||||
generateOTP(request?, otpData?)
|
||||||
Parameters :
Returns :
void
|
initializeFormFields |
initializeFormFields()
|
Returns :
void
|
ngOnDestroy |
ngOnDestroy()
|
Returns :
void
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
onContactValueChange |
onContactValueChange()
|
Returns :
void
|
onSubmitForm |
onSubmitForm()
|
Returns :
void
|
prepareOtpData | ||||
prepareOtpData(otpData?)
|
||||
Parameters :
Returns :
void
|
setInteractEventData |
setInteractEventData()
|
Returns :
void
|
showParentForm | ||||
showParentForm(event)
|
||||
Parameters :
Returns :
void
|
updateProfile | ||||
updateProfile(data)
|
||||
Parameters :
Returns :
void
|
vaidateUserContact |
vaidateUserContact()
|
Returns :
void
|
Private Async validateAndEditContact |
validateAndEditContact()
|
Returns :
any
|
Public configService |
Type : ConfigService
|
contactTypeForm |
Type : UntypedFormGroup
|
enableSubmitBtn |
Default value : false
|
otpData |
Type : any
|
Public otpService |
Type : OtpService
|
Public profileService |
Type : ProfileService
|
Public resourceService |
Type : ResourceService
|
showForm |
Default value : false
|
showUniqueError |
Type : string
|
Default value : ''
|
submitInteractEdata |
Type : IInteractEventEdata
|
telemetryInteractObject |
Type : IInteractEventObject
|
templateId |
Type : any
|
Default value : 'otpContactUpdateTemplate'
|
Public toasterService |
Type : ToasterService
|
Public unsubscribe |
Default value : new Subject<void>()
|
Public userService |
Type : UserService
|
verifiedUser |
Default value : false
|
import { Component, OnInit, OnDestroy, Input, Output, EventEmitter } from '@angular/core';
import { Validators, UntypedFormGroup, UntypedFormControl } from '@angular/forms';
import * as _ from 'lodash-es';
import { UserService, OtpService } from '@sunbird/core';
import { ResourceService, ServerResponse, ToasterService, ConfigService } from '@sunbird/shared';
import { Subject } from 'rxjs';
import { ProfileService } from './../../services';
import { IInteractEventObject, IInteractEventEdata } from '@sunbird/telemetry';
import { MatDialog } from '@angular/material/dialog';
@Component({
selector: 'app-update-contact-details',
templateUrl: './update-contact-details.component.html',
styleUrls: ['./update-contact-details.component.scss']
})
export class UpdateContactDetailsComponent implements OnInit, OnDestroy {
public unsubscribe = new Subject<void>();
@Input() contactType: string;
@Input() userProfile: any;
@Output() close = new EventEmitter<any>();
@Input() dialogProps;
contactTypeForm: UntypedFormGroup;
enableSubmitBtn = false;
showUniqueError = '';
showForm = false;
// @ViewChild('contactTypeModal', {static: true}) contactTypeModal;
otpData: any;
submitInteractEdata: IInteractEventEdata;
telemetryInteractObject: IInteractEventObject;
verifiedUser = false;
templateId: any = 'otpContactUpdateTemplate';
constructor(public resourceService: ResourceService, public userService: UserService,
public otpService: OtpService, public toasterService: ToasterService,
public profileService: ProfileService, private matDialog: MatDialog,
public configService: ConfigService) { }
ngOnInit() {
this.validateAndEditContact();
}
initializeFormFields() {
if (this.contactType === 'phone') {
this.contactTypeForm = new UntypedFormGroup({
phone: new UntypedFormControl('', [Validators.required , Validators.pattern(/^[6-9]\d{9}$/)]),
uniqueContact: new UntypedFormControl(null, [Validators.required])
});
this.onContactValueChange();
} else if (this.contactType === 'email') {
this.contactTypeForm = new UntypedFormGroup({
email: new UntypedFormControl('', [Validators.required, Validators.pattern(/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[a-z]{2,4}$/)]),
uniqueContact: new UntypedFormControl(null, [Validators.required])
});
this.onContactValueChange();
}
this.enableSubmitButton();
this.setInteractEventData();
}
private async 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';
}
const 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.verifiedUser = false;
this.generateOTP({ request }, otpData);
}
}
closeModal() {
this.closeMatDialog();
this.close.emit();
}
enableSubmitButton() {
this.contactTypeForm.valueChanges.subscribe(val => {
this.enableSubmitBtn = (this.contactTypeForm.status === 'VALID');
});
}
showParentForm(event) {
if (event === 'true') {
this.initializeFormFields();
this.showForm = true;
}
}
prepareOtpData(otpData?) {
this.otpData = otpData || {
'type': this.contactType.toString(),
'value': this.contactType === 'phone' ?
this.contactTypeForm.controls.phone.value.toString() : this.contactTypeForm.controls.email.value,
'instructions': this.contactType === 'phone' ?
this.resourceService.frmelmnts.instn.t0083 : this.resourceService.frmelmnts.instn.t0084,
'retryMessage': this.contactType === 'phone' ?
this.resourceService.frmelmnts.lbl.unableToUpdateMobile : this.resourceService.frmelmnts.lbl.unableToUpdateEmail,
'wrongOtpMessage': this.contactType === 'phone' ? this.resourceService.frmelmnts.lbl.wrongPhoneOTP :
this.resourceService.frmelmnts.lbl.wrongEmailOTP
};
}
vaidateUserContact() {
const value = this.contactType === 'phone' ?
this.contactTypeForm.controls.phone.value.toString() : this.contactTypeForm.controls.email.value;
const uri = this.contactType + '/' + value;
this.userService.getIsUserExistsUserByKey(uri).subscribe(
(data: ServerResponse) => {
if (this.userService.userid === data.result.response.id) {
this.showUniqueError = this.contactType === 'phone' ?
this.resourceService.frmelmnts.lbl.samePhoneNo : this.resourceService.frmelmnts.lbl.sameEmailId;
} else {
this.showUniqueError = this.contactType === 'phone' ?
this.resourceService.frmelmnts.lbl.uniqueMobile : this.resourceService.frmelmnts.lbl.uniqueEmailId;
}
},
(err) => {
if (_.get(err, 'error.params.status') && err.error.params.status === 'USER_ACCOUNT_BLOCKED') {
this.showUniqueError = this.resourceService.frmelmnts.lbl.blockedUserError;
} else {
this.contactTypeForm.controls['uniqueContact'].setValue(true);
this.showUniqueError = '';
}
}
);
}
onContactValueChange() {
const contactTypeControl = this.contactType === 'phone' ?
this.contactTypeForm.get('phone') : this.contactTypeForm.get('email');
let contactValue = '';
contactTypeControl.valueChanges.subscribe(
(data: string) => {
if (contactTypeControl.status === 'VALID' && contactValue !== contactTypeControl.value) {
this.contactTypeForm.controls['uniqueContact'].setValue('');
this.vaidateUserContact();
contactValue = contactTypeControl.value;
}
});
}
onSubmitForm() {
this.enableSubmitBtn = false;
if (this.contactTypeForm.valid) {
this.generateOTP();
}
}
generateOTP(request?, otpData?) {
if (!request) {
request = {
'request': {
'key': this.contactType === 'phone' ?
this.contactTypeForm.controls.phone.value.toString() : this.contactTypeForm.controls.email.value,
'type': this.contactType.toString()
}
};
}
this.otpService.generateOTP(request).subscribe(
(data: ServerResponse) => {
this.prepareOtpData(otpData);
this.showForm = false;
},
(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.enableSubmitBtn = true;
if (!this.verifiedUser) {
this.closeModal();
}
}
);
}
updateProfile(data) {
if (this.verifiedUser) {
this.profileService.updateProfile(data).subscribe(res => {
this.closeModal();
const sMessage = this.contactType === 'phone' ?
this.resourceService.messages.smsg.m0047 : this.resourceService.messages.smsg.m0048;
this.toasterService.success(sMessage);
}, err => {
this.closeModal();
const fMessage = this.contactType === 'phone' ?
this.resourceService.messages.emsg.m0014 : this.resourceService.messages.emsg.m0015;
this.toasterService.error(fMessage);
});
} else {
this.initializeFormFields();
this.verifiedUser = true;
this.showForm = true;
}
}
setInteractEventData() {
const id = this.contactType === 'phone' ?
'submit-mobile' : 'submit-emailId';
this.submitInteractEdata = {
id: id,
type: 'click',
pageid: 'profile-read'
};
this.telemetryInteractObject = {
id: this.userService.userid,
type: 'User',
ver: '1.0'
};
}
ngOnDestroy() {
this.unsubscribe.next();
this.unsubscribe.complete();
this.closeMatDialog();
}
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>
<ng-container *ngIf="showForm">
<!--Content-->
<mat-dialog-content>
<div class="sb-mat__modal__content">
<!--Header-->
<div class="sb-modal-header font-weight-bold fmedium" [ngSwitch]="contactType" role="heading" aria-level="2">
<span *ngSwitchCase="'email'">{{resourceService?.frmelmnts?.lbl?.updateEmailId}}</span>
<span *ngSwitchCase="'phone'">{{resourceService?.frmelmnts?.lbl?.updatePhoneNo}}</span>
</div>
<!--/Header-->
<form class="sb-form borderd" [formGroup]="contactTypeForm">
<div [ngSwitch]="contactType">
<div class="sb-field-group" *ngSwitchCase="'email'">
<label>{{resourceService?.frmelmnts?.prmpt?.enteremailID}}</label>
<div class="sb-field ui input">
<input class="sb-form-control" formControlName="email" type="text"
placeholder="{{ resourceService?.frmelmnts?.lbl?.enterEmail}}">
</div>
<label class="ui basic blue error label pt-0"
*ngIf="contactTypeForm.controls.email.dirty && contactTypeForm.controls['email'].errors">{{resourceService?.frmelmnts?.lbl?.validEmail}}</label>
<label class="ui basic blue error label pt-0"
*ngIf="showUniqueError && !(contactTypeForm.controls.email.touched && contactTypeForm.controls['email'].errors)">{{showUniqueError}}</label>
</div>
<div class="sb-field-group" *ngSwitchCase="'phone'">
<label>{{resourceService?.frmelmnts?.prmpt?.enterphoneno}}</label>
<div class="sb-field ui left icon input">
<input class="sb-form-control" formControlName="phone" type="number"
placeholder="{{resourceService?.frmelmnts?.lbl?.enterPhoneNumber}}">
<i class=" icon">{{resourceService?.frmelmnts?.lbl?.indPhoneCode}}-</i>
</div>
<label class="ui basic blue error label pt-0"
*ngIf="contactTypeForm.controls.phone.dirty && contactTypeForm.controls['phone'].errors">{{resourceService?.frmelmnts?.lbl?.validPhone}}</label>
<label class="ui basic blue error label pt-0"
*ngIf="showUniqueError && !(contactTypeForm.controls['phone'].errors)">{{showUniqueError}}</label>
</div>
</div>
</form>
</div>
</mat-dialog-content>
<!--/Content-->
<!--Actions-->
<div class="sb-mat__modal__actions text-center">
<button appTelemetryInteract [telemetryInteractObject]="telemetryInteractObject"
[telemetryInteractEdata]="submitInteractEdata" [ngClass]="{'sb-btn-disabled':!enableSubmitBtn}" (click)="onSubmitForm()"
tabindex="0" class="sb-btn sb-btn-normal sb-btn-primary">
{{resourceService?.frmelmnts?.btn?.submit}}
</button>
</div>
<!--/Actions-->
</ng-container>
<app-otp-popup *ngIf="!showForm" [otpData]="otpData" [redirectToLogin]="false"
(verificationSuccess)="updateProfile($event)" (closeContactForm)="closeModal()"
(redirectToParent)="showParentForm($event)">
</app-otp-popup>
</div>
<router-outlet></router-outlet>
./update-contact-details.component.scss
@use "@project-sunbird/sb-styles/assets/mixins/mixins" as *;
@use "@project-sunbird/sb-styles/assets/varSass" as *;
@use 'components/form' as *;
.ui.grey.button {
font-size: 1rem;
}
.ui.dodger-blue.button {
background: var(--primary-color);
color: var(--white);
font-size: 1rem;
height: calculateRem(38px) !important;
}
.ui.left.icon.input .icon {
font-size: 1rem;
}
.height-200 {
height: calculateRem(200px) !important;
}
.sb-field.ui.input {
display: block;
.icon {
left: 0;
top: 0.625rem;
}
.sb-form-control {
border: $sb-input-border-normal;
line-height: 1.5;
padding: 0.5714rem;
&:hover {
border: $sb-input-border-hover;
}
}
}