Client: fix loading server configurations

This commit is contained in:
Chocobozzz 2017-10-09 19:12:40 +02:00
parent bcd1c9e194
commit db7af09bd8
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
13 changed files with 99 additions and 104 deletions

View File

@ -1,7 +1,7 @@
import { Component, OnInit, ViewContainerRef } from '@angular/core' import { Component, OnInit, ViewContainerRef } from '@angular/core'
import { Router } from '@angular/router' import { Router } from '@angular/router'
import { AuthService, ConfigService } from './core' import { AuthService, ServerService } from './core'
import { UserService } from './shared' import { UserService } from './shared'
@Component({ @Component({
@ -28,7 +28,7 @@ export class AppComponent implements OnInit {
constructor ( constructor (
private router: Router, private router: Router,
private authService: AuthService, private authService: AuthService,
private configService: ConfigService, private serverService: ServerService,
private userService: UserService private userService: UserService
) {} ) {}
@ -40,7 +40,11 @@ export class AppComponent implements OnInit {
this.userService.checkTokenValidity() 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 // Do not display menu on small screens
if (window.innerWidth < 600) { if (window.innerWidth < 600) {

View File

@ -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
}
}

View File

@ -1 +0,0 @@
export * from './config.service'

View File

@ -8,7 +8,7 @@ import { SimpleNotificationsModule } from 'angular2-notifications'
import { ModalModule } from 'ngx-bootstrap/modal' import { ModalModule } from 'ngx-bootstrap/modal'
import { AuthService } from './auth' import { AuthService } from './auth'
import { ConfigService } from './config' import { ServerService } from './server'
import { ConfirmComponent, ConfirmService } from './confirm' import { ConfirmComponent, ConfirmService } from './confirm'
import { MenuComponent, MenuAdminComponent } from './menu' import { MenuComponent, MenuAdminComponent } from './menu'
import { throwIfAlreadyLoaded } from './module-import-guard' import { throwIfAlreadyLoaded } from './module-import-guard'
@ -41,7 +41,7 @@ import { throwIfAlreadyLoaded } from './module-import-guard'
providers: [ providers: [
AuthService, AuthService,
ConfirmService, ConfirmService,
ConfigService ServerService
] ]
}) })
export class CoreModule { export class CoreModule {

View File

@ -1,5 +1,5 @@
export * from './auth' export * from './auth'
export * from './config' export * from './server'
export * from './confirm' export * from './confirm'
export * from './menu' export * from './menu'
export * from './routing' export * from './routing'

View File

@ -2,7 +2,7 @@ import { Component, OnInit } from '@angular/core'
import { Router } from '@angular/router' import { Router } from '@angular/router'
import { AuthService, AuthStatus } from '../auth' import { AuthService, AuthStatus } from '../auth'
import { ConfigService } from '../config' import { ServerService } from '../server'
@Component({ @Component({
selector: 'my-menu', selector: 'my-menu',
@ -14,7 +14,7 @@ export class MenuComponent implements OnInit {
constructor ( constructor (
private authService: AuthService, private authService: AuthService,
private configService: ConfigService, private serverService: ServerService,
private router: Router private router: Router
) {} ) {}
@ -37,7 +37,7 @@ export class MenuComponent implements OnInit {
} }
isRegistrationAllowed () { isRegistrationAllowed () {
return this.configService.getConfig().signup.allowed return this.serverService.getConfig().signup.allowed
} }
isUserAdmin () { isUserAdmin () {

View File

@ -0,0 +1 @@
export * from './server.service'

View File

@ -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]
})
})
})
}
}

View File

@ -11,12 +11,13 @@ import {
VIDEO_LICENCE, VIDEO_LICENCE,
VIDEO_LANGUAGE, VIDEO_LANGUAGE,
VIDEO_DESCRIPTION, VIDEO_DESCRIPTION,
VIDEO_TAGS VIDEO_TAGS,
VIDEO_FILE
} from '../../shared' } from '../../shared'
import { ServerService} from '../../core'
import { VideoService } from '../shared' import { VideoService } from '../shared'
import { VideoCreate } from '../../../../../shared' import { VideoCreate } from '../../../../../shared'
import { HttpEventType, HttpResponse } from '@angular/common/http' import { HttpEventType, HttpResponse } from '@angular/common/http'
import { VIDEO_FILE } from '../../shared/forms/form-validators/video'
@Component({ @Component({
selector: 'my-videos-add', selector: 'my-videos-add',
@ -59,6 +60,7 @@ export class VideoAddComponent extends FormReactive implements OnInit {
private formBuilder: FormBuilder, private formBuilder: FormBuilder,
private router: Router, private router: Router,
private notificationsService: NotificationsService, private notificationsService: NotificationsService,
private serverService: ServerService,
private videoService: VideoService private videoService: VideoService
) { ) {
super() super()
@ -84,9 +86,9 @@ export class VideoAddComponent extends FormReactive implements OnInit {
} }
ngOnInit () { ngOnInit () {
this.videoCategories = this.videoService.videoCategories this.videoCategories = this.serverService.getVideoCategories()
this.videoLicences = this.videoService.videoLicences this.videoLicences = this.serverService.getVideoLicences()
this.videoLanguages = this.videoService.videoLanguages this.videoLanguages = this.serverService.getVideoLanguages()
this.buildForm() this.buildForm()
} }

View 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 { FormBuilder, FormGroup } from '@angular/forms'
import { ActivatedRoute, Router } from '@angular/router' import { ActivatedRoute, Router } from '@angular/router'
import { NotificationsService } from 'angular2-notifications' import { NotificationsService } from 'angular2-notifications'
import { AuthService } from '../../core' import { ServerService } from '../../core'
import { import {
FormReactive, FormReactive,
VIDEO_NAME, VIDEO_NAME,
@ -52,12 +52,11 @@ export class VideoUpdateComponent extends FormReactive implements OnInit {
fileError = '' fileError = ''
constructor ( constructor (
private authService: AuthService,
private elementRef: ElementRef,
private formBuilder: FormBuilder, private formBuilder: FormBuilder,
private route: ActivatedRoute, private route: ActivatedRoute,
private router: Router, private router: Router,
private notificationsService: NotificationsService, private notificationsService: NotificationsService,
private serverService: ServerService,
private videoService: VideoService private videoService: VideoService
) { ) {
super() super()
@ -80,9 +79,9 @@ export class VideoUpdateComponent extends FormReactive implements OnInit {
ngOnInit () { ngOnInit () {
this.buildForm() this.buildForm()
this.videoCategories = this.videoService.videoCategories this.videoCategories = this.serverService.getVideoCategories()
this.videoLicences = this.videoService.videoLicences this.videoLicences = this.serverService.getVideoLicences()
this.videoLanguages = this.videoService.videoLanguages this.videoLanguages = this.serverService.getVideoLanguages()
const uuid: string = this.route.snapshot.params['uuid'] const uuid: string = this.route.snapshot.params['uuid']
this.videoService.getVideo(uuid) this.videoService.getVideo(uuid)

View File

@ -14,7 +14,6 @@ import {
import { Video } from './video.model' import { Video } from './video.model'
import { VideoPagination } from './video-pagination.model' import { VideoPagination } from './video-pagination.model'
import { import {
VideoCreate,
UserVideoRate, UserVideoRate,
VideoRateType, VideoRateType,
VideoUpdate, VideoUpdate,
@ -28,28 +27,12 @@ import {
export class VideoService { export class VideoService {
private static BASE_VIDEO_URL = API_URL + '/api/v1/videos/' 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 ( constructor (
private authHttp: HttpClient, private authHttp: HttpClient,
private restExtractor: RestExtractor, private restExtractor: RestExtractor,
private restService: RestService 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) { getVideo (uuid: string) {
return this.authHttp.get<VideoServerModel>(VideoService.BASE_VIDEO_URL + uuid) return this.authHttp.get<VideoServerModel>(VideoService.BASE_VIDEO_URL + uuid)
.map(videoHash => new Video(videoHash)) .map(videoHash => new Video(videoHash))
@ -74,8 +57,7 @@ export class VideoService {
.catch(this.restExtractor.handleError) .catch(this.restExtractor.handleError)
} }
// uploadVideo (video: VideoCreate) { uploadVideo (video: FormData) {
uploadVideo (video: any) {
const req = new HttpRequest('POST', `${VideoService.BASE_VIDEO_URL}/upload`, video, { reportProgress: true }) const req = new HttpRequest('POST', `${VideoService.BASE_VIDEO_URL}/upload`, video, { reportProgress: true })
return this.authHttp.request(req) return this.authHttp.request(req)
@ -175,16 +157,4 @@ export class VideoService {
return { videos, totalVideos } 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]
})
})
})
}
} }

View 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 { SortField, Video } from '../shared'
import { ConfirmService, ConfigService } from '../../core'
import { SortField, Video, VideoService } from '../shared'
import { User } from '../../shared' import { User } from '../../shared'
@Component({ @Component({
@ -11,19 +8,11 @@ import { User } from '../../shared'
styleUrls: [ './video-miniature.component.scss' ], styleUrls: [ './video-miniature.component.scss' ],
templateUrl: './video-miniature.component.html' templateUrl: './video-miniature.component.html'
}) })
export class VideoMiniatureComponent { export class VideoMiniatureComponent {
@Input() currentSort: SortField @Input() currentSort: SortField
@Input() user: User @Input() user: User
@Input() video: Video @Input() video: Video
constructor (
private notificationsService: NotificationsService,
private confirmService: ConfirmService,
private configService: ConfigService,
private videoService: VideoService
) {}
getVideoName () { getVideoName () {
if (this.isVideoNSFWForThisUser()) { if (this.isVideoNSFWForThisUser()) {
return 'NSFW' return 'NSFW'

View File

@ -1,16 +1,6 @@
import { Component, OnInit } from '@angular/core' import { Component } from '@angular/core'
import { VideoService } from './shared'
@Component({ @Component({
template: '<router-outlet></router-outlet>' template: '<router-outlet></router-outlet>'
}) })
export class VideosComponent implements OnInit { export class VideosComponent {}
constructor (private videoService: VideoService) {}
ngOnInit () {
this.videoService.loadVideoCategories()
this.videoService.loadVideoLicences()
this.videoService.loadVideoLanguages()
}
}