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