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 | 56 |
1 files changed, 28 insertions, 28 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 index 64165a9a5..86b018de2 100644 --- a/client/src/app/admin/friends/friend-add/friend-add.component.ts +++ b/client/src/app/admin/friends/friend-add/friend-add.component.ts | |||
@@ -2,7 +2,7 @@ import { Component, OnInit } from '@angular/core'; | |||
2 | import { FormControl, FormGroup } from '@angular/forms'; | 2 | import { FormControl, FormGroup } from '@angular/forms'; |
3 | import { Router } from '@angular/router'; | 3 | import { Router } from '@angular/router'; |
4 | 4 | ||
5 | import { validateUrl } from '../../../shared'; | 5 | import { validateHost } from '../../../shared'; |
6 | import { FriendService } from '../shared'; | 6 | import { FriendService } from '../shared'; |
7 | 7 | ||
8 | @Component({ | 8 | @Component({ |
@@ -12,7 +12,7 @@ import { FriendService } from '../shared'; | |||
12 | }) | 12 | }) |
13 | export class FriendAddComponent implements OnInit { | 13 | export class FriendAddComponent implements OnInit { |
14 | form: FormGroup; | 14 | form: FormGroup; |
15 | urls = [ ]; | 15 | hosts = [ ]; |
16 | error: string = null; | 16 | error: string = null; |
17 | 17 | ||
18 | constructor(private router: Router, private friendService: FriendService) {} | 18 | constructor(private router: Router, private friendService: FriendService) {} |
@@ -23,8 +23,8 @@ export class FriendAddComponent implements OnInit { | |||
23 | } | 23 | } |
24 | 24 | ||
25 | addField() { | 25 | addField() { |
26 | this.form.addControl(`url-${this.urls.length}`, new FormControl('', [ validateUrl ])); | 26 | this.form.addControl(`host-${this.hosts.length}`, new FormControl('', [ validateHost ])); |
27 | this.urls.push(''); | 27 | this.hosts.push(''); |
28 | } | 28 | } |
29 | 29 | ||
30 | customTrackBy(index: number, obj: any): any { | 30 | customTrackBy(index: number, obj: any): any { |
@@ -32,52 +32,52 @@ export class FriendAddComponent implements OnInit { | |||
32 | } | 32 | } |
33 | 33 | ||
34 | displayAddField(index: number) { | 34 | displayAddField(index: number) { |
35 | return index === (this.urls.length - 1); | 35 | return index === (this.hosts.length - 1); |
36 | } | 36 | } |
37 | 37 | ||
38 | displayRemoveField(index: number) { | 38 | displayRemoveField(index: number) { |
39 | return (index !== 0 || this.urls.length > 1) && index !== (this.urls.length - 1); | 39 | return (index !== 0 || this.hosts.length > 1) && index !== (this.hosts.length - 1); |
40 | } | 40 | } |
41 | 41 | ||
42 | isFormValid() { | 42 | isFormValid() { |
43 | // Do not check the last input | 43 | // Do not check the last input |
44 | for (let i = 0; i < this.urls.length - 1; i++) { | 44 | for (let i = 0; i < this.hosts.length - 1; i++) { |
45 | if (!this.form.controls[`url-${i}`].valid) return false; | 45 | if (!this.form.controls[`host-${i}`].valid) return false; |
46 | } | 46 | } |
47 | 47 | ||
48 | const lastIndex = this.urls.length - 1; | 48 | const lastIndex = this.hosts.length - 1; |
49 | // If the last input (which is not the first) is empty, it's ok | 49 | // If the last input (which is not the first) is empty, it's ok |
50 | if (this.urls[lastIndex] === '' && lastIndex !== 0) { | 50 | if (this.hosts[lastIndex] === '' && lastIndex !== 0) { |
51 | return true; | 51 | return true; |
52 | } else { | 52 | } else { |
53 | return this.form.controls[`url-${lastIndex}`].valid; | 53 | return this.form.controls[`host-${lastIndex}`].valid; |
54 | } | 54 | } |
55 | } | 55 | } |
56 | 56 | ||
57 | removeField(index: number) { | 57 | removeField(index: number) { |
58 | // Remove the last control | 58 | // Remove the last control |
59 | this.form.removeControl(`url-${this.urls.length - 1}`); | 59 | this.form.removeControl(`host-${this.hosts.length - 1}`); |
60 | this.urls.splice(index, 1); | 60 | this.hosts.splice(index, 1); |
61 | } | 61 | } |
62 | 62 | ||
63 | makeFriends() { | 63 | makeFriends() { |
64 | this.error = ''; | 64 | this.error = ''; |
65 | 65 | ||
66 | const notEmptyUrls = this.getNotEmptyUrls(); | 66 | const notEmptyHosts = this.getNotEmptyHosts(); |
67 | if (notEmptyUrls.length === 0) { | 67 | if (notEmptyHosts.length === 0) { |
68 | this.error = 'You need to specify at less 1 url.'; | 68 | this.error = 'You need to specify at least 1 host.'; |
69 | return; | 69 | return; |
70 | } | 70 | } |
71 | 71 | ||
72 | if (!this.isUrlsUnique(notEmptyUrls)) { | 72 | if (!this.isHostsUnique(notEmptyHosts)) { |
73 | this.error = 'Urls need to be unique.'; | 73 | this.error = 'Hosts need to be unique.'; |
74 | return; | 74 | return; |
75 | } | 75 | } |
76 | 76 | ||
77 | const confirmMessage = 'Are you sure to make friends with:\n - ' + notEmptyUrls.join('\n - '); | 77 | const confirmMessage = 'Are you sure to make friends with:\n - ' + notEmptyHosts.join('\n - '); |
78 | if (!confirm(confirmMessage)) return; | 78 | if (!confirm(confirmMessage)) return; |
79 | 79 | ||
80 | this.friendService.makeFriends(notEmptyUrls).subscribe( | 80 | this.friendService.makeFriends(notEmptyHosts).subscribe( |
81 | status => { | 81 | status => { |
82 | // TODO: extractdatastatus | 82 | // TODO: extractdatastatus |
83 | // if (status === 409) { | 83 | // if (status === 409) { |
@@ -91,18 +91,18 @@ export class FriendAddComponent implements OnInit { | |||
91 | ); | 91 | ); |
92 | } | 92 | } |
93 | 93 | ||
94 | private getNotEmptyUrls() { | 94 | private getNotEmptyHosts() { |
95 | const notEmptyUrls = []; | 95 | const notEmptyHosts = []; |
96 | 96 | ||
97 | Object.keys(this.form.value).forEach((urlKey) => { | 97 | Object.keys(this.form.value).forEach((hostKey) => { |
98 | const url = this.form.value[urlKey]; | 98 | const host = this.form.value[hostKey]; |
99 | if (url !== '') notEmptyUrls.push(url); | 99 | if (host !== '') notEmptyHosts.push(host); |
100 | }); | 100 | }); |
101 | 101 | ||
102 | return notEmptyUrls; | 102 | return notEmptyHosts; |
103 | } | 103 | } |
104 | 104 | ||
105 | private isUrlsUnique(urls: string[]) { | 105 | private isHostsUnique(hosts: string[]) { |
106 | return urls.every(url => urls.indexOf(url) === urls.lastIndexOf(url)); | 106 | return hosts.every(host => hosts.indexOf(host) === hosts.lastIndexOf(host)); |
107 | } | 107 | } |
108 | } | 108 | } |