]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/follows/following-add/following-add.component.ts
Design follow admin page
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / follows / following-add / following-add.component.ts
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'
7
8 @Component({
9 selector: 'my-following-add',
10 templateUrl: './following-add.component.html',
11 styleUrls: [ './following-add.component.scss' ]
12 })
13 export class FollowingAddComponent {
14 hostsString = ''
15 hostsError: string = null
16 error: string = null
17
18 constructor (
19 private router: Router,
20 private notificationsService: NotificationsService,
21 private confirmService: ConfirmService,
22 private followService: FollowService
23 ) {}
24
25 httpEnabled () {
26 return window.location.protocol === 'https:'
27 }
28
29 onHostsChanged () {
30 this.hostsError = null
31
32 const newHostsErrors = []
33 const hosts = this.getNotEmptyHosts()
34
35 for (const host of hosts) {
36 if (validateHost(host) === false) {
37 newHostsErrors.push(`${host} is not valid`)
38 }
39 }
40
41 if (newHostsErrors.length !== 0) {
42 this.hostsError = newHostsErrors.join('. ')
43 }
44 }
45
46 addFollowing () {
47 this.error = ''
48
49 const hosts = this.getNotEmptyHosts()
50 if (hosts.length === 0) {
51 this.error = 'You need to specify hosts to follow.'
52 }
53
54 if (!this.isHostsUnique(hosts)) {
55 this.error = 'Hosts need to be unique.'
56 return
57 }
58
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(
61 res => {
62 if (res === false) return
63
64 this.followService.follow(hosts).subscribe(
65 status => {
66 this.notificationsService.success('Success', 'Follow request(s) sent!')
67
68 setTimeout(() => this.router.navigate([ '/admin/follows/following-list' ]), 500)
69 },
70
71 err => this.notificationsService.error('Error', err.message)
72 )
73 }
74 )
75 }
76
77 private isHostsUnique (hosts: string[]) {
78 return hosts.every(host => hosts.indexOf(host) === hosts.lastIndexOf(host))
79 }
80
81 private getNotEmptyHosts () {
82 const hosts = this.hostsString
83 .split('\n')
84 .filter(host => host && host.length !== 0) // Eject empty hosts
85
86 return hosts
87 }
88 }