Client: fix loading server configurations
authorChocobozzz <florian.bigard@gmail.com>
Mon, 9 Oct 2017 17:12:40 +0000 (19:12 +0200)
committerChocobozzz <florian.bigard@gmail.com>
Mon, 9 Oct 2017 17:12:40 +0000 (19:12 +0200)
13 files changed:
client/src/app/app.component.ts
client/src/app/core/config/config.service.ts [deleted file]
client/src/app/core/config/index.ts [deleted file]
client/src/app/core/core.module.ts
client/src/app/core/index.ts
client/src/app/core/menu/menu.component.ts
client/src/app/core/server/index.ts [new file with mode: 0644]
client/src/app/core/server/server.service.ts [new file with mode: 0644]
client/src/app/videos/+video-edit/video-add.component.ts
client/src/app/videos/+video-edit/video-update.component.ts
client/src/app/videos/shared/video.service.ts
client/src/app/videos/video-list/video-miniature.component.ts
client/src/app/videos/videos.component.ts

index 82e647c98d5e7f386bab71f98c4bd12931a59144..68719f73d284d236ac8ddb244fdca7a9213fbaa9 100644 (file)
@@ -1,7 +1,7 @@
 import { Component, OnInit, ViewContainerRef } from '@angular/core'
 import { Router } from '@angular/router'
 
-import { AuthService, ConfigService } from './core'
+import { AuthService, ServerService } from './core'
 import { UserService } from './shared'
 
 @Component({
@@ -28,7 +28,7 @@ export class AppComponent implements OnInit {
   constructor (
     private router: Router,
     private authService: AuthService,
-    private configService: ConfigService,
+    private serverService: ServerService,
     private userService: UserService
   ) {}
 
@@ -40,7 +40,11 @@ export class AppComponent implements OnInit {
       this.userService.checkTokenValidity()
     }
 
-    this.configService.loadConfig()
+    // Load custom data from server
+    this.serverService.loadConfig()
+    this.serverService.loadVideoCategories()
+    this.serverService.loadVideoLanguages()
+    this.serverService.loadVideoLicences()
 
     // Do not display menu on small screens
     if (window.innerWidth < 600) {
diff --git a/client/src/app/core/config/config.service.ts b/client/src/app/core/config/config.service.ts
deleted file mode 100644 (file)
index 3c479bc..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-import { Injectable } from '@angular/core'
-import { HttpClient } from '@angular/common/http'
-
-import { ServerConfig } from '../../../../../shared'
-
-@Injectable()
-export class ConfigService {
-  private static BASE_CONFIG_URL = API_URL + '/api/v1/config/'
-
-  private config: ServerConfig = {
-    signup: {
-      allowed: false
-    }
-  }
-
-  constructor (private http: HttpClient) {}
-
-  loadConfig () {
-    this.http.get<ServerConfig>(ConfigService.BASE_CONFIG_URL)
-             .subscribe(data => this.config = data)
-  }
-
-  getConfig () {
-    return this.config
-  }
-}
diff --git a/client/src/app/core/config/index.ts b/client/src/app/core/config/index.ts
deleted file mode 100644 (file)
index 3724e12..0000000
+++ /dev/null
@@ -1 +0,0 @@
-export * from './config.service'
index 9ca94dd0e7b138b0c60d44c5b881acf7286aa094..fd1586f8e48e6153ccef44cfc2dcee68b8fb37c8 100644 (file)
@@ -8,7 +8,7 @@ import { SimpleNotificationsModule } from 'angular2-notifications'
 import { ModalModule } from 'ngx-bootstrap/modal'
 
 import { AuthService } from './auth'
-import { ConfigService } from './config'
+import { ServerService } from './server'
 import { ConfirmComponent, ConfirmService } from './confirm'
 import { MenuComponent, MenuAdminComponent } from './menu'
 import { throwIfAlreadyLoaded } from './module-import-guard'
@@ -41,7 +41,7 @@ import { throwIfAlreadyLoaded } from './module-import-guard'
   providers: [
     AuthService,
     ConfirmService,
-    ConfigService
+    ServerService
   ]
 })
 export class CoreModule {
index 31322138f7932eb9bb5d75e0a755be0df687c827..8358261ae6b9de7619e4d1a39b990de3e4eb70eb 100644 (file)
@@ -1,5 +1,5 @@
 export * from './auth'
-export * from './config'
+export * from './server'
 export * from './confirm'
 export * from './menu'
 export * from './routing'
index 669fc6572035600a9e9dc22fadfd254d427a6315..8f15d88381ac5df578e10dd4503868e60a54ed2f 100644 (file)
@@ -2,7 +2,7 @@ import { Component, OnInit } from '@angular/core'
 import { Router } from '@angular/router'
 
 import { AuthService, AuthStatus } from '../auth'
-import { ConfigService } from '../config'
+import { ServerService } from '../server'
 
 @Component({
   selector: 'my-menu',
@@ -14,7 +14,7 @@ export class MenuComponent implements OnInit {
 
   constructor (
     private authService: AuthService,
-    private configService: ConfigService,
+    private serverService: ServerService,
     private router: Router
   ) {}
 
@@ -37,7 +37,7 @@ export class MenuComponent implements OnInit {
   }
 
   isRegistrationAllowed () {
-    return this.configService.getConfig().signup.allowed
+    return this.serverService.getConfig().signup.allowed
   }
 
   isUserAdmin () {
diff --git a/client/src/app/core/server/index.ts b/client/src/app/core/server/index.ts
new file mode 100644 (file)
index 0000000..224da12
--- /dev/null
@@ -0,0 +1 @@
+export * from './server.service'
diff --git a/client/src/app/core/server/server.service.ts b/client/src/app/core/server/server.service.ts
new file mode 100644 (file)
index 0000000..f24df5a
--- /dev/null
@@ -0,0 +1,67 @@
+import { Injectable } from '@angular/core'
+import { HttpClient } from '@angular/common/http'
+
+import { ServerConfig } from '../../../../../shared'
+
+@Injectable()
+export class ServerService {
+  private static BASE_CONFIG_URL = API_URL + '/api/v1/config/'
+  private static BASE_VIDEO_URL = API_URL + '/api/v1/videos/'
+
+  private config: ServerConfig = {
+    signup: {
+      allowed: false
+    }
+  }
+  private videoCategories: Array<{ id: number, label: string }> = []
+  private videoLicences: Array<{ id: number, label: string }> = []
+  private videoLanguages: Array<{ id: number, label: string }> = []
+
+  constructor (private http: HttpClient) {}
+
+  loadConfig () {
+    this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
+             .subscribe(data => this.config = data)
+  }
+
+  loadVideoCategories () {
+    return this.loadVideoAttributeEnum('categories', this.videoCategories)
+  }
+
+  loadVideoLicences () {
+    return this.loadVideoAttributeEnum('licences', this.videoLicences)
+  }
+
+  loadVideoLanguages () {
+    return this.loadVideoAttributeEnum('languages', this.videoLanguages)
+  }
+
+  getConfig () {
+    return this.config
+  }
+
+  getVideoCategories () {
+    return this.videoCategories
+  }
+
+  getVideoLicences () {
+    return this.videoLicences
+  }
+
+  getVideoLanguages () {
+    return this.videoLanguages
+  }
+
+  private loadVideoAttributeEnum (attributeName: 'categories' | 'licences' | 'languages', hashToPopulate: { id: number, label: string }[]) {
+    return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
+               .subscribe(data => {
+                 Object.keys(data)
+                       .forEach(dataKey => {
+                         hashToPopulate.push({
+                           id: parseInt(dataKey, 10),
+                           label: data[dataKey]
+                         })
+                       })
+               })
+  }
+}
index 21311b184067add1af455b3bc7228b5f346e240d..d930423c288c239247e6dfa2873620d1e9a33445 100644 (file)
@@ -11,12 +11,13 @@ import {
   VIDEO_LICENCE,
   VIDEO_LANGUAGE,
   VIDEO_DESCRIPTION,
-  VIDEO_TAGS
+  VIDEO_TAGS,
+  VIDEO_FILE
 } from '../../shared'
+import { ServerService}  from '../../core'
 import { VideoService } from '../shared'
 import { VideoCreate } from '../../../../../shared'
 import { HttpEventType, HttpResponse } from '@angular/common/http'
-import { VIDEO_FILE } from '../../shared/forms/form-validators/video'
 
 @Component({
   selector: 'my-videos-add',
@@ -59,6 +60,7 @@ export class VideoAddComponent extends FormReactive implements OnInit {
     private formBuilder: FormBuilder,
     private router: Router,
     private notificationsService: NotificationsService,
+    private serverService: ServerService,
     private videoService: VideoService
   ) {
     super()
@@ -84,9 +86,9 @@ export class VideoAddComponent extends FormReactive implements OnInit {
   }
 
   ngOnInit () {
-    this.videoCategories = this.videoService.videoCategories
-    this.videoLicences = this.videoService.videoLicences
-    this.videoLanguages = this.videoService.videoLanguages
+    this.videoCategories = this.serverService.getVideoCategories()
+    this.videoLicences = this.serverService.getVideoLicences()
+    this.videoLanguages = this.serverService.getVideoLanguages()
 
     this.buildForm()
   }
index 141ed3522a483ecdeb175abe4a91415989920213..6d45265e790ad7ed457c89286485a36427473674 100644 (file)
@@ -1,10 +1,10 @@
-import { Component, ElementRef, OnInit } from '@angular/core'
+import { Component, OnInit } from '@angular/core'
 import { FormBuilder, FormGroup } from '@angular/forms'
 import { ActivatedRoute, Router } from '@angular/router'
 
 import { NotificationsService } from 'angular2-notifications'
 
-import { AuthService } from '../../core'
+import { ServerService } from '../../core'
 import {
   FormReactive,
   VIDEO_NAME,
@@ -52,12 +52,11 @@ export class VideoUpdateComponent extends FormReactive implements OnInit {
   fileError = ''
 
   constructor (
-    private authService: AuthService,
-    private elementRef: ElementRef,
     private formBuilder: FormBuilder,
     private route: ActivatedRoute,
     private router: Router,
     private notificationsService: NotificationsService,
+    private serverService: ServerService,
     private videoService: VideoService
   ) {
     super()
@@ -80,9 +79,9 @@ export class VideoUpdateComponent extends FormReactive implements OnInit {
   ngOnInit () {
     this.buildForm()
 
-    this.videoCategories = this.videoService.videoCategories
-    this.videoLicences = this.videoService.videoLicences
-    this.videoLanguages = this.videoService.videoLanguages
+    this.videoCategories = this.serverService.getVideoCategories()
+    this.videoLicences = this.serverService.getVideoLicences()
+    this.videoLanguages = this.serverService.getVideoLanguages()
 
     const uuid: string = this.route.snapshot.params['uuid']
     this.videoService.getVideo(uuid)
index cfce4cb16d6b54ca9420c73f1a2af22e0f724ebc..037c20416010c828eddda7a9b82de2a59855ebe9 100644 (file)
@@ -14,7 +14,6 @@ import {
 import { Video } from './video.model'
 import { VideoPagination } from './video-pagination.model'
 import {
-  VideoCreate,
   UserVideoRate,
   VideoRateType,
   VideoUpdate,
@@ -28,28 +27,12 @@ import {
 export class VideoService {
   private static BASE_VIDEO_URL = API_URL + '/api/v1/videos/'
 
-  videoCategories: Array<{ id: number, label: string }> = []
-  videoLicences: Array<{ id: number, label: string }> = []
-  videoLanguages: Array<{ id: number, label: string }> = []
-
   constructor (
     private authHttp: HttpClient,
     private restExtractor: RestExtractor,
     private restService: RestService
   ) {}
 
-  loadVideoCategories () {
-    return this.loadVideoAttributeEnum('categories', this.videoCategories)
-  }
-
-  loadVideoLicences () {
-    return this.loadVideoAttributeEnum('licences', this.videoLicences)
-  }
-
-  loadVideoLanguages () {
-    return this.loadVideoAttributeEnum('languages', this.videoLanguages)
-  }
-
   getVideo (uuid: string) {
     return this.authHttp.get<VideoServerModel>(VideoService.BASE_VIDEO_URL + uuid)
                         .map(videoHash => new Video(videoHash))
@@ -74,8 +57,7 @@ export class VideoService {
                         .catch(this.restExtractor.handleError)
   }
 
-  // uploadVideo (video: VideoCreate) {
-  uploadVideo (video: any) {
+  uploadVideo (video: FormData) {
     const req = new HttpRequest('POST', `${VideoService.BASE_VIDEO_URL}/upload`, video, { reportProgress: true })
 
     return this.authHttp.request(req)
@@ -175,16 +157,4 @@ export class VideoService {
 
     return { videos, totalVideos }
   }
-
-  private loadVideoAttributeEnum (attributeName: 'categories' | 'licences' | 'languages', hashToPopulate: { id: number, label: string }[]) {
-    return this.authHttp.get(VideoService.BASE_VIDEO_URL + attributeName)
-                        .subscribe(data => {
-                          Object.keys(data).forEach(dataKey => {
-                            hashToPopulate.push({
-                              id: parseInt(dataKey, 10),
-                              label: data[dataKey]
-                            })
-                          })
-                        })
-  }
 }
index 1cfeacf36192c4e82ca1a7d28b77e06251485b60..8d8b817ee494a439124b2205a6f4fbf71ca81a1b 100644 (file)
@@ -1,9 +1,6 @@
-import { Component, Input, Output, EventEmitter } from '@angular/core'
+import { Component, Input } from '@angular/core'
 
-import { NotificationsService } from 'angular2-notifications'
-
-import { ConfirmService, ConfigService } from '../../core'
-import { SortField, Video, VideoService } from '../shared'
+import { SortField, Video } from '../shared'
 import { User } from '../../shared'
 
 @Component({
@@ -11,19 +8,11 @@ import { User } from '../../shared'
   styleUrls: [ './video-miniature.component.scss' ],
   templateUrl: './video-miniature.component.html'
 })
-
 export class VideoMiniatureComponent {
   @Input() currentSort: SortField
   @Input() user: User
   @Input() video: Video
 
-  constructor (
-    private notificationsService: NotificationsService,
-    private confirmService: ConfirmService,
-    private configService: ConfigService,
-    private videoService: VideoService
-  ) {}
-
   getVideoName () {
     if (this.isVideoNSFWForThisUser()) {
       return 'NSFW'
index 80ff46a0a18c4128c405d1fe95e1088ab047f51c..585a3ad9aa0b7bc57d7e7262d93519fadd94f303 100644 (file)
@@ -1,16 +1,6 @@
-import { Component, OnInit } from '@angular/core'
-
-import { VideoService } from './shared'
+import { Component } from '@angular/core'
 
 @Component({
   template: '<router-outlet></router-outlet>'
 })
-export class VideosComponent implements OnInit {
-  constructor (private videoService: VideoService) {}
-
-  ngOnInit () {
-    this.videoService.loadVideoCategories()
-    this.videoService.loadVideoLicences()
-    this.videoService.loadVideoLanguages()
-  }
-}
+export class VideosComponent {}