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