File

src/app/plugins/profile/components/update-contact-details/update-contact-details.component.ts

Metadata

selector app-update-contact-details
styleUrls update-contact-details.component.scss
templateUrl update-contact-details.component.html

Inputs

contactType

Type: string

Outputs

close $event type: EventEmitter

Constructor

constructor(resourceService: any, userService: any, otpService: any, toasterService: any, profileService: ProfileService)

Methods

initializeFormFields
initializeFormFields()
Returns: void
closeModal
closeModal()
Returns: void
enableSubmitButton
enableSubmitButton()
Returns: void
showParentForm
showParentForm(event: any)
Returns: void
prepareOtpData
prepareOtpData()
Returns: void
vaidateUserContact
vaidateUserContact()
Returns: void
onContactValueChange
onContactValueChange()
Returns: void
onSubmitForm
onSubmitForm()
Returns: void
generateOTP
generateOTP()
Returns: void
updateProfile
updateProfile(data: any)
Returns: void
setInteractEventData
setInteractEventData()
Returns: void

Properties

contactTypeForm
contactTypeForm: any
contactTypeModal
contactTypeModal: any
enableSubmitBtn
enableSubmitBtn: boolean
Default value: false
otpData
otpData: any
otpService
otpService: any
profileService
profileService: ProfileService
resourceService
resourceService: any
showForm
showForm: boolean
Default value: true
showUniqueError
showUniqueError: string
submitInteractEdata
submitInteractEdata: any
telemetryInteractObject
telemetryInteractObject: any
toasterService
toasterService: any
Public unsubscribe
unsubscribe: Subject<void>
userService
userService: any
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { Component, OnInit, OnDestroy, Input, Output, EventEmitter, ViewChild } 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 } from '@sunbird/shared';
import { Subject } from 'rxjs';
import { ProfileService } from './../../services';
import { IInteractEventObject, IInteractEventEdata } from '@sunbird/telemetry';

@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;
  @Output() close = new EventEmitter<any>();
  contactTypeForm: UntypedFormGroup;
  enableSubmitBtn = false;
  showUniqueError = '';
  showForm = true;
  @ViewChild('contactTypeModal') contactTypeModal;
  otpData: any;
  submitInteractEdata: IInteractEventEdata;
  telemetryInteractObject: IInteractEventObject;

  constructor(public resourceService: ResourceService, public userService: UserService,
    public otpService: OtpService, public toasterService: ToasterService,
    public profileService: ProfileService) { }

  ngOnInit() {
    this.initializeFormFields();
  }

  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();
  }

  closeModal() {
    this.contactTypeModal.deny();
    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() {
    this.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.getUserByKey(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() {
    const 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();
        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;
      }
    );
  }

  updateProfile(data) {
    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);
    });
  }

  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() {
    if (this.unsubscribe) {
      this.unsubscribe.next();
      this.unsubscribe.complete();
    }
    if (this.contactTypeModal) {
      this.contactTypeModal.deny();
    }
  }
}

results matching ""

    No results matching ""