]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts
Merge remote-tracking branch 'weblate/develop' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / plugin-show-installed / plugin-show-installed.component.ts
1 import { Subscription } from 'rxjs'
2 import { map, switchMap } from 'rxjs/operators'
3 import { Component, OnDestroy, OnInit } from '@angular/core'
4 import { ActivatedRoute } from '@angular/router'
5 import { Notifier } from '@app/core'
6 import { BuildFormArgument } from '@app/shared/form-validators'
7 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
8 import { PeerTubePlugin, RegisterServerSettingOptions } from '@shared/models'
9 import { PluginApiService } from '../shared/plugin-api.service'
10
11 @Component({
12 selector: 'my-plugin-show-installed',
13 templateUrl: './plugin-show-installed.component.html',
14 styleUrls: [ './plugin-show-installed.component.scss' ]
15 })
16 export class PluginShowInstalledComponent extends FormReactive implements OnInit, OnDestroy {
17 plugin: PeerTubePlugin
18 registeredSettings: RegisterServerSettingOptions[] = []
19 pluginTypeLabel: string
20
21 private sub: Subscription
22
23 constructor (
24 protected formValidatorService: FormValidatorService,
25 private pluginService: PluginApiService,
26 private notifier: Notifier,
27 private route: ActivatedRoute
28 ) {
29 super()
30 }
31
32 ngOnInit () {
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 () => {
52 this.notifier.success($localize`Settings updated.`)
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)
67 .pipe(map(data => ({ plugin, registeredSettings: data.registeredSettings })))
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 () {
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
100 if (settings && settings[name]) return settings[name]
101
102 const registered = this.registeredSettings.find(r => r.name === name)
103
104 return registered.default
105 }
106
107 }