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