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