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