Client: fix loading server configurations
[oweals/peertube.git] / client / src / app / core / server / server.service.ts
1 import { Injectable } from '@angular/core'
2 import { HttpClient } from '@angular/common/http'
3
4 import { ServerConfig } from '../../../../../shared'
5
6 @Injectable()
7 export class ServerService {
8   private static BASE_CONFIG_URL = API_URL + '/api/v1/config/'
9   private static BASE_VIDEO_URL = API_URL + '/api/v1/videos/'
10
11   private config: ServerConfig = {
12     signup: {
13       allowed: false
14     }
15   }
16   private videoCategories: Array<{ id: number, label: string }> = []
17   private videoLicences: Array<{ id: number, label: string }> = []
18   private videoLanguages: Array<{ id: number, label: string }> = []
19
20   constructor (private http: HttpClient) {}
21
22   loadConfig () {
23     this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
24              .subscribe(data => this.config = data)
25   }
26
27   loadVideoCategories () {
28     return this.loadVideoAttributeEnum('categories', this.videoCategories)
29   }
30
31   loadVideoLicences () {
32     return this.loadVideoAttributeEnum('licences', this.videoLicences)
33   }
34
35   loadVideoLanguages () {
36     return this.loadVideoAttributeEnum('languages', this.videoLanguages)
37   }
38
39   getConfig () {
40     return this.config
41   }
42
43   getVideoCategories () {
44     return this.videoCategories
45   }
46
47   getVideoLicences () {
48     return this.videoLicences
49   }
50
51   getVideoLanguages () {
52     return this.videoLanguages
53   }
54
55   private loadVideoAttributeEnum (attributeName: 'categories' | 'licences' | 'languages', hashToPopulate: { id: number, label: string }[]) {
56     return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
57                .subscribe(data => {
58                  Object.keys(data)
59                        .forEach(dataKey => {
60                          hashToPopulate.push({
61                            id: parseInt(dataKey, 10),
62                            label: data[dataKey]
63                          })
64                        })
65                })
66   }
67 }