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