]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - client/src/app/admin/friends/friend-add/friend-add.component.ts
Server: set manually the post host of a remote video throught the
[github/Chocobozzz/PeerTube.git] / client / src / app / admin / friends / friend-add / friend-add.component.ts
... / ...
CommitLineData
1import { Component, OnInit } from '@angular/core';
2import { FormControl, FormGroup } from '@angular/forms';
3import { Router } from '@angular/router';
4
5import { validateHost } from '../../../shared';
6import { FriendService } from '../shared';
7
8@Component({
9 selector: 'my-friend-add',
10 templateUrl: './friend-add.component.html',
11 styleUrls: [ './friend-add.component.scss' ]
12})
13export 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 // TODO: extractdatastatus
87 // if (status === 409) {
88 // alert('Already made friends!');
89 // } else {
90 alert('Make friends request sent!');
91 this.router.navigate([ '/admin/friends/list' ]);
92 // }
93 },
94 error => alert(error.text)
95 );
96 }
97
98 private getNotEmptyHosts() {
99 const notEmptyHosts = [];
100
101 Object.keys(this.form.value).forEach((hostKey) => {
102 const host = this.form.value[hostKey];
103 if (host !== '') notEmptyHosts.push(host);
104 });
105
106 return notEmptyHosts;
107 }
108
109 private isHostsUnique(hosts: string[]) {
110 return hosts.every(host => hosts.indexOf(host) === hosts.lastIndexOf(host));
111 }
112}