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