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