diff options
Diffstat (limited to 'client/src/app/+admin/friends/friend-add')
-rw-r--r-- | client/src/app/+admin/friends/friend-add/friend-add.component.ts | 108 | ||||
-rw-r--r-- | client/src/app/+admin/friends/friend-add/index.ts | 2 |
2 files changed, 55 insertions, 55 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 12c46e5cd..35cf4a1f7 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 | |||
@@ -1,12 +1,12 @@ | |||
1 | import { Component, OnInit } from '@angular/core'; | 1 | 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 { NotificationsService } from 'angular2-notifications'; | 5 | import { NotificationsService } from 'angular2-notifications' |
6 | 6 | ||
7 | import { ConfirmService } from '../../../core'; | 7 | import { ConfirmService } from '../../../core' |
8 | import { validateHost } from '../../../shared'; | 8 | import { validateHost } from '../../../shared' |
9 | import { FriendService } from '../shared'; | 9 | import { FriendService } from '../shared' |
10 | 10 | ||
11 | @Component({ | 11 | @Component({ |
12 | selector: 'my-friend-add', | 12 | selector: 'my-friend-add', |
@@ -14,107 +14,107 @@ import { FriendService } from '../shared'; | |||
14 | styleUrls: [ './friend-add.component.scss' ] | 14 | styleUrls: [ './friend-add.component.scss' ] |
15 | }) | 15 | }) |
16 | export class FriendAddComponent implements OnInit { | 16 | export class FriendAddComponent implements OnInit { |
17 | form: FormGroup; | 17 | form: FormGroup |
18 | hosts = [ ]; | 18 | hosts = [ ] |
19 | error: string = null; | 19 | error: string = null |
20 | 20 | ||
21 | constructor( | 21 | constructor ( |
22 | private router: Router, | 22 | private router: Router, |
23 | private notificationsService: NotificationsService, | 23 | private notificationsService: NotificationsService, |
24 | private confirmService: ConfirmService, | 24 | private confirmService: ConfirmService, |
25 | private friendService: FriendService | 25 | private friendService: FriendService |
26 | ) {} | 26 | ) {} |
27 | 27 | ||
28 | ngOnInit() { | 28 | ngOnInit () { |
29 | this.form = new FormGroup({}); | 29 | this.form = new FormGroup({}) |
30 | this.addField(); | 30 | this.addField() |
31 | } | 31 | } |
32 | 32 | ||
33 | addField() { | 33 | addField () { |
34 | this.form.addControl(`host-${this.hosts.length}`, new FormControl('', [ validateHost ])); | 34 | this.form.addControl(`host-${this.hosts.length}`, new FormControl('', [ validateHost ])) |
35 | this.hosts.push(''); | 35 | this.hosts.push('') |
36 | } | 36 | } |
37 | 37 | ||
38 | canMakeFriends() { | 38 | canMakeFriends () { |
39 | return window.location.protocol === 'https:'; | 39 | return window.location.protocol === 'https:' |
40 | } | 40 | } |
41 | 41 | ||
42 | customTrackBy(index: number, obj: any): any { | 42 | customTrackBy (index: number, obj: any): any { |
43 | return index; | 43 | return index |
44 | } | 44 | } |
45 | 45 | ||
46 | displayAddField(index: number) { | 46 | displayAddField (index: number) { |
47 | return index === (this.hosts.length - 1); | 47 | return index === (this.hosts.length - 1) |
48 | } | 48 | } |
49 | 49 | ||
50 | displayRemoveField(index: number) { | 50 | displayRemoveField (index: number) { |
51 | return (index !== 0 || this.hosts.length > 1) && index !== (this.hosts.length - 1); | 51 | return (index !== 0 || this.hosts.length > 1) && index !== (this.hosts.length - 1) |
52 | } | 52 | } |
53 | 53 | ||
54 | isFormValid() { | 54 | isFormValid () { |
55 | // Do not check the last input | 55 | // Do not check the last input |
56 | for (let i = 0; i < this.hosts.length - 1; i++) { | 56 | for (let i = 0; i < this.hosts.length - 1; i++) { |
57 | if (!this.form.controls[`host-${i}`].valid) return false; | 57 | if (!this.form.controls[`host-${i}`].valid) return false |
58 | } | 58 | } |
59 | 59 | ||
60 | const lastIndex = this.hosts.length - 1; | 60 | const lastIndex = this.hosts.length - 1 |
61 | // If the last input (which is not the first) is empty, it's ok | 61 | // If the last input (which is not the first) is empty, it's ok |
62 | if (this.hosts[lastIndex] === '' && lastIndex !== 0) { | 62 | if (this.hosts[lastIndex] === '' && lastIndex !== 0) { |
63 | return true; | 63 | return true |
64 | } else { | 64 | } else { |
65 | return this.form.controls[`host-${lastIndex}`].valid; | 65 | return this.form.controls[`host-${lastIndex}`].valid |
66 | } | 66 | } |
67 | } | 67 | } |
68 | 68 | ||
69 | removeField(index: number) { | 69 | removeField (index: number) { |
70 | // Remove the last control | 70 | // Remove the last control |
71 | this.form.removeControl(`host-${this.hosts.length - 1}`); | 71 | this.form.removeControl(`host-${this.hosts.length - 1}`) |
72 | this.hosts.splice(index, 1); | 72 | this.hosts.splice(index, 1) |
73 | } | 73 | } |
74 | 74 | ||
75 | makeFriends() { | 75 | makeFriends () { |
76 | this.error = ''; | 76 | this.error = '' |
77 | 77 | ||
78 | const notEmptyHosts = this.getNotEmptyHosts(); | 78 | const notEmptyHosts = this.getNotEmptyHosts() |
79 | if (notEmptyHosts.length === 0) { | 79 | if (notEmptyHosts.length === 0) { |
80 | this.error = 'You need to specify at least 1 host.'; | 80 | this.error = 'You need to specify at least 1 host.' |
81 | return; | 81 | return |
82 | } | 82 | } |
83 | 83 | ||
84 | if (!this.isHostsUnique(notEmptyHosts)) { | 84 | if (!this.isHostsUnique(notEmptyHosts)) { |
85 | this.error = 'Hosts need to be unique.'; | 85 | this.error = 'Hosts need to be unique.' |
86 | return; | 86 | return |
87 | } | 87 | } |
88 | 88 | ||
89 | const confirmMessage = 'Are you sure to make friends with:<br /> - ' + notEmptyHosts.join('<br /> - '); | 89 | const confirmMessage = 'Are you sure to make friends with:<br /> - ' + notEmptyHosts.join('<br /> - ') |
90 | this.confirmService.confirm(confirmMessage, 'Make friends').subscribe( | 90 | this.confirmService.confirm(confirmMessage, 'Make friends').subscribe( |
91 | res => { | 91 | res => { |
92 | if (res === false) return; | 92 | if (res === false) return |
93 | 93 | ||
94 | this.friendService.makeFriends(notEmptyHosts).subscribe( | 94 | this.friendService.makeFriends(notEmptyHosts).subscribe( |
95 | status => { | 95 | status => { |
96 | this.notificationsService.success('Sucess', 'Make friends request sent!'); | 96 | this.notificationsService.success('Sucess', 'Make friends request sent!') |
97 | this.router.navigate([ '/admin/friends/list' ]); | 97 | this.router.navigate([ '/admin/friends/list' ]) |
98 | }, | 98 | }, |
99 | 99 | ||
100 | err => this.notificationsService.error('Error', err.text) | 100 | err => this.notificationsService.error('Error', err.text) |
101 | ); | 101 | ) |
102 | } | 102 | } |
103 | ); | 103 | ) |
104 | } | 104 | } |
105 | 105 | ||
106 | private getNotEmptyHosts() { | 106 | private getNotEmptyHosts () { |
107 | const notEmptyHosts = []; | 107 | const notEmptyHosts = [] |
108 | 108 | ||
109 | Object.keys(this.form.value).forEach((hostKey) => { | 109 | Object.keys(this.form.value).forEach((hostKey) => { |
110 | const host = this.form.value[hostKey]; | 110 | const host = this.form.value[hostKey] |
111 | if (host !== '') notEmptyHosts.push(host); | 111 | if (host !== '') notEmptyHosts.push(host) |
112 | }); | 112 | }) |
113 | 113 | ||
114 | return notEmptyHosts; | 114 | return notEmptyHosts |
115 | } | 115 | } |
116 | 116 | ||
117 | private isHostsUnique(hosts: string[]) { | 117 | private isHostsUnique (hosts: string[]) { |
118 | return hosts.every(host => hosts.indexOf(host) === hosts.lastIndexOf(host)); | 118 | return hosts.every(host => hosts.indexOf(host) === hosts.lastIndexOf(host)) |
119 | } | 119 | } |
120 | } | 120 | } |
diff --git a/client/src/app/+admin/friends/friend-add/index.ts b/client/src/app/+admin/friends/friend-add/index.ts index a101b3be5..978ab3d46 100644 --- a/client/src/app/+admin/friends/friend-add/index.ts +++ b/client/src/app/+admin/friends/friend-add/index.ts | |||
@@ -1 +1 @@ | |||
export * from './friend-add.component'; | export * from './friend-add.component' | ||