1 import { concat, of } from 'rxjs'
2 import { pairwise } from 'rxjs/operators'
3 import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
4 import { FormGroup } from '@angular/forms'
5 import { UserService } from '@app/core'
7 USER_DISPLAY_NAME_REQUIRED_VALIDATOR,
9 USER_PASSWORD_VALIDATOR,
11 USER_USERNAME_VALIDATOR
12 } from '@app/shared/form-validators/user-validators'
13 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
16 selector: 'my-register-step-user',
17 templateUrl: './register-step-user.component.html',
18 styleUrls: [ './register.component.scss' ]
20 export class RegisterStepUserComponent extends FormReactive implements OnInit {
21 @Input() hasCodeOfConduct = false
23 @Output() formBuilt = new EventEmitter<FormGroup>()
24 @Output() termsClick = new EventEmitter<void>()
25 @Output() codeOfConductClick = new EventEmitter<void>()
28 protected formValidatorService: FormValidatorService,
29 private userService: UserService
35 return window.location.host
40 displayName: USER_DISPLAY_NAME_REQUIRED_VALIDATOR,
41 username: USER_USERNAME_VALIDATOR,
42 password: USER_PASSWORD_VALIDATOR,
43 email: USER_EMAIL_VALIDATOR,
44 terms: USER_TERMS_VALIDATOR
47 setTimeout(() => this.formBuilt.emit(this.form))
51 this.form.get('displayName').valueChanges
53 .subscribe(([ oldValue, newValue ]) => this.onDisplayNameChange(oldValue, newValue))
56 onTermsClick (event: Event) {
57 event.preventDefault()
58 this.termsClick.emit()
61 onCodeOfConductClick (event: Event) {
62 event.preventDefault()
63 this.codeOfConductClick.emit()
66 private onDisplayNameChange (oldDisplayName: string, newDisplayName: string) {
67 const username = this.form.value['username'] || ''
69 const newUsername = this.userService.getNewUsername(oldDisplayName, newDisplayName, username)
70 this.form.patchValue({ username: newUsername })