1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model'
3 import { I18n } from '@ngx-translate/i18n-polyfill'
4 import { PluginApiService } from '@app/+admin/plugins/shared/plugin-api.service'
5 import { Notifier } from '@app/core'
6 import { ActivatedRoute } from '@angular/router'
7 import { Subscription } from 'rxjs'
8 import { map, switchMap } from 'rxjs/operators'
9 import { BuildFormArgument, FormReactive, FormValidatorService } from '@app/shared'
10 import { RegisterServerSettingOptions } from '@shared/models/plugins/register-server-setting.model'
13 selector: 'my-plugin-show-installed',
14 templateUrl: './plugin-show-installed.component.html',
15 styleUrls: [ './plugin-show-installed.component.scss' ]
17 export class PluginShowInstalledComponent extends FormReactive implements OnInit, OnDestroy {
18 plugin: PeerTubePlugin
19 registeredSettings: RegisterServerSettingOptions[] = []
20 pluginTypeLabel: string
22 private sub: Subscription
25 protected formValidatorService: FormValidatorService,
27 private pluginService: PluginApiService,
28 private notifier: Notifier,
29 private route: ActivatedRoute
35 this.sub = this.route.params.subscribe(
37 const npmName = routeParams['npmName']
39 this.loadPlugin(npmName)
45 if (this.sub) this.sub.unsubscribe()
49 const settings = this.form.value
51 this.pluginService.updatePluginSettings(this.plugin.name, this.plugin.type, settings)
54 this.notifier.success(this.i18n('Settings updated.'))
57 err => this.notifier.error(err.message)
61 hasRegisteredSettings () {
62 return Array.isArray(this.registeredSettings) && this.registeredSettings.length !== 0
65 private loadPlugin (npmName: string) {
66 this.pluginService.getPlugin(npmName)
67 .pipe(switchMap(plugin => {
68 return this.pluginService.getPluginRegisteredSettings(plugin.name, plugin.type)
69 .pipe(map(data => ({ plugin, registeredSettings: data.settings })))
72 ({ plugin, registeredSettings }) => {
74 this.registeredSettings = registeredSettings
76 this.pluginTypeLabel = this.pluginService.getPluginTypeLabel(this.plugin.type)
78 this.buildSettingsForm()
81 err => this.notifier.error(err.message)
85 private buildSettingsForm () {
86 const buildOptions: BuildFormArgument = {}
87 const settingsValues: any = {}
89 for (const setting of this.registeredSettings) {
90 buildOptions[ setting.name ] = null
91 settingsValues[ setting.name ] = this.getSetting(setting.name)
94 this.buildForm(buildOptions)
96 this.form.patchValue(settingsValues)
99 private getSetting (name: string) {
100 const settings = this.plugin.settings
102 if (settings && settings[name]) return settings[name]
104 const registered = this.registeredSettings.find(r => r.name === name)
106 return registered.default