diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-11 14:40:19 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-07-24 10:58:16 +0200 |
commit | dba85a1e9e9f603ba52e1ea42deaf3fdd799b1d8 (patch) | |
tree | 7695023d90b78f972abafc718346c50264587ff5 /client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts | |
parent | d00dc28dd73ad9dd419d5a5ac6ac747cefbc6e8b (diff) | |
download | PeerTube-dba85a1e9e9f603ba52e1ea42deaf3fdd799b1d8.tar.gz PeerTube-dba85a1e9e9f603ba52e1ea42deaf3fdd799b1d8.tar.zst PeerTube-dba85a1e9e9f603ba52e1ea42deaf3fdd799b1d8.zip |
WIP plugins: add plugin settings/uninstall in client
Diffstat (limited to 'client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts')
-rw-r--r-- | client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts | 100 |
1 files changed, 98 insertions, 2 deletions
diff --git a/client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts b/client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts index f65599532..8750bfd38 100644 --- a/client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts +++ b/client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts | |||
@@ -1,14 +1,110 @@ | |||
1 | import { Component, OnInit } from '@angular/core' | 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 { RegisterSettingOptions } from '@shared/models/plugins/register-setting.model' | ||
10 | import { BuildFormArgument, BuildFormDefaultValues, FormReactive, FormValidatorService } from '@app/shared' | ||
2 | 11 | ||
3 | @Component({ | 12 | @Component({ |
4 | selector: 'my-plugin-show-installed', | 13 | selector: 'my-plugin-show-installed', |
5 | templateUrl: './plugin-show-installed.component.html', | 14 | templateUrl: './plugin-show-installed.component.html', |
6 | styleUrls: [ './plugin-show-installed.component.scss' ] | 15 | styleUrls: [ './plugin-show-installed.component.scss' ] |
7 | }) | 16 | }) |
8 | export class PluginShowInstalledComponent implements OnInit { | 17 | export class PluginShowInstalledComponent extends FormReactive implements OnInit, OnDestroy{ |
18 | plugin: PeerTubePlugin | ||
19 | registeredSettings: RegisterSettingOptions[] = [] | ||
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 | } | ||
9 | 33 | ||
10 | ngOnInit () { | 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 defaultValues: BuildFormDefaultValues = {} | ||
87 | const buildOptions: BuildFormArgument = {} | ||
88 | const settingsValues: any = {} | ||
89 | |||
90 | for (const setting of this.registeredSettings) { | ||
91 | buildOptions[ setting.name ] = null | ||
92 | settingsValues[ setting.name ] = this.getSetting(setting.name) | ||
93 | } | ||
94 | |||
95 | this.buildForm(buildOptions) | ||
96 | |||
97 | this.form.patchValue(settingsValues) | ||
98 | } | ||
99 | |||
100 | private getSetting (name: string) { | ||
101 | const settings = this.plugin.settings | ||
102 | |||
103 | if (settings && settings[name]) return settings[name] | ||
104 | |||
105 | const registered = this.registeredSettings.find(r => r.name === name) | ||
11 | 106 | ||
107 | return registered.default | ||
12 | } | 108 | } |
13 | 109 | ||
14 | } | 110 | } |