]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+signup/+register/register-step-channel.component.ts
Retrieve user by id instead of username
[github/Chocobozzz/PeerTube.git] / client / src / app / +signup / +register / register-step-channel.component.ts
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'
6 import { VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR, VIDEO_CHANNEL_NAME_VALIDATOR } from '@app/shared/form-validators/video-channel-validators'
7 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
8
9 @Component({
10 selector: 'my-register-step-channel',
11 templateUrl: './register-step-channel.component.html',
12 styleUrls: [ './register.component.scss' ]
13 })
14 export class RegisterStepChannelComponent extends FormReactive implements OnInit {
15 @Input() username: string
16 @Output() formBuilt = new EventEmitter<FormGroup>()
17
18 constructor (
19 protected formValidatorService: FormValidatorService,
20 private userService: UserService
21 ) {
22 super()
23 }
24
25 get instanceHost () {
26 return window.location.host
27 }
28
29 ngOnInit () {
30 this.buildForm({
31 displayName: VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR,
32 name: VIDEO_CHANNEL_NAME_VALIDATOR
33 })
34
35 setTimeout(() => this.formBuilt.emit(this.form))
36
37 concat(
38 of(''),
39 this.form.get('displayName').valueChanges
40 ).pipe(pairwise())
41 .subscribe(([ oldValue, newValue ]) => this.onDisplayNameChange(oldValue, newValue))
42 }
43
44 isSameThanUsername () {
45 return this.username && this.username === this.form.value['name']
46 }
47
48 private onDisplayNameChange (oldDisplayName: string, newDisplayName: string) {
49 const name = this.form.value['name'] || ''
50
51 const newName = this.userService.getNewUsername(oldDisplayName, newDisplayName, name)
52 this.form.patchValue({ name: newName })
53 }
54 }