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('. ')
46 async addFollowing () {
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 const res = await this.confirmService.confirm(confirmMessage, 'Follow new server(s)')
61 if (res === false) return
63 this.followService.follow(hosts).subscribe(
65 this.notificationsService.success('Success', 'Follow request(s) sent!')
67 setTimeout(() => this.router.navigate([ '/admin/follows/following-list' ]), 500)
70 err => this.notificationsService.error('Error', err.message)
74 private isHostsUnique (hosts: string[]) {
75 return hosts.every(host => hosts.indexOf(host) === hosts.lastIndexOf(host))
78 private getNotEmptyHosts () {
79 const hosts = this.hostsString
81 .filter(host => host && host.length !== 0) // Eject empty hosts