]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
9e8aa10d
C
1import { Component, OnInit } from '@angular/core';
2import { FormControl, FormGroup, REACTIVE_FORM_DIRECTIVES } from '@angular/forms';
e105c19c
C
3import { Router } from '@angular/router';
4
9e8aa10d 5import { validateUrl } from '../../../shared';
e105c19c
C
6import { FriendService } from '../shared';
7
8@Component({
9 selector: 'my-friend-add',
10 template: require('./friend-add.component.html'),
9e8aa10d
C
11 styles: [ require('./friend-add.component.scss') ],
12 directives: [ REACTIVE_FORM_DIRECTIVES ]
e105c19c 13})
9e8aa10d
C
14export class FriendAddComponent implements OnInit {
15 friendAddForm: FormGroup;
16 urls = [ ];
e105c19c
C
17 error: string = null;
18
19 constructor(private router: Router, private friendService: FriendService) {}
20
9e8aa10d
C
21 ngOnInit() {
22 this.friendAddForm = new FormGroup({});
23 this.addField();
24 }
25
e105c19c 26 addField() {
9e8aa10d 27 this.friendAddForm.addControl(`url-${this.urls.length}`, new FormControl('', [ validateUrl ]));
e105c19c
C
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
9e8aa10d
C
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
e105c19c
C
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
e105c19c
C
71 if (!this.isUrlsUnique(notEmptyUrls)) {
72 this.error = 'Urls need to be unique.';
73 return;
74 }
75
0f6da32b 76 const confirmMessage = 'Are you sure to make friends with:\n - ' + notEmptyUrls.join('\n - ');
e105c19c
C
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 {
9ab1071c
C
84 alert('Make friends request sent!');
85 this.router.navigate([ '/admin/friends/list' ]);
e105c19c
C
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
e105c19c
C
102 private isUrlsUnique(urls: string[]) {
103 return urls.every(url => urls.indexOf(url) === urls.lastIndexOf(url));
104 }
105}