]>
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' | |
7ed1edbb C |
6 | import { BuildFormArgument } from '@app/shared/form-validators' |
7 | import { FormReactive, FormValidatorService } from '@app/shared/shared-forms' | |
67ed6552 C |
8 | import { PeerTubePlugin, RegisterServerSettingOptions } from '@shared/models' |
9 | import { 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 | 16 | export 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 | ||
100 | if (settings && settings[name]) return settings[name] | |
101 | ||
102 | const registered = this.registeredSettings.find(r => r.name === name) | |
d00dc28d | 103 | |
dba85a1e | 104 | return registered.default |
d00dc28d C |
105 | } |
106 | ||
107 | } |