import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
import { AuthService } from '@app/core'
-import { FormReactive, VideoChannelValidatorsService } from '@app/shared'
+import { FormReactive, UserService, VideoChannelValidatorsService } from '@app/shared'
import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
import { FormGroup } from '@angular/forms'
+import { pairwise } from 'rxjs/operators'
+import { concat, of } from 'rxjs'
@Component({
selector: 'my-register-step-channel',
constructor (
protected formValidatorService: FormValidatorService,
private authService: AuthService,
+ private userService: UserService,
private videoChannelValidatorsService: VideoChannelValidatorsService
) {
super()
return window.location.host
}
- isSameThanUsername () {
- return this.username && this.username === this.form.value['name']
- }
-
ngOnInit () {
this.buildForm({
- name: this.videoChannelValidatorsService.VIDEO_CHANNEL_NAME,
- displayName: this.videoChannelValidatorsService.VIDEO_CHANNEL_DISPLAY_NAME
+ displayName: this.videoChannelValidatorsService.VIDEO_CHANNEL_DISPLAY_NAME,
+ name: this.videoChannelValidatorsService.VIDEO_CHANNEL_NAME
})
setTimeout(() => this.formBuilt.emit(this.form))
+
+ concat(
+ of(''),
+ this.form.get('displayName').valueChanges
+ ).pipe(pairwise())
+ .subscribe(([ oldValue, newValue ]) => this.onDisplayNameChange(oldValue, newValue))
+ }
+
+ isSameThanUsername () {
+ return this.username && this.username === this.form.value['name']
+ }
+
+ private onDisplayNameChange (oldDisplayName: string, newDisplayName: string) {
+ const name = this.form.value['name'] || ''
+
+ const newName = this.userService.getNewUsername(oldDisplayName, newDisplayName, name)
+ this.form.patchValue({ name: newName })
}
}