Client: support the new make friends method
authorChocobozzz <florian.bigard@gmail.com>
Sun, 21 Aug 2016 08:41:21 +0000 (10:41 +0200)
committerChocobozzz <florian.bigard@gmail.com>
Sun, 21 Aug 2016 08:41:21 +0000 (10:41 +0200)
13 files changed:
client/src/app/admin/admin.routes.ts
client/src/app/admin/friends/friend-add/friend-add.component.html [new file with mode: 0644]
client/src/app/admin/friends/friend-add/friend-add.component.scss [new file with mode: 0644]
client/src/app/admin/friends/friend-add/friend-add.component.ts [new file with mode: 0644]
client/src/app/admin/friends/friend-add/index.ts [new file with mode: 0644]
client/src/app/admin/friends/friend-list/friend-list.component.html
client/src/app/admin/friends/friend-list/friend-list.component.ts
client/src/app/admin/friends/friends.routes.ts
client/src/app/admin/friends/index.ts
client/src/app/admin/friends/shared/friend.service.ts
client/src/app/admin/menu-admin.component.html
client/src/app/menu.component.html
client/tsconfig.json

index f57deef62dfca4e881bddc530c4dc303733678b8..80b3ecbc1354bbe8c55e1d86aefeeea557b9a7ca 100644 (file)
@@ -9,6 +9,11 @@ export const AdminRoutes: RouterConfig = [
     path: 'admin',
     component: AdminComponent,
     children: [
+      {
+        path: '',
+        redirectTo: 'users',
+        pathMatch: 'full'
+      },
       ...FriendsRoutes,
       ...UsersRoutes
     ]
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 (file)
index 0000000..a52965e
--- /dev/null
@@ -0,0 +1,18 @@
+<h3>Make friends</h3>
+
+<div *ngIf="error" class="alert alert-danger">{{ error }}</div>
+
+<form role="form" (ngSubmit)="makeFriends()">
+  <div class="form-group"  *ngFor="let url of urls; let id = index; trackBy:customTrackBy">
+    <label for="username">Url</label>
+    <div class="input-group">
+      <input type="text" class="form-control" name="url" id="url" placeholder="http://domain.com" [(ngModel)]="urls[id]" />
+      <span class="input-group-btn">
+        <button *ngIf="displayAddField(id)" (click)="addField()" class="btn btn-default" type="button">+</button>
+        <button *ngIf="displayRemoveField(id)" (click)="removeField(index)" class="btn btn-default" type="button">-</button>
+      </span>
+    </div>
+  </div>
+
+  <input type="submit" value="Make friends" class="btn btn-default">
+</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 (file)
index 0000000..cb597e1
--- /dev/null
@@ -0,0 +1,3 @@
+table {
+  margin-bottom: 40px;
+}
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 (file)
index 0000000..30dbf4d
--- /dev/null
@@ -0,0 +1,99 @@
+import { Component } from '@angular/core';
+import { Router } from '@angular/router';
+
+import { FriendService } from '../shared';
+
+@Component({
+  selector: 'my-friend-add',
+  template: require('./friend-add.component.html'),
+  styles: [ require('./friend-add.component.scss') ]
+})
+export class FriendAddComponent {
+  urls = [ '' ];
+  error: string = null;
+
+  constructor(private router: Router, private friendService: FriendService) {}
+
+  addField() {
+    this.urls.push('');
+  }
+
+  customTrackBy(index: number, obj: any): any {
+    return index;
+  }
+
+  displayAddField(index: number) {
+    return index === (this.urls.length - 1);
+  }
+
+  displayRemoveField(index: number) {
+    return (index !== 0 || this.urls.length > 1) && index !== (this.urls.length - 1);
+  }
+
+  removeField(index: number) {
+    this.urls.splice(index, 1);
+  }
+
+  makeFriends() {
+    this.error = '';
+
+    const notEmptyUrls = this.getNotEmptyUrls();
+    if (notEmptyUrls.length === 0) {
+      this.error = 'You need to specify at less 1 url.';
+      return;
+    }
+
+    if (!this.isUrlsRegexValid(notEmptyUrls)) {
+      this.error = 'Some url(s) are not valid.';
+      return;
+    }
+
+    if (!this.isUrlsUnique(notEmptyUrls)) {
+      this.error = 'Urls need to be unique.';
+      return;
+    }
+
+    const confirmMessage = 'Are you sure to make friends with:\n - ' + this.urls.join('\n - ');
+    if (!confirm(confirmMessage)) return;
+
+    this.friendService.makeFriends(notEmptyUrls).subscribe(
+      status => {
+        if (status === 409) {
+          alert('Already made friends!');
+        } else {
+          alert('Made friends!');
+        }
+      },
+      error => alert(error)
+    );
+  }
+
+  private getNotEmptyUrls() {
+    const notEmptyUrls = [];
+
+    this.urls.forEach((url) => {
+      if (url !== '') notEmptyUrls.push(url);
+    });
+
+    return notEmptyUrls;
+  }
+
+  // Temporary
+  // Use HTML validators
+  private isUrlsRegexValid(urls: string[]) {
+    let res = true;
+
+    const urlRegex = new RegExp('^https?://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)$');
+    urls.forEach((url) => {
+      if (urlRegex.test(url) === false) {
+        res = false;
+      }
+    });
+
+    return res;
+  }
+
+  private isUrlsUnique(urls: string[]) {
+    return urls.every(url => urls.indexOf(url) === urls.lastIndexOf(url));
+  }
+}
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 (file)
index 0000000..a101b3b
--- /dev/null
@@ -0,0 +1 @@
+export * from './friend-add.component';
index f4d14293ed5b67b744977f6a24e630fae2290383..4be3d364f77cbc72ca6532f5bcc40b27a0bcf9fb 100644 (file)
@@ -18,6 +18,6 @@
   Quit friends
 </a>
 
-<a class="add-user btn btn-success pull-right" (click)="makeFriends()">
+<a class="add-user btn btn-success pull-right" [routerLink]="['/admin/friends/add']">
   Make friends
 </a>
index bf66d3ff1083824cf64d34110c9f0359f8df1d26..aa92c1b1e26752211ca69ca10bd89f48d6b49b33 100644 (file)
@@ -1,11 +1,13 @@
 import { Component, OnInit } from '@angular/core';
+import { ROUTER_DIRECTIVES } from '@angular/router';
 
 import { Friend, FriendService } from '../shared';
 
 @Component({
   selector: 'my-friend-list',
   template: require('./friend-list.component.html'),
-  styles: [ require('./friend-list.component.scss') ]
+  styles: [ require('./friend-list.component.scss') ],
+  directives: [ ROUTER_DIRECTIVES ]
 })
 export class FriendListComponent implements OnInit {
   friends: Friend[];
index 1e3646395eaf44fb6c89c9b5aec5d98f8d8e4a7c..42b4a6c14351ed121ad44aa19c5c347b47143969 100644 (file)
@@ -1,6 +1,7 @@
 import { RouterConfig } from '@angular/router';
 
 import { FriendsComponent } from './friends.component';
+import { FriendAddComponent } from './friend-add';
 import { FriendListComponent } from './friend-list';
 
 export const FriendsRoutes: RouterConfig = [
@@ -16,6 +17,10 @@ export const FriendsRoutes: RouterConfig = [
         {
           path: 'list',
           component: FriendListComponent
+        },
+        {
+          path: 'add',
+          component: FriendAddComponent
         }
       ]
     }
index 01aeedeee756fcc5092579be50fd193001736b26..f3110e31db4670209409d201edee0c6cdf204561 100644 (file)
@@ -1,3 +1,4 @@
-export * from './shared';
+export * from './friend-add';
 export * from './friend-list';
+export * from './shared';
 export * from './friends.routes';
index da4d64611828985742455a66483fce1893be47c6..e4e680c29179623d67d5a5d6af529886c41a5864 100644 (file)
@@ -20,8 +20,12 @@ export class FriendService {
                         .catch(this.handleError);
   }
 
-  makeFriends() {
-    return this.authHttp.get(FriendService.BASE_FRIEND_URL + 'makefriends')
+  makeFriends(notEmptyUrls) {
+    const body = {
+      urls: notEmptyUrls
+    };
+
+    return this.authHttp.post(FriendService.BASE_FRIEND_URL + 'makefriends', body)
                         .map(res => res.status)
                         .catch(this.handleError);
   }
index 092ab6081d36cb921c6dc39fa533859c1003af4f..26a3f34924b69e01804bd111e3ad3457b39fec0f 100644 (file)
@@ -8,14 +8,14 @@
 
     <div id="panel-friends" class="panel-button">
       <span class="hidden-xs glyphicon glyphicon-cloud"></span>
-      <a [routerLink]="['/admin/friends/list']">Friends</a>
+      <a [routerLink]="['/admin/friends/list']">List friends</a>
     </div>
   </div>
 
   <div class="panel-block">
     <div id="panel-quit-administration" class="panel-button">
       <span class="hidden-xs glyphicon glyphicon-cog"></span>
-      <a (click)="quitAdmin()">Quit admin.</a>
+      <a [routerLink]="['/videos/list']" (click)="quitAdmin()">Quit admin.</a>
     </div>
   </div>
 </menu>
index 92237539540919ecb85b370bac20890681c91c31..8ea99138d9e53716cd0289cd56a85caa38a83211 100644 (file)
@@ -33,7 +33,7 @@
   <div class="panel-block" *ngIf="isUserAdmin()">
     <div id="panel-get-videos" class="panel-button">
       <span class="hidden-xs glyphicon glyphicon-cog"></span>
-      <a (click)="enterInAdmin()">Administration</a>
+      <a [routerLink]="['/admin']" (click)="enterInAdmin()">Administration</a>
     </div>
   </div>
 </menu>
index 20938ce557ad0b8adfc4350ed59f3a9fa8819400..87a06b0c68edd0d62fbf51feb233ce960fd67337 100644 (file)
@@ -33,6 +33,8 @@
     "src/app/account/index.ts",
     "src/app/admin/admin.component.ts",
     "src/app/admin/admin.routes.ts",
+    "src/app/admin/friends/friend-add/friend-add.component.ts",
+    "src/app/admin/friends/friend-add/index.ts",
     "src/app/admin/friends/friend-list/friend-list.component.ts",
     "src/app/admin/friends/friend-list/index.ts",
     "src/app/admin/friends/friends.component.ts",