diff options
Diffstat (limited to 'client/src/app/+admin/friends')
14 files changed, 321 insertions, 0 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..eebe033f9 --- /dev/null +++ b/client/src/app/+admin/friends/friend-add/friend-add.component.html | |||
@@ -0,0 +1,30 @@ | |||
1 | <h3>Make friends</h3> | ||
2 | |||
3 | <div *ngIf="error" class="alert alert-danger">{{ error }}</div> | ||
4 | |||
5 | <form (ngSubmit)="makeFriends()" [formGroup]="form"> | ||
6 | <div class="form-group" *ngFor="let host of hosts; let id = index; trackBy:customTrackBy"> | ||
7 | <label for="username">Host</label> | ||
8 | |||
9 | <div class="input-group"> | ||
10 | <input | ||
11 | type="text" class="form-control" placeholder="domain.tld" | ||
12 | [id]="'host-' + id" [formControlName]="'host-' + id" | ||
13 | /> | ||
14 | <span class="input-group-btn"> | ||
15 | <button *ngIf="displayAddField(id)" (click)="addField()" class="btn btn-default" type="button">+</button> | ||
16 | <button *ngIf="displayRemoveField(id)" (click)="removeField(id)" class="btn btn-default" type="button">-</button> | ||
17 | </span> | ||
18 | </div> | ||
19 | |||
20 | <div [hidden]="form.controls['host-' + id].valid || form.controls['host-' + id].pristine" class="alert alert-warning"> | ||
21 | It should be a valid host. | ||
22 | </div> | ||
23 | </div> | ||
24 | |||
25 | <div *ngIf="canMakeFriends() === false" class="alert alert-warning"> | ||
26 | It seems that you are not on a HTTPS pod. Your webserver need to have TLS activated in order to make friends. | ||
27 | </div> | ||
28 | |||
29 | <input type="submit" value="Make friends" class="btn btn-default" [disabled]="!isFormValid()"> | ||
30 | </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..5fde51636 --- /dev/null +++ b/client/src/app/+admin/friends/friend-add/friend-add.component.scss | |||
@@ -0,0 +1,7 @@ | |||
1 | table { | ||
2 | margin-bottom: 40px; | ||
3 | } | ||
4 | |||
5 | .input-group-btn button { | ||
6 | width: 35px; | ||
7 | } | ||
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..014252011 --- /dev/null +++ b/client/src/app/+admin/friends/friend-add/friend-add.component.ts | |||
@@ -0,0 +1,107 @@ | |||
1 | import { Component, OnInit } from '@angular/core'; | ||
2 | import { FormControl, FormGroup } from '@angular/forms'; | ||
3 | import { Router } from '@angular/router'; | ||
4 | |||
5 | import { validateHost } 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 | hosts = [ ]; | ||
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(`host-${this.hosts.length}`, new FormControl('', [ validateHost ])); | ||
27 | this.hosts.push(''); | ||
28 | } | ||
29 | |||
30 | canMakeFriends() { | ||
31 | return window.location.protocol === 'https:'; | ||
32 | } | ||
33 | |||
34 | customTrackBy(index: number, obj: any): any { | ||
35 | return index; | ||
36 | } | ||
37 | |||
38 | displayAddField(index: number) { | ||
39 | return index === (this.hosts.length - 1); | ||
40 | } | ||
41 | |||
42 | displayRemoveField(index: number) { | ||
43 | return (index !== 0 || this.hosts.length > 1) && index !== (this.hosts.length - 1); | ||
44 | } | ||
45 | |||
46 | isFormValid() { | ||
47 | // Do not check the last input | ||
48 | for (let i = 0; i < this.hosts.length - 1; i++) { | ||
49 | if (!this.form.controls[`host-${i}`].valid) return false; | ||
50 | } | ||
51 | |||
52 | const lastIndex = this.hosts.length - 1; | ||
53 | // If the last input (which is not the first) is empty, it's ok | ||
54 | if (this.hosts[lastIndex] === '' && lastIndex !== 0) { | ||
55 | return true; | ||
56 | } else { | ||
57 | return this.form.controls[`host-${lastIndex}`].valid; | ||
58 | } | ||
59 | } | ||
60 | |||
61 | removeField(index: number) { | ||
62 | // Remove the last control | ||
63 | this.form.removeControl(`host-${this.hosts.length - 1}`); | ||
64 | this.hosts.splice(index, 1); | ||
65 | } | ||
66 | |||
67 | makeFriends() { | ||
68 | this.error = ''; | ||
69 | |||
70 | const notEmptyHosts = this.getNotEmptyHosts(); | ||
71 | if (notEmptyHosts.length === 0) { | ||
72 | this.error = 'You need to specify at least 1 host.'; | ||
73 | return; | ||
74 | } | ||
75 | |||
76 | if (!this.isHostsUnique(notEmptyHosts)) { | ||
77 | this.error = 'Hosts need to be unique.'; | ||
78 | return; | ||
79 | } | ||
80 | |||
81 | const confirmMessage = 'Are you sure to make friends with:\n - ' + notEmptyHosts.join('\n - '); | ||
82 | if (!confirm(confirmMessage)) return; | ||
83 | |||
84 | this.friendService.makeFriends(notEmptyHosts).subscribe( | ||
85 | status => { | ||
86 | alert('Make friends request sent!'); | ||
87 | this.router.navigate([ '/admin/friends/list' ]); | ||
88 | }, | ||
89 | error => alert(error.text) | ||
90 | ); | ||
91 | } | ||
92 | |||
93 | private getNotEmptyHosts() { | ||
94 | const notEmptyHosts = []; | ||
95 | |||
96 | Object.keys(this.form.value).forEach((hostKey) => { | ||
97 | const host = this.form.value[hostKey]; | ||
98 | if (host !== '') notEmptyHosts.push(host); | ||
99 | }); | ||
100 | |||
101 | return notEmptyHosts; | ||
102 | } | ||
103 | |||
104 | private isHostsUnique(hosts: string[]) { | ||
105 | return hosts.every(host => hosts.indexOf(host) === hosts.lastIndexOf(host)); | ||
106 | } | ||
107 | } | ||
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 new file mode 100644 index 000000000..06258f8c8 --- /dev/null +++ b/client/src/app/+admin/friends/friend-list/friend-list.component.html | |||
@@ -0,0 +1,29 @@ | |||
1 | <h3>Friends list</h3> | ||
2 | |||
3 | <table class="table table-hover"> | ||
4 | <thead> | ||
5 | <tr> | ||
6 | <th class="table-column-id">ID</th> | ||
7 | <th>Host</th> | ||
8 | <th>Score</th> | ||
9 | <th>Created Date</th> | ||
10 | </tr> | ||
11 | </thead> | ||
12 | |||
13 | <tbody> | ||
14 | <tr *ngFor="let friend of friends"> | ||
15 | <td>{{ friend.id }}</td> | ||
16 | <td>{{ friend.host }}</td> | ||
17 | <td>{{ friend.score }}</td> | ||
18 | <td>{{ friend.createdAt | date: 'medium' }}</td> | ||
19 | </tr> | ||
20 | </tbody> | ||
21 | </table> | ||
22 | |||
23 | <a *ngIf="friends && friends.length !== 0" class="add-user btn btn-danger pull-left" (click)="quitFriends()"> | ||
24 | Quit friends | ||
25 | </a> | ||
26 | |||
27 | <a *ngIf="friends?.length === 0" class="add-user btn btn-success pull-right" [routerLink]="['/admin/friends/add']"> | ||
28 | Make friends | ||
29 | </a> | ||
diff --git a/client/src/app/+admin/friends/friend-list/friend-list.component.scss b/client/src/app/+admin/friends/friend-list/friend-list.component.scss new file mode 100644 index 000000000..cb597e12b --- /dev/null +++ b/client/src/app/+admin/friends/friend-list/friend-list.component.scss | |||
@@ -0,0 +1,3 @@ | |||
1 | table { | ||
2 | margin-bottom: 40px; | ||
3 | } | ||
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 new file mode 100644 index 000000000..bec10162c --- /dev/null +++ b/client/src/app/+admin/friends/friend-list/friend-list.component.ts | |||
@@ -0,0 +1,38 @@ | |||
1 | import { Component, OnInit } from '@angular/core'; | ||
2 | |||
3 | import { Friend, FriendService } from '../shared'; | ||
4 | |||
5 | @Component({ | ||
6 | selector: 'my-friend-list', | ||
7 | templateUrl: './friend-list.component.html', | ||
8 | styleUrls: [ './friend-list.component.scss' ] | ||
9 | }) | ||
10 | export class FriendListComponent implements OnInit { | ||
11 | friends: Friend[]; | ||
12 | |||
13 | constructor(private friendService: FriendService) { } | ||
14 | |||
15 | ngOnInit() { | ||
16 | this.getFriends(); | ||
17 | } | ||
18 | |||
19 | quitFriends() { | ||
20 | if (!confirm('Are you sure?')) return; | ||
21 | |||
22 | this.friendService.quitFriends().subscribe( | ||
23 | status => { | ||
24 | alert('Quit friends!'); | ||
25 | this.getFriends(); | ||
26 | }, | ||
27 | error => alert(error.text) | ||
28 | ); | ||
29 | } | ||
30 | |||
31 | private getFriends() { | ||
32 | this.friendService.getFriends().subscribe( | ||
33 | res => this.friends = res.friends, | ||
34 | |||
35 | err => alert(err.text) | ||
36 | ); | ||
37 | } | ||
38 | } | ||
diff --git a/client/src/app/+admin/friends/friend-list/index.ts b/client/src/app/+admin/friends/friend-list/index.ts new file mode 100644 index 000000000..354c978a4 --- /dev/null +++ b/client/src/app/+admin/friends/friend-list/index.ts | |||
@@ -0,0 +1 @@ | |||
export * from './friend-list.component'; | |||
diff --git a/client/src/app/+admin/friends/friends.component.ts b/client/src/app/+admin/friends/friends.component.ts new file mode 100644 index 000000000..bc3f54158 --- /dev/null +++ b/client/src/app/+admin/friends/friends.component.ts | |||
@@ -0,0 +1,8 @@ | |||
1 | import { Component } from '@angular/core'; | ||
2 | |||
3 | @Component({ | ||
4 | template: '<router-outlet></router-outlet>' | ||
5 | }) | ||
6 | |||
7 | export class FriendsComponent { | ||
8 | } | ||
diff --git a/client/src/app/+admin/friends/friends.routes.ts b/client/src/app/+admin/friends/friends.routes.ts new file mode 100644 index 000000000..a9a06539b --- /dev/null +++ b/client/src/app/+admin/friends/friends.routes.ts | |||
@@ -0,0 +1,37 @@ | |||
1 | import { Routes } from '@angular/router'; | ||
2 | |||
3 | import { FriendsComponent } from './friends.component'; | ||
4 | import { FriendAddComponent } from './friend-add'; | ||
5 | import { FriendListComponent } from './friend-list'; | ||
6 | |||
7 | export const FriendsRoutes: Routes = [ | ||
8 | { | ||
9 | path: 'friends', | ||
10 | component: FriendsComponent, | ||
11 | children: [ | ||
12 | { | ||
13 | path: '', | ||
14 | redirectTo: 'list', | ||
15 | pathMatch: 'full' | ||
16 | }, | ||
17 | { | ||
18 | path: 'list', | ||
19 | component: FriendListComponent, | ||
20 | data: { | ||
21 | meta: { | ||
22 | titleSuffix: ' - Friends list' | ||
23 | } | ||
24 | } | ||
25 | }, | ||
26 | { | ||
27 | path: 'add', | ||
28 | component: FriendAddComponent, | ||
29 | data: { | ||
30 | meta: { | ||
31 | titleSuffix: ' - Add friends' | ||
32 | } | ||
33 | } | ||
34 | } | ||
35 | ] | ||
36 | } | ||
37 | ]; | ||
diff --git a/client/src/app/+admin/friends/index.ts b/client/src/app/+admin/friends/index.ts new file mode 100644 index 000000000..dd4df2538 --- /dev/null +++ b/client/src/app/+admin/friends/index.ts | |||
@@ -0,0 +1,5 @@ | |||
1 | export * from './friend-add'; | ||
2 | export * from './friend-list'; | ||
3 | export * from './shared'; | ||
4 | export * from './friends.component'; | ||
5 | export * from './friends.routes'; | ||
diff --git a/client/src/app/+admin/friends/shared/friend.model.ts b/client/src/app/+admin/friends/shared/friend.model.ts new file mode 100644 index 000000000..462cc82ed --- /dev/null +++ b/client/src/app/+admin/friends/shared/friend.model.ts | |||
@@ -0,0 +1,6 @@ | |||
1 | export interface Friend { | ||
2 | id: string; | ||
3 | host: string; | ||
4 | score: number; | ||
5 | createdAt: Date; | ||
6 | } | ||
diff --git a/client/src/app/+admin/friends/shared/friend.service.ts b/client/src/app/+admin/friends/shared/friend.service.ts new file mode 100644 index 000000000..e97459385 --- /dev/null +++ b/client/src/app/+admin/friends/shared/friend.service.ts | |||
@@ -0,0 +1,47 @@ | |||
1 | import { Injectable } from '@angular/core'; | ||
2 | import { Observable } from 'rxjs/Observable'; | ||
3 | import 'rxjs/add/operator/catch'; | ||
4 | import 'rxjs/add/operator/map'; | ||
5 | |||
6 | import { Friend } from './friend.model'; | ||
7 | import { AuthHttp, RestExtractor, ResultList } from '../../../shared'; | ||
8 | |||
9 | @Injectable() | ||
10 | export class FriendService { | ||
11 | private static BASE_FRIEND_URL: string = '/api/v1/pods/'; | ||
12 | |||
13 | constructor ( | ||
14 | private authHttp: AuthHttp, | ||
15 | private restExtractor: RestExtractor | ||
16 | ) {} | ||
17 | |||
18 | getFriends() { | ||
19 | return this.authHttp.get(FriendService.BASE_FRIEND_URL) | ||
20 | .map(this.restExtractor.extractDataList) | ||
21 | .map(this.extractFriends) | ||
22 | .catch((res) => this.restExtractor.handleError(res)); | ||
23 | } | ||
24 | |||
25 | makeFriends(notEmptyHosts) { | ||
26 | const body = { | ||
27 | hosts: notEmptyHosts | ||
28 | }; | ||
29 | |||
30 | return this.authHttp.post(FriendService.BASE_FRIEND_URL + 'makefriends', body) | ||
31 | .map(this.restExtractor.extractDataBool) | ||
32 | .catch((res) => this.restExtractor.handleError(res)); | ||
33 | } | ||
34 | |||
35 | quitFriends() { | ||
36 | return this.authHttp.get(FriendService.BASE_FRIEND_URL + 'quitfriends') | ||
37 | .map(res => res.status) | ||
38 | .catch((res) => this.restExtractor.handleError(res)); | ||
39 | } | ||
40 | |||
41 | private extractFriends(result: ResultList) { | ||
42 | const friends: Friend[] = result.data; | ||
43 | const totalFriends = result.total; | ||
44 | |||
45 | return { friends, totalFriends }; | ||
46 | } | ||
47 | } | ||
diff --git a/client/src/app/+admin/friends/shared/index.ts b/client/src/app/+admin/friends/shared/index.ts new file mode 100644 index 000000000..0d671637d --- /dev/null +++ b/client/src/app/+admin/friends/shared/index.ts | |||
@@ -0,0 +1,2 @@ | |||
1 | export * from './friend.model'; | ||
2 | export * from './friend.service'; | ||