1 import { Component } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { NotificationsService } from 'angular2-notifications'
4 import { ConfirmService } from '../../../core'
5 import { validateHost } from '../../../shared'
6 import { FollowService } from '../shared'
9 selector: 'my-following-add',
10 templateUrl: './following-add.component.html',
11 styleUrls: [ './following-add.component.scss' ]
13 export class FollowingAddComponent {
15 hostsError: string = null
19 private router: Router,
20 private notificationsService: NotificationsService,
21 private confirmService: ConfirmService,
22 private followService: FollowService
26 return window.location.protocol === 'https:'
30 this.hostsError = null
32 const newHostsErrors = []
33 const hosts = this.getNotEmptyHosts()
35 for (const host of hosts) {
36 if (validateHost(host) === false) {
37 newHostsErrors.push(`${host} is not valid`)
41 if (newHostsErrors.length !== 0) {
42 this.hostsError = newHostsErrors.join('. ')
49 const hosts = this.getNotEmptyHosts()
50 if (hosts.length === 0) {
51 this.error = 'You need to specify hosts to follow.'
54 if (!this.isHostsUnique(hosts)) {
55 this.error = 'Hosts need to be unique.'
59 const confirmMessage = 'If you confirm, you will send a follow request to:<br /> - ' + hosts.join('<br /> - ')
60 this.confirmService.confirm(confirmMessage, 'Follow new server(s)').subscribe(
62 if (res === false) return
64 this.followService.follow(hosts).subscribe(
66 this.notificationsService.success('Success', 'Follow request(s) sent!')
68 setTimeout(() => this.router.navigate([ '/admin/follows/following-list' ]), 500)
71 err => this.notificationsService.error('Error', err.message)
77 private isHostsUnique (hosts: string[]) {
78 return hosts.every(host => hosts.indexOf(host) === hosts.lastIndexOf(host))
81 private getNotEmptyHosts () {
82 const hosts = this.hostsString
84 .filter(host => host && host.length !== 0) // Eject empty hosts