]>
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 | 5 | import { UserService } from '@app/core' |
7ed1edbb C |
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' | |
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, | |
7ed1edbb | 20 | private userService: UserService |
1d5342ab C |
21 | ) { |
22 | super() | |
23 | } | |
24 | ||
25 | get instanceHost () { | |
26 | return window.location.host | |
27 | } | |
28 | ||
1d5342ab C |
29 | ngOnInit () { |
30 | this.buildForm({ | |
7ed1edbb C |
31 | displayName: VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR, |
32 | name: VIDEO_CHANNEL_NAME_VALIDATOR | |
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 | } |