From db7af09bd8e9de57cdda88c2e32387551235b3a4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 9 Oct 2017 19:12:40 +0200 Subject: [PATCH] Client: fix loading server configurations --- client/src/app/app.component.ts | 10 ++- client/src/app/core/config/config.service.ts | 26 ------- client/src/app/core/config/index.ts | 1 - client/src/app/core/core.module.ts | 4 +- client/src/app/core/index.ts | 2 +- client/src/app/core/menu/menu.component.ts | 6 +- client/src/app/core/server/index.ts | 1 + client/src/app/core/server/server.service.ts | 67 +++++++++++++++++++ .../videos/+video-edit/video-add.component.ts | 12 ++-- .../+video-edit/video-update.component.ts | 13 ++-- client/src/app/videos/shared/video.service.ts | 32 +-------- .../video-list/video-miniature.component.ts | 15 +---- client/src/app/videos/videos.component.ts | 14 +--- 13 files changed, 99 insertions(+), 104 deletions(-) delete mode 100644 client/src/app/core/config/config.service.ts delete mode 100644 client/src/app/core/config/index.ts create mode 100644 client/src/app/core/server/index.ts create mode 100644 client/src/app/core/server/server.service.ts diff --git a/client/src/app/app.component.ts b/client/src/app/app.component.ts index 82e647c98..68719f73d 100644 --- a/client/src/app/app.component.ts +++ b/client/src/app/app.component.ts @@ -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 index 3c479bcb8..000000000 --- a/client/src/app/core/config/config.service.ts +++ /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(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 index 3724e12f2..000000000 --- a/client/src/app/core/config/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './config.service' diff --git a/client/src/app/core/core.module.ts b/client/src/app/core/core.module.ts index 9ca94dd0e..fd1586f8e 100644 --- a/client/src/app/core/core.module.ts +++ b/client/src/app/core/core.module.ts @@ -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 { diff --git a/client/src/app/core/index.ts b/client/src/app/core/index.ts index 31322138f..8358261ae 100644 --- a/client/src/app/core/index.ts +++ b/client/src/app/core/index.ts @@ -1,5 +1,5 @@ export * from './auth' -export * from './config' +export * from './server' export * from './confirm' export * from './menu' export * from './routing' diff --git a/client/src/app/core/menu/menu.component.ts b/client/src/app/core/menu/menu.component.ts index 669fc6572..8f15d8838 100644 --- a/client/src/app/core/menu/menu.component.ts +++ b/client/src/app/core/menu/menu.component.ts @@ -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 index 000000000..224da121a --- /dev/null +++ b/client/src/app/core/server/index.ts @@ -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 index 000000000..f24df5a89 --- /dev/null +++ b/client/src/app/core/server/server.service.ts @@ -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(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] + }) + }) + }) + } +} diff --git a/client/src/app/videos/+video-edit/video-add.component.ts b/client/src/app/videos/+video-edit/video-add.component.ts index 21311b184..d930423c2 100644 --- a/client/src/app/videos/+video-edit/video-add.component.ts +++ b/client/src/app/videos/+video-edit/video-add.component.ts @@ -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() } diff --git a/client/src/app/videos/+video-edit/video-update.component.ts b/client/src/app/videos/+video-edit/video-update.component.ts index 141ed3522..6d45265e7 100644 --- a/client/src/app/videos/+video-edit/video-update.component.ts +++ b/client/src/app/videos/+video-edit/video-update.component.ts @@ -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) diff --git a/client/src/app/videos/shared/video.service.ts b/client/src/app/videos/shared/video.service.ts index cfce4cb16..037c20416 100644 --- a/client/src/app/videos/shared/video.service.ts +++ b/client/src/app/videos/shared/video.service.ts @@ -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(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] - }) - }) - }) - } } diff --git a/client/src/app/videos/video-list/video-miniature.component.ts b/client/src/app/videos/video-list/video-miniature.component.ts index 1cfeacf36..8d8b817ee 100644 --- a/client/src/app/videos/video-list/video-miniature.component.ts +++ b/client/src/app/videos/video-list/video-miniature.component.ts @@ -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' diff --git a/client/src/app/videos/videos.component.ts b/client/src/app/videos/videos.component.ts index 80ff46a0a..585a3ad9a 100644 --- a/client/src/app/videos/videos.component.ts +++ b/client/src/app/videos/videos.component.ts @@ -1,16 +1,6 @@ -import { Component, OnInit } from '@angular/core' - -import { VideoService } from './shared' +import { Component } from '@angular/core' @Component({ template: '' }) -export class VideosComponent implements OnInit { - constructor (private videoService: VideoService) {} - - ngOnInit () { - this.videoService.loadVideoCategories() - this.videoService.loadVideoLicences() - this.videoService.loadVideoLanguages() - } -} +export class VideosComponent {} -- 2.41.0