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