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