]>
Commit | Line | Data |
---|---|---|
9e8aa10d C |
1 | import { Component, OnInit } from '@angular/core'; |
2 | import { FormControl, FormGroup, REACTIVE_FORM_DIRECTIVES } from '@angular/forms'; | |
e105c19c C |
3 | import { Router } from '@angular/router'; |
4 | ||
9e8aa10d | 5 | import { validateUrl } from '../../../shared'; |
e105c19c C |
6 | import { FriendService } from '../shared'; |
7 | ||
8 | @Component({ | |
9 | selector: 'my-friend-add', | |
10 | template: require('./friend-add.component.html'), | |
9e8aa10d C |
11 | styles: [ require('./friend-add.component.scss') ], |
12 | directives: [ REACTIVE_FORM_DIRECTIVES ] | |
e105c19c | 13 | }) |
9e8aa10d C |
14 | export class FriendAddComponent implements OnInit { |
15 | friendAddForm: FormGroup; | |
16 | urls = [ ]; | |
e105c19c C |
17 | error: string = null; |
18 | ||
19 | constructor(private router: Router, private friendService: FriendService) {} | |
20 | ||
9e8aa10d C |
21 | ngOnInit() { |
22 | this.friendAddForm = new FormGroup({}); | |
23 | this.addField(); | |
24 | } | |
25 | ||
e105c19c | 26 | addField() { |
9e8aa10d | 27 | this.friendAddForm.addControl(`url-${this.urls.length}`, new FormControl('', [ validateUrl ])); |
e105c19c C |
28 | this.urls.push(''); |
29 | } | |
30 | ||
31 | customTrackBy(index: number, obj: any): any { | |
32 | return index; | |
33 | } | |
34 | ||
35 | displayAddField(index: number) { | |
36 | return index === (this.urls.length - 1); | |
37 | } | |
38 | ||
39 | displayRemoveField(index: number) { | |
40 | return (index !== 0 || this.urls.length > 1) && index !== (this.urls.length - 1); | |
41 | } | |
42 | ||
9e8aa10d C |
43 | isFormValid() { |
44 | // Do not check the last input | |
45 | for (let i = 0; i < this.urls.length - 1; i++) { | |
46 | if (!this.friendAddForm.controls[`url-${i}`].valid) return false; | |
47 | } | |
48 | ||
49 | const lastIndex = this.urls.length - 1; | |
50 | // If the last input (which is not the first) is empty, it's ok | |
51 | if (this.urls[lastIndex] === '' && lastIndex !== 0) { | |
52 | return true; | |
53 | } else { | |
54 | return this.friendAddForm.controls[`url-${lastIndex}`].valid; | |
55 | } | |
56 | } | |
57 | ||
e105c19c | 58 | removeField(index: number) { |
f84a89f0 C |
59 | // Remove the last control |
60 | this.friendAddForm.removeControl(`url-${this.urls.length - 1}`); | |
e105c19c C |
61 | this.urls.splice(index, 1); |
62 | } | |
63 | ||
64 | makeFriends() { | |
65 | this.error = ''; | |
66 | ||
67 | const notEmptyUrls = this.getNotEmptyUrls(); | |
68 | if (notEmptyUrls.length === 0) { | |
69 | this.error = 'You need to specify at less 1 url.'; | |
70 | return; | |
71 | } | |
72 | ||
e105c19c C |
73 | if (!this.isUrlsUnique(notEmptyUrls)) { |
74 | this.error = 'Urls need to be unique.'; | |
75 | return; | |
76 | } | |
77 | ||
0f6da32b | 78 | const confirmMessage = 'Are you sure to make friends with:\n - ' + notEmptyUrls.join('\n - '); |
e105c19c C |
79 | if (!confirm(confirmMessage)) return; |
80 | ||
81 | this.friendService.makeFriends(notEmptyUrls).subscribe( | |
82 | status => { | |
83 | if (status === 409) { | |
84 | alert('Already made friends!'); | |
85 | } else { | |
9ab1071c C |
86 | alert('Make friends request sent!'); |
87 | this.router.navigate([ '/admin/friends/list' ]); | |
e105c19c C |
88 | } |
89 | }, | |
90 | error => alert(error) | |
91 | ); | |
92 | } | |
93 | ||
94 | private getNotEmptyUrls() { | |
95 | const notEmptyUrls = []; | |
96 | ||
97 | this.urls.forEach((url) => { | |
98 | if (url !== '') notEmptyUrls.push(url); | |
99 | }); | |
100 | ||
101 | return notEmptyUrls; | |
102 | } | |
103 | ||
e105c19c C |
104 | private isUrlsUnique(urls: string[]) { |
105 | return urls.every(url => urls.indexOf(url) === urls.lastIndexOf(url)); | |
106 | } | |
107 | } |