]>
Commit | Line | Data |
---|---|---|
1d5342ab C |
1 | import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core' |
2 | import { AuthService } from '@app/core' | |
1f20622f | 3 | import { FormReactive, UserService, VideoChannelValidatorsService } 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-channel', |
11 | templateUrl: './register-step-channel.component.html', | |
12 | styleUrls: [ './register.component.scss' ] | |
1d5342ab | 13 | }) |
b247a132 | 14 | export class RegisterStepChannelComponent extends FormReactive implements OnInit { |
1d5342ab C |
15 | @Input() username: string |
16 | @Output() formBuilt = new EventEmitter<FormGroup>() | |
17 | ||
18 | constructor ( | |
19 | protected formValidatorService: FormValidatorService, | |
20 | private authService: AuthService, | |
1f20622f | 21 | private userService: UserService, |
1d5342ab C |
22 | private videoChannelValidatorsService: VideoChannelValidatorsService |
23 | ) { | |
24 | super() | |
25 | } | |
26 | ||
27 | get instanceHost () { | |
28 | return window.location.host | |
29 | } | |
30 | ||
1d5342ab C |
31 | ngOnInit () { |
32 | this.buildForm({ | |
1f20622f C |
33 | displayName: this.videoChannelValidatorsService.VIDEO_CHANNEL_DISPLAY_NAME, |
34 | name: this.videoChannelValidatorsService.VIDEO_CHANNEL_NAME | |
1d5342ab C |
35 | }) |
36 | ||
37 | setTimeout(() => this.formBuilt.emit(this.form)) | |
1f20622f C |
38 | |
39 | concat( | |
40 | of(''), | |
41 | this.form.get('displayName').valueChanges | |
42 | ).pipe(pairwise()) | |
43 | .subscribe(([ oldValue, newValue ]) => this.onDisplayNameChange(oldValue, newValue)) | |
44 | } | |
45 | ||
46 | isSameThanUsername () { | |
47 | return this.username && this.username === this.form.value['name'] | |
48 | } | |
49 | ||
50 | private onDisplayNameChange (oldDisplayName: string, newDisplayName: string) { | |
51 | const name = this.form.value['name'] || '' | |
52 | ||
53 | const newName = this.userService.getNewUsername(oldDisplayName, newDisplayName, name) | |
54 | this.form.patchValue({ name: newName }) | |
1d5342ab C |
55 | } |
56 | } |