]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts
Fix default boolean plugin setting
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / plugin-show-installed / plugin-show-installed.component.ts
CommitLineData
dba85a1e
C
1import { Subscription } from 'rxjs'
2import { map, switchMap } from 'rxjs/operators'
67ed6552
C
3import { Component, OnDestroy, OnInit } from '@angular/core'
4import { ActivatedRoute } from '@angular/router'
5import { Notifier } from '@app/core'
7ed1edbb
C
6import { BuildFormArgument } from '@app/shared/form-validators'
7import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
67ed6552
C
8import { PeerTubePlugin, RegisterServerSettingOptions } from '@shared/models'
9import { PluginApiService } from '../shared/plugin-api.service'
d00dc28d
C
10
11@Component({
12 selector: 'my-plugin-show-installed',
13 templateUrl: './plugin-show-installed.component.html',
14 styleUrls: [ './plugin-show-installed.component.scss' ]
15})
109d893f 16export class PluginShowInstalledComponent extends FormReactive implements OnInit, OnDestroy {
dba85a1e 17 plugin: PeerTubePlugin
9ae88819 18 registeredSettings: RegisterServerSettingOptions[] = []
dba85a1e
C
19 pluginTypeLabel: string
20
21 private sub: Subscription
22
23 constructor (
24 protected formValidatorService: FormValidatorService,
dba85a1e
C
25 private pluginService: PluginApiService,
26 private notifier: Notifier,
27 private route: ActivatedRoute
28 ) {
29 super()
30 }
d00dc28d
C
31
32 ngOnInit () {
dba85a1e
C
33 this.sub = this.route.params.subscribe(
34 routeParams => {
35 const npmName = routeParams['npmName']
36
37 this.loadPlugin(npmName)
38 }
39 )
40 }
41
42 ngOnDestroy () {
43 if (this.sub) this.sub.unsubscribe()
44 }
45
46 formValidated () {
47 const settings = this.form.value
48
49 this.pluginService.updatePluginSettings(this.plugin.name, this.plugin.type, settings)
50 .subscribe(
51 () => {
66357162 52 this.notifier.success($localize`Settings updated.`)
dba85a1e
C
53 },
54
55 err => this.notifier.error(err.message)
56 )
57 }
58
59 hasRegisteredSettings () {
60 return Array.isArray(this.registeredSettings) && this.registeredSettings.length !== 0
61 }
62
63 private loadPlugin (npmName: string) {
64 this.pluginService.getPlugin(npmName)
65 .pipe(switchMap(plugin => {
66 return this.pluginService.getPluginRegisteredSettings(plugin.name, plugin.type)
ba211e73 67 .pipe(map(data => ({ plugin, registeredSettings: data.registeredSettings })))
dba85a1e
C
68 }))
69 .subscribe(
70 ({ plugin, registeredSettings }) => {
71 this.plugin = plugin
72 this.registeredSettings = registeredSettings
73
74 this.pluginTypeLabel = this.pluginService.getPluginTypeLabel(this.plugin.type)
75
76 this.buildSettingsForm()
77 },
78
79 err => this.notifier.error(err.message)
80 )
81 }
82
83 private buildSettingsForm () {
dba85a1e
C
84 const buildOptions: BuildFormArgument = {}
85 const settingsValues: any = {}
86
87 for (const setting of this.registeredSettings) {
88 buildOptions[ setting.name ] = null
89 settingsValues[ setting.name ] = this.getSetting(setting.name)
90 }
91
92 this.buildForm(buildOptions)
93
94 this.form.patchValue(settingsValues)
95 }
96
97 private getSetting (name: string) {
98 const settings = this.plugin.settings
99
253d4ab6 100 if (settings && settings[name] !== undefined) return settings[name]
dba85a1e
C
101
102 const registered = this.registeredSettings.find(r => r.name === name)
d00dc28d 103
dba85a1e 104 return registered.default
d00dc28d
C
105 }
106
107}