diff options
Diffstat (limited to 'client/src/app/admin/friends')
9 files changed, 138 insertions, 5 deletions
diff --git a/client/src/app/admin/friends/friend-add/friend-add.component.html b/client/src/app/admin/friends/friend-add/friend-add.component.html new file mode 100644 index 000000000..a52965e8f --- /dev/null +++ b/client/src/app/admin/friends/friend-add/friend-add.component.html | |||
@@ -0,0 +1,18 @@ | |||
1 | <h3>Make friends</h3> | ||
2 | |||
3 | <div *ngIf="error" class="alert alert-danger">{{ error }}</div> | ||
4 | |||
5 | <form role="form" (ngSubmit)="makeFriends()"> | ||
6 | <div class="form-group" *ngFor="let url of urls; let id = index; trackBy:customTrackBy"> | ||
7 | <label for="username">Url</label> | ||
8 | <div class="input-group"> | ||
9 | <input type="text" class="form-control" name="url" id="url" placeholder="http://domain.com" [(ngModel)]="urls[id]" /> | ||
10 | <span class="input-group-btn"> | ||
11 | <button *ngIf="displayAddField(id)" (click)="addField()" class="btn btn-default" type="button">+</button> | ||
12 | <button *ngIf="displayRemoveField(id)" (click)="removeField(index)" class="btn btn-default" type="button">-</button> | ||
13 | </span> | ||
14 | </div> | ||
15 | </div> | ||
16 | |||
17 | <input type="submit" value="Make friends" class="btn btn-default"> | ||
18 | </form> | ||
diff --git a/client/src/app/admin/friends/friend-add/friend-add.component.scss b/client/src/app/admin/friends/friend-add/friend-add.component.scss new file mode 100644 index 000000000..cb597e12b --- /dev/null +++ b/client/src/app/admin/friends/friend-add/friend-add.component.scss | |||
@@ -0,0 +1,3 @@ | |||
1 | table { | ||
2 | margin-bottom: 40px; | ||
3 | } | ||
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..30dbf4d36 --- /dev/null +++ b/client/src/app/admin/friends/friend-add/friend-add.component.ts | |||
@@ -0,0 +1,99 @@ | |||
1 | import { Component } from '@angular/core'; | ||
2 | import { Router } from '@angular/router'; | ||
3 | |||
4 | import { FriendService } from '../shared'; | ||
5 | |||
6 | @Component({ | ||
7 | selector: 'my-friend-add', | ||
8 | template: require('./friend-add.component.html'), | ||
9 | styles: [ require('./friend-add.component.scss') ] | ||
10 | }) | ||
11 | export class FriendAddComponent { | ||
12 | urls = [ '' ]; | ||
13 | error: string = null; | ||
14 | |||
15 | constructor(private router: Router, private friendService: FriendService) {} | ||
16 | |||
17 | addField() { | ||
18 | this.urls.push(''); | ||
19 | } | ||
20 | |||
21 | customTrackBy(index: number, obj: any): any { | ||
22 | return index; | ||
23 | } | ||
24 | |||
25 | displayAddField(index: number) { | ||
26 | return index === (this.urls.length - 1); | ||
27 | } | ||
28 | |||
29 | displayRemoveField(index: number) { | ||
30 | return (index !== 0 || this.urls.length > 1) && index !== (this.urls.length - 1); | ||
31 | } | ||
32 | |||
33 | removeField(index: number) { | ||
34 | this.urls.splice(index, 1); | ||
35 | } | ||
36 | |||
37 | makeFriends() { | ||
38 | this.error = ''; | ||
39 | |||
40 | const notEmptyUrls = this.getNotEmptyUrls(); | ||
41 | if (notEmptyUrls.length === 0) { | ||
42 | this.error = 'You need to specify at less 1 url.'; | ||
43 | return; | ||
44 | } | ||
45 | |||
46 | if (!this.isUrlsRegexValid(notEmptyUrls)) { | ||
47 | this.error = 'Some url(s) are not valid.'; | ||
48 | return; | ||
49 | } | ||
50 | |||
51 | if (!this.isUrlsUnique(notEmptyUrls)) { | ||
52 | this.error = 'Urls need to be unique.'; | ||
53 | return; | ||
54 | } | ||
55 | |||
56 | const confirmMessage = 'Are you sure to make friends with:\n - ' + this.urls.join('\n - '); | ||
57 | if (!confirm(confirmMessage)) return; | ||
58 | |||
59 | this.friendService.makeFriends(notEmptyUrls).subscribe( | ||
60 | status => { | ||
61 | if (status === 409) { | ||
62 | alert('Already made friends!'); | ||
63 | } else { | ||
64 | alert('Made friends!'); | ||
65 | } | ||
66 | }, | ||
67 | error => alert(error) | ||
68 | ); | ||
69 | } | ||
70 | |||
71 | private getNotEmptyUrls() { | ||
72 | const notEmptyUrls = []; | ||
73 | |||
74 | this.urls.forEach((url) => { | ||
75 | if (url !== '') notEmptyUrls.push(url); | ||
76 | }); | ||
77 | |||
78 | return notEmptyUrls; | ||
79 | } | ||
80 | |||
81 | // Temporary | ||
82 | // Use HTML validators | ||
83 | private isUrlsRegexValid(urls: string[]) { | ||
84 | let res = true; | ||
85 | |||
86 | const urlRegex = new RegExp('^https?://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)$'); | ||
87 | urls.forEach((url) => { | ||
88 | if (urlRegex.test(url) === false) { | ||
89 | res = false; | ||
90 | } | ||
91 | }); | ||
92 | |||
93 | return res; | ||
94 | } | ||
95 | |||
96 | private isUrlsUnique(urls: string[]) { | ||
97 | return urls.every(url => urls.indexOf(url) === urls.lastIndexOf(url)); | ||
98 | } | ||
99 | } | ||
diff --git a/client/src/app/admin/friends/friend-add/index.ts b/client/src/app/admin/friends/friend-add/index.ts new file mode 100644 index 000000000..a101b3be5 --- /dev/null +++ b/client/src/app/admin/friends/friend-add/index.ts | |||
@@ -0,0 +1 @@ | |||
export * from './friend-add.component'; | |||
diff --git a/client/src/app/admin/friends/friend-list/friend-list.component.html b/client/src/app/admin/friends/friend-list/friend-list.component.html index f4d14293e..4be3d364f 100644 --- a/client/src/app/admin/friends/friend-list/friend-list.component.html +++ b/client/src/app/admin/friends/friend-list/friend-list.component.html | |||
@@ -18,6 +18,6 @@ | |||
18 | Quit friends | 18 | Quit friends |
19 | </a> | 19 | </a> |
20 | 20 | ||
21 | <a class="add-user btn btn-success pull-right" (click)="makeFriends()"> | 21 | <a class="add-user btn btn-success pull-right" [routerLink]="['/admin/friends/add']"> |
22 | Make friends | 22 | Make friends |
23 | </a> | 23 | </a> |
diff --git a/client/src/app/admin/friends/friend-list/friend-list.component.ts b/client/src/app/admin/friends/friend-list/friend-list.component.ts index bf66d3ff1..aa92c1b1e 100644 --- a/client/src/app/admin/friends/friend-list/friend-list.component.ts +++ b/client/src/app/admin/friends/friend-list/friend-list.component.ts | |||
@@ -1,11 +1,13 @@ | |||
1 | import { Component, OnInit } from '@angular/core'; | 1 | import { Component, OnInit } from '@angular/core'; |
2 | import { ROUTER_DIRECTIVES } from '@angular/router'; | ||
2 | 3 | ||
3 | import { Friend, FriendService } from '../shared'; | 4 | import { Friend, FriendService } from '../shared'; |
4 | 5 | ||
5 | @Component({ | 6 | @Component({ |
6 | selector: 'my-friend-list', | 7 | selector: 'my-friend-list', |
7 | template: require('./friend-list.component.html'), | 8 | template: require('./friend-list.component.html'), |
8 | styles: [ require('./friend-list.component.scss') ] | 9 | styles: [ require('./friend-list.component.scss') ], |
10 | directives: [ ROUTER_DIRECTIVES ] | ||
9 | }) | 11 | }) |
10 | export class FriendListComponent implements OnInit { | 12 | export class FriendListComponent implements OnInit { |
11 | friends: Friend[]; | 13 | friends: Friend[]; |
diff --git a/client/src/app/admin/friends/friends.routes.ts b/client/src/app/admin/friends/friends.routes.ts index 1e3646395..42b4a6c14 100644 --- a/client/src/app/admin/friends/friends.routes.ts +++ b/client/src/app/admin/friends/friends.routes.ts | |||
@@ -1,6 +1,7 @@ | |||
1 | import { RouterConfig } from '@angular/router'; | 1 | import { RouterConfig } from '@angular/router'; |
2 | 2 | ||
3 | import { FriendsComponent } from './friends.component'; | 3 | import { FriendsComponent } from './friends.component'; |
4 | import { FriendAddComponent } from './friend-add'; | ||
4 | import { FriendListComponent } from './friend-list'; | 5 | import { FriendListComponent } from './friend-list'; |
5 | 6 | ||
6 | export const FriendsRoutes: RouterConfig = [ | 7 | export const FriendsRoutes: RouterConfig = [ |
@@ -16,6 +17,10 @@ export const FriendsRoutes: RouterConfig = [ | |||
16 | { | 17 | { |
17 | path: 'list', | 18 | path: 'list', |
18 | component: FriendListComponent | 19 | component: FriendListComponent |
20 | }, | ||
21 | { | ||
22 | path: 'add', | ||
23 | component: FriendAddComponent | ||
19 | } | 24 | } |
20 | ] | 25 | ] |
21 | } | 26 | } |
diff --git a/client/src/app/admin/friends/index.ts b/client/src/app/admin/friends/index.ts index 01aeedeee..f3110e31d 100644 --- a/client/src/app/admin/friends/index.ts +++ b/client/src/app/admin/friends/index.ts | |||
@@ -1,3 +1,4 @@ | |||
1 | export * from './shared'; | 1 | export * from './friend-add'; |
2 | export * from './friend-list'; | 2 | export * from './friend-list'; |
3 | export * from './shared'; | ||
3 | export * from './friends.routes'; | 4 | export * from './friends.routes'; |
diff --git a/client/src/app/admin/friends/shared/friend.service.ts b/client/src/app/admin/friends/shared/friend.service.ts index da4d64611..e4e680c29 100644 --- a/client/src/app/admin/friends/shared/friend.service.ts +++ b/client/src/app/admin/friends/shared/friend.service.ts | |||
@@ -20,8 +20,12 @@ export class FriendService { | |||
20 | .catch(this.handleError); | 20 | .catch(this.handleError); |
21 | } | 21 | } |
22 | 22 | ||
23 | makeFriends() { | 23 | makeFriends(notEmptyUrls) { |
24 | return this.authHttp.get(FriendService.BASE_FRIEND_URL + 'makefriends') | 24 | const body = { |
25 | urls: notEmptyUrls | ||
26 | }; | ||
27 | |||
28 | return this.authHttp.post(FriendService.BASE_FRIEND_URL + 'makefriends', body) | ||
25 | .map(res => res.status) | 29 | .map(res => res.status) |
26 | .catch(this.handleError); | 30 | .catch(this.handleError); |
27 | } | 31 | } |