]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { concat, of } from 'rxjs' |
2 | import { pairwise } from 'rxjs/operators' | |
1d5342ab | 3 | import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core' |
1d5342ab | 4 | import { FormGroup } from '@angular/forms' |
67ed6552 C |
5 | import { UserService } from '@app/core' |
6 | import { FormReactive, FormValidatorService, VideoChannelValidatorsService } from '@app/shared/shared-forms' | |
1d5342ab C |
7 | |
8 | @Component({ | |
b247a132 C |
9 | selector: 'my-register-step-channel', |
10 | templateUrl: './register-step-channel.component.html', | |
11 | styleUrls: [ './register.component.scss' ] | |
1d5342ab | 12 | }) |
b247a132 | 13 | export class RegisterStepChannelComponent extends FormReactive implements OnInit { |
1d5342ab C |
14 | @Input() username: string |
15 | @Output() formBuilt = new EventEmitter<FormGroup>() | |
16 | ||
17 | constructor ( | |
18 | protected formValidatorService: FormValidatorService, | |
1f20622f | 19 | private userService: UserService, |
1d5342ab C |
20 | private videoChannelValidatorsService: VideoChannelValidatorsService |
21 | ) { | |
22 | super() | |
23 | } | |
24 | ||
25 | get instanceHost () { | |
26 | return window.location.host | |
27 | } | |
28 | ||
1d5342ab C |
29 | ngOnInit () { |
30 | this.buildForm({ | |
1f20622f C |
31 | displayName: this.videoChannelValidatorsService.VIDEO_CHANNEL_DISPLAY_NAME, |
32 | name: this.videoChannelValidatorsService.VIDEO_CHANNEL_NAME | |
1d5342ab C |
33 | }) |
34 | ||
35 | setTimeout(() => this.formBuilt.emit(this.form)) | |
1f20622f C |
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 }) | |
1d5342ab C |
53 | } |
54 | } |