]>
Commit | Line | Data |
---|---|---|
421d935d | 1 | import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core' |
1d5342ab | 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 { |
421d935d C |
15 | @Input() hasCodeOfConduct = false |
16 | ||
1d5342ab | 17 | @Output() formBuilt = new EventEmitter<FormGroup>() |
421d935d C |
18 | @Output() termsClick = new EventEmitter<void>() |
19 | @Output() codeOfConductClick = new EventEmitter<void>() | |
1d5342ab C |
20 | |
21 | constructor ( | |
22 | protected formValidatorService: FormValidatorService, | |
23 | private authService: AuthService, | |
1f20622f | 24 | private userService: UserService, |
1d5342ab C |
25 | private userValidatorsService: UserValidatorsService |
26 | ) { | |
27 | super() | |
28 | } | |
29 | ||
30 | get instanceHost () { | |
31 | return window.location.host | |
32 | } | |
33 | ||
34 | ngOnInit () { | |
35 | this.buildForm({ | |
1f20622f | 36 | displayName: this.userValidatorsService.USER_DISPLAY_NAME_REQUIRED, |
1d5342ab C |
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)) | |
1f20622f C |
44 | |
45 | concat( | |
46 | of(''), | |
47 | this.form.get('displayName').valueChanges | |
48 | ).pipe(pairwise()) | |
49 | .subscribe(([ oldValue, newValue ]) => this.onDisplayNameChange(oldValue, newValue)) | |
50 | } | |
51 | ||
421d935d C |
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 | ||
1f20622f C |
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 }) | |
1d5342ab C |
67 | } |
68 | } |