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 { 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) {

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 { 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 {

View File

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

View 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 () {

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_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()
}

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 { 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)

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

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 { 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'

View 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 {}