]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts
Change plugin models names
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / plugin-show-installed / plugin-show-installed.component.ts
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'
11
12 @Component({
13 selector: 'my-plugin-show-installed',
14 templateUrl: './plugin-show-installed.component.html',
15 styleUrls: [ './plugin-show-installed.component.scss' ]
16 })
17 export class PluginShowInstalledComponent extends FormReactive implements OnInit, OnDestroy {
18 plugin: PeerTubePlugin
19 registeredSettings: RegisterServerSettingOptions[] = []
20 pluginTypeLabel: string
21
22 private sub: Subscription
23
24 constructor (
25 protected formValidatorService: FormValidatorService,
26 private i18n: I18n,
27 private pluginService: PluginApiService,
28 private notifier: Notifier,
29 private route: ActivatedRoute
30 ) {
31 super()
32 }
33
34 ngOnInit () {
35 this.sub = this.route.params.subscribe(
36 routeParams => {
37 const npmName = routeParams['npmName']
38
39 this.loadPlugin(npmName)
40 }
41 )
42 }
43
44 ngOnDestroy () {
45 if (this.sub) this.sub.unsubscribe()
46 }
47
48 formValidated () {
49 const settings = this.form.value
50
51 this.pluginService.updatePluginSettings(this.plugin.name, this.plugin.type, settings)
52 .subscribe(
53 () => {
54 this.notifier.success(this.i18n('Settings updated.'))
55 },
56
57 err => this.notifier.error(err.message)
58 )
59 }
60
61 hasRegisteredSettings () {
62 return Array.isArray(this.registeredSettings) && this.registeredSettings.length !== 0
63 }
64
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 })))
70 }))
71 .subscribe(
72 ({ plugin, registeredSettings }) => {
73 this.plugin = plugin
74 this.registeredSettings = registeredSettings
75
76 this.pluginTypeLabel = this.pluginService.getPluginTypeLabel(this.plugin.type)
77
78 this.buildSettingsForm()
79 },
80
81 err => this.notifier.error(err.message)
82 )
83 }
84
85 private buildSettingsForm () {
86 const buildOptions: BuildFormArgument = {}
87 const settingsValues: any = {}
88
89 for (const setting of this.registeredSettings) {
90 buildOptions[ setting.name ] = null
91 settingsValues[ setting.name ] = this.getSetting(setting.name)
92 }
93
94 this.buildForm(buildOptions)
95
96 this.form.patchValue(settingsValues)
97 }
98
99 private getSetting (name: string) {
100 const settings = this.plugin.settings
101
102 if (settings && settings[name]) return settings[name]
103
104 const registered = this.registeredSettings.find(r => r.name === name)
105
106 return registered.default
107 }
108
109 }