]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/admin/friends/friend-add/friend-add.component.ts
Do not wait the make friends process ends to send a response to the
[github/Chocobozzz/PeerTube.git] / client / src / app / admin / friends / friend-add / friend-add.component.ts
CommitLineData
e105c19c
C
1import { Component } from '@angular/core';
2import { Router } from '@angular/router';
3
4import { FriendService } from '../shared';
5
6@Component({
7 selector: 'my-friend-add',
8 template: require('./friend-add.component.html'),
9 styles: [ require('./friend-add.component.scss') ]
10})
11export class FriendAddComponent {
12 urls = [ '' ];
13 error: string = null;
14
15 constructor(private router: Router, private friendService: FriendService) {}
16
17 addField() {
18 this.urls.push('');
19 }
20
21 customTrackBy(index: number, obj: any): any {
22 return index;
23 }
24
25 displayAddField(index: number) {
26 return index === (this.urls.length - 1);
27 }
28
29 displayRemoveField(index: number) {
30 return (index !== 0 || this.urls.length > 1) && index !== (this.urls.length - 1);
31 }
32
33 removeField(index: number) {
34 this.urls.splice(index, 1);
35 }
36
37 makeFriends() {
38 this.error = '';
39
40 const notEmptyUrls = this.getNotEmptyUrls();
41 if (notEmptyUrls.length === 0) {
42 this.error = 'You need to specify at less 1 url.';
43 return;
44 }
45
46 if (!this.isUrlsRegexValid(notEmptyUrls)) {
47 this.error = 'Some url(s) are not valid.';
48 return;
49 }
50
51 if (!this.isUrlsUnique(notEmptyUrls)) {
52 this.error = 'Urls need to be unique.';
53 return;
54 }
55
0f6da32b 56 const confirmMessage = 'Are you sure to make friends with:\n - ' + notEmptyUrls.join('\n - ');
e105c19c
C
57 if (!confirm(confirmMessage)) return;
58
59 this.friendService.makeFriends(notEmptyUrls).subscribe(
60 status => {
61 if (status === 409) {
62 alert('Already made friends!');
63 } else {
9ab1071c
C
64 alert('Make friends request sent!');
65 this.router.navigate([ '/admin/friends/list' ]);
e105c19c
C
66 }
67 },
68 error => alert(error)
69 );
70 }
71
72 private getNotEmptyUrls() {
73 const notEmptyUrls = [];
74
75 this.urls.forEach((url) => {
76 if (url !== '') notEmptyUrls.push(url);
77 });
78
79 return notEmptyUrls;
80 }
81
82 // Temporary
83 // Use HTML validators
84 private isUrlsRegexValid(urls: string[]) {
85 let res = true;
86
87 const urlRegex = new RegExp('^https?://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)$');
88 urls.forEach((url) => {
89 if (urlRegex.test(url) === false) {
90 res = false;
91 }
92 });
93
94 return res;
95 }
96
97 private isUrlsUnique(urls: string[]) {
98 return urls.every(url => urls.indexOf(url) === urls.lastIndexOf(url));
99 }
100}