]>
Commit | Line | Data |
---|---|---|
1d5342ab C |
1 | import { Component, EventEmitter, OnInit, Output } from '@angular/core' |
2 | import { AuthService } from '@app/core' | |
1f20622f | 3 | import { FormReactive, UserService, UserValidatorsService } from '@app/shared' |
1d5342ab C |
4 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' |
5 | import { FormGroup } from '@angular/forms' | |
1f20622f C |
6 | import { pairwise } from 'rxjs/operators' |
7 | import { concat, of } from 'rxjs' | |
1d5342ab C |
8 | |
9 | @Component({ | |
b247a132 C |
10 | selector: 'my-register-step-user', |
11 | templateUrl: './register-step-user.component.html', | |
12 | styleUrls: [ './register.component.scss' ] | |
1d5342ab | 13 | }) |
b247a132 | 14 | export class RegisterStepUserComponent extends FormReactive implements OnInit { |
1d5342ab C |
15 | @Output() formBuilt = new EventEmitter<FormGroup>() |
16 | ||
17 | constructor ( | |
18 | protected formValidatorService: FormValidatorService, | |
19 | private authService: AuthService, | |
1f20622f | 20 | private userService: UserService, |
1d5342ab C |
21 | private userValidatorsService: UserValidatorsService |
22 | ) { | |
23 | super() | |
24 | } | |
25 | ||
26 | get instanceHost () { | |
27 | return window.location.host | |
28 | } | |
29 | ||
30 | ngOnInit () { | |
31 | this.buildForm({ | |
1f20622f | 32 | displayName: this.userValidatorsService.USER_DISPLAY_NAME_REQUIRED, |
1d5342ab C |
33 | username: this.userValidatorsService.USER_USERNAME, |
34 | password: this.userValidatorsService.USER_PASSWORD, | |
35 | email: this.userValidatorsService.USER_EMAIL, | |
36 | terms: this.userValidatorsService.USER_TERMS | |
37 | }) | |
38 | ||
39 | setTimeout(() => this.formBuilt.emit(this.form)) | |
1f20622f C |
40 | |
41 | concat( | |
42 | of(''), | |
43 | this.form.get('displayName').valueChanges | |
44 | ).pipe(pairwise()) | |
45 | .subscribe(([ oldValue, newValue ]) => this.onDisplayNameChange(oldValue, newValue)) | |
46 | } | |
47 | ||
48 | private onDisplayNameChange (oldDisplayName: string, newDisplayName: string) { | |
49 | const username = this.form.value['username'] || '' | |
50 | ||
51 | const newUsername = this.userService.getNewUsername(oldDisplayName, newDisplayName, username) | |
52 | this.form.patchValue({ username: newUsername }) | |
1d5342ab C |
53 | } |
54 | } |