diff options
Diffstat (limited to 'client/src/app/admin/friends/friend-add/friend-add.component.ts')
-rw-r--r-- | client/src/app/admin/friends/friend-add/friend-add.component.ts | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/client/src/app/admin/friends/friend-add/friend-add.component.ts b/client/src/app/admin/friends/friend-add/friend-add.component.ts new file mode 100644 index 000000000..30dbf4d36 --- /dev/null +++ b/client/src/app/admin/friends/friend-add/friend-add.component.ts | |||
@@ -0,0 +1,99 @@ | |||
1 | import { Component } from '@angular/core'; | ||
2 | import { Router } from '@angular/router'; | ||
3 | |||
4 | import { 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 | }) | ||
11 | export 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 | |||
56 | const confirmMessage = 'Are you sure to make friends with:\n - ' + this.urls.join('\n - '); | ||
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 { | ||
64 | alert('Made friends!'); | ||
65 | } | ||
66 | }, | ||
67 | error => alert(error) | ||
68 | ); | ||
69 | } | ||
70 | |||
71 | private getNotEmptyUrls() { | ||
72 | const notEmptyUrls = []; | ||
73 | |||
74 | this.urls.forEach((url) => { | ||
75 | if (url !== '') notEmptyUrls.push(url); | ||
76 | }); | ||
77 | |||
78 | return notEmptyUrls; | ||
79 | } | ||
80 | |||
81 | // Temporary | ||
82 | // Use HTML validators | ||
83 | private isUrlsRegexValid(urls: string[]) { | ||
84 | let res = true; | ||
85 | |||
86 | const urlRegex = new RegExp('^https?://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)$'); | ||
87 | urls.forEach((url) => { | ||
88 | if (urlRegex.test(url) === false) { | ||
89 | res = false; | ||
90 | } | ||
91 | }); | ||
92 | |||
93 | return res; | ||
94 | } | ||
95 | |||
96 | private isUrlsUnique(urls: string[]) { | ||
97 | return urls.every(url => urls.indexOf(url) === urls.lastIndexOf(url)); | ||
98 | } | ||
99 | } | ||