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