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