]>
Commit | Line | Data |
---|---|---|
df98563e C |
1 | import { Component, OnInit } from '@angular/core' |
2 | import { FormControl, FormGroup } from '@angular/forms' | |
3 | import { Router } from '@angular/router' | |
e105c19c | 4 | |
df98563e | 5 | import { NotificationsService } from 'angular2-notifications' |
7ddd02c9 | 6 | |
df98563e C |
7 | import { ConfirmService } from '../../../core' |
8 | import { validateHost } from '../../../shared' | |
51548b31 | 9 | import { FollowService } from '../shared' |
e105c19c C |
10 | |
11 | @Component({ | |
51548b31 C |
12 | selector: 'my-following-add', |
13 | templateUrl: './following-add.component.html', | |
14 | styleUrls: [ './following-add.component.scss' ] | |
e105c19c | 15 | }) |
51548b31 | 16 | export class FollowingAddComponent implements OnInit { |
df98563e | 17 | form: FormGroup |
4771e000 | 18 | hosts: string[] = [ ] |
df98563e | 19 | error: string = null |
e105c19c | 20 | |
df98563e | 21 | constructor ( |
7ddd02c9 C |
22 | private router: Router, |
23 | private notificationsService: NotificationsService, | |
5769e1db | 24 | private confirmService: ConfirmService, |
51548b31 | 25 | private followService: FollowService |
7ddd02c9 | 26 | ) {} |
e105c19c | 27 | |
df98563e C |
28 | ngOnInit () { |
29 | this.form = new FormGroup({}) | |
30 | this.addField() | |
9e8aa10d C |
31 | } |
32 | ||
df98563e C |
33 | addField () { |
34 | this.form.addControl(`host-${this.hosts.length}`, new FormControl('', [ validateHost ])) | |
35 | this.hosts.push('') | |
e105c19c C |
36 | } |
37 | ||
df98563e C |
38 | canMakeFriends () { |
39 | return window.location.protocol === 'https:' | |
8735451a C |
40 | } |
41 | ||
df98563e C |
42 | customTrackBy (index: number, obj: any): any { |
43 | return index | |
e105c19c C |
44 | } |
45 | ||
df98563e C |
46 | displayAddField (index: number) { |
47 | return index === (this.hosts.length - 1) | |
e105c19c C |
48 | } |
49 | ||
df98563e C |
50 | displayRemoveField (index: number) { |
51 | return (index !== 0 || this.hosts.length > 1) && index !== (this.hosts.length - 1) | |
e105c19c C |
52 | } |
53 | ||
df98563e | 54 | isFormValid () { |
9e8aa10d | 55 | // Do not check the last input |
49abbbbe | 56 | for (let i = 0; i < this.hosts.length - 1; i++) { |
df98563e | 57 | if (!this.form.controls[`host-${i}`].valid) return false |
9e8aa10d C |
58 | } |
59 | ||
df98563e | 60 | const lastIndex = this.hosts.length - 1 |
9e8aa10d | 61 | // If the last input (which is not the first) is empty, it's ok |
49abbbbe | 62 | if (this.hosts[lastIndex] === '' && lastIndex !== 0) { |
df98563e | 63 | return true |
9e8aa10d | 64 | } else { |
df98563e | 65 | return this.form.controls[`host-${lastIndex}`].valid |
9e8aa10d C |
66 | } |
67 | } | |
68 | ||
df98563e | 69 | removeField (index: number) { |
f84a89f0 | 70 | // Remove the last control |
df98563e C |
71 | this.form.removeControl(`host-${this.hosts.length - 1}`) |
72 | this.hosts.splice(index, 1) | |
e105c19c C |
73 | } |
74 | ||
51548b31 | 75 | addFollowing () { |
df98563e | 76 | this.error = '' |
e105c19c | 77 | |
df98563e | 78 | const notEmptyHosts = this.getNotEmptyHosts() |
49abbbbe | 79 | if (notEmptyHosts.length === 0) { |
df98563e C |
80 | this.error = 'You need to specify at least 1 host.' |
81 | return | |
e105c19c C |
82 | } |
83 | ||
49abbbbe | 84 | if (!this.isHostsUnique(notEmptyHosts)) { |
df98563e C |
85 | this.error = 'Hosts need to be unique.' |
86 | return | |
e105c19c C |
87 | } |
88 | ||
df98563e | 89 | const confirmMessage = 'Are you sure to make friends with:<br /> - ' + notEmptyHosts.join('<br /> - ') |
51548b31 | 90 | this.confirmService.confirm(confirmMessage, 'Follow new server(s)').subscribe( |
5769e1db | 91 | res => { |
df98563e | 92 | if (res === false) return |
e105c19c | 93 | |
51548b31 | 94 | this.followService.follow(notEmptyHosts).subscribe( |
5769e1db | 95 | status => { |
51548b31 | 96 | this.notificationsService.success('Success', 'Follow request(s) sent!') |
59c857da | 97 | this.router.navigate([ '/admin/follows/following-list' ]) |
5769e1db | 98 | }, |
7ddd02c9 | 99 | |
bfb3a98f | 100 | err => this.notificationsService.error('Error', err.message) |
df98563e | 101 | ) |
5769e1db | 102 | } |
df98563e | 103 | ) |
e105c19c C |
104 | } |
105 | ||
df98563e C |
106 | private getNotEmptyHosts () { |
107 | const notEmptyHosts = [] | |
e105c19c | 108 | |
49abbbbe | 109 | Object.keys(this.form.value).forEach((hostKey) => { |
df98563e C |
110 | const host = this.form.value[hostKey] |
111 | if (host !== '') notEmptyHosts.push(host) | |
112 | }) | |
e105c19c | 113 | |
df98563e | 114 | return notEmptyHosts |
e105c19c C |
115 | } |
116 | ||
df98563e C |
117 | private isHostsUnique (hosts: string[]) { |
118 | return hosts.every(host => hosts.indexOf(host) === hosts.lastIndexOf(host)) | |
e105c19c C |
119 | } |
120 | } |