]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/admin/friends/friend-add/friend-add.component.ts
Client: change url validation for friend add
[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 this.urls.splice(index, 1);
60 }
61
62 makeFriends() {
63 this.error = '';
64
65 const notEmptyUrls = this.getNotEmptyUrls();
66 if (notEmptyUrls.length === 0) {
67 this.error = 'You need to specify at less 1 url.';
68 return;
69 }
70
71 if (!this.isUrlsUnique(notEmptyUrls)) {
72 this.error = 'Urls need to be unique.';
73 return;
74 }
75
76 const confirmMessage = 'Are you sure to make friends with:\n - ' + notEmptyUrls.join('\n - ');
77 if (!confirm(confirmMessage)) return;
78
79 this.friendService.makeFriends(notEmptyUrls).subscribe(
80 status => {
81 if (status === 409) {
82 alert('Already made friends!');
83 } else {
84 alert('Make friends request sent!');
85 this.router.navigate([ '/admin/friends/list' ]);
86 }
87 },
88 error => alert(error)
89 );
90 }
91
92 private getNotEmptyUrls() {
93 const notEmptyUrls = [];
94
95 this.urls.forEach((url) => {
96 if (url !== '') notEmptyUrls.push(url);
97 });
98
99 return notEmptyUrls;
100 }
101
102 private isUrlsUnique(urls: string[]) {
103 return urls.every(url => urls.indexOf(url) === urls.lastIndexOf(url));
104 }
105 }