]>
Commit | Line | Data |
---|---|---|
e600e1fe | 1 | import { Component } from '@angular/core' |
df98563e | 2 | import { Router } from '@angular/router' |
df98563e | 3 | import { NotificationsService } from 'angular2-notifications' |
df98563e C |
4 | import { ConfirmService } from '../../../core' |
5 | import { validateHost } from '../../../shared' | |
51548b31 | 6 | import { FollowService } from '../shared' |
e105c19c C |
7 | |
8 | @Component({ | |
51548b31 C |
9 | selector: 'my-following-add', |
10 | templateUrl: './following-add.component.html', | |
11 | styleUrls: [ './following-add.component.scss' ] | |
e105c19c | 12 | }) |
e600e1fe C |
13 | export class FollowingAddComponent { |
14 | hostsString = '' | |
15 | hostsError: string = null | |
df98563e | 16 | error: string = null |
e105c19c | 17 | |
df98563e | 18 | constructor ( |
7ddd02c9 C |
19 | private router: Router, |
20 | private notificationsService: NotificationsService, | |
5769e1db | 21 | private confirmService: ConfirmService, |
51548b31 | 22 | private followService: FollowService |
7ddd02c9 | 23 | ) {} |
e105c19c | 24 | |
e600e1fe | 25 | httpEnabled () { |
df98563e | 26 | return window.location.protocol === 'https:' |
8735451a C |
27 | } |
28 | ||
e600e1fe C |
29 | onHostsChanged () { |
30 | this.hostsError = null | |
e105c19c | 31 | |
e600e1fe C |
32 | const newHostsErrors = [] |
33 | const hosts = this.getNotEmptyHosts() | |
e105c19c | 34 | |
e600e1fe C |
35 | for (const host of hosts) { |
36 | if (validateHost(host) === false) { | |
37 | newHostsErrors.push(`${host} is not valid`) | |
38 | } | |
9e8aa10d C |
39 | } |
40 | ||
e600e1fe C |
41 | if (newHostsErrors.length !== 0) { |
42 | this.hostsError = newHostsErrors.join('. ') | |
9e8aa10d C |
43 | } |
44 | } | |
45 | ||
1f30a185 | 46 | async addFollowing () { |
df98563e | 47 | this.error = '' |
e105c19c | 48 | |
e600e1fe C |
49 | const hosts = this.getNotEmptyHosts() |
50 | if (hosts.length === 0) { | |
51 | this.error = 'You need to specify hosts to follow.' | |
e105c19c C |
52 | } |
53 | ||
e600e1fe | 54 | if (!this.isHostsUnique(hosts)) { |
df98563e C |
55 | this.error = 'Hosts need to be unique.' |
56 | return | |
e105c19c C |
57 | } |
58 | ||
e600e1fe | 59 | const confirmMessage = 'If you confirm, you will send a follow request to:<br /> - ' + hosts.join('<br /> - ') |
1f30a185 C |
60 | const res = await this.confirmService.confirm(confirmMessage, 'Follow new server(s)') |
61 | if (res === false) return | |
e105c19c | 62 | |
1f30a185 C |
63 | this.followService.follow(hosts).subscribe( |
64 | () => { | |
65 | this.notificationsService.success('Success', 'Follow request(s) sent!') | |
e600e1fe | 66 | |
1f30a185 C |
67 | setTimeout(() => this.router.navigate([ '/admin/follows/following-list' ]), 500) |
68 | }, | |
7ddd02c9 | 69 | |
1f30a185 | 70 | err => this.notificationsService.error('Error', err.message) |
df98563e | 71 | ) |
e105c19c C |
72 | } |
73 | ||
df98563e C |
74 | private isHostsUnique (hosts: string[]) { |
75 | return hosts.every(host => hosts.indexOf(host) === hosts.lastIndexOf(host)) | |
e105c19c | 76 | } |
e600e1fe C |
77 | |
78 | private getNotEmptyHosts () { | |
79 | const hosts = this.hostsString | |
80 | .split('\n') | |
81 | .filter(host => host && host.length !== 0) // Eject empty hosts | |
82 | ||
83 | return hosts | |
84 | } | |
e105c19c | 85 | } |