]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/plugins/plugin-list-installed/plugin-list-installed.component.ts
WIP plugins: add plugin settings/uninstall in client
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / plugin-list-installed / plugin-list-installed.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { PluginType } from '@shared/models/plugins/plugin.type'
3 import { I18n } from '@ngx-translate/i18n-polyfill'
4 import { PluginApiService } from '@app/+admin/plugins/shared/plugin-api.service'
5 import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model'
6 import { ConfirmService, Notifier } from '@app/core'
7 import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model'
8 import { ActivatedRoute, Router } from '@angular/router'
9
10 @Component({
11 selector: 'my-plugin-list-installed',
12 templateUrl: './plugin-list-installed.component.html',
13 styleUrls: [
14 '../shared/toggle-plugin-type.scss',
15 './plugin-list-installed.component.scss'
16 ]
17 })
18 export class PluginListInstalledComponent implements OnInit {
19 pluginTypeOptions: { label: string, value: PluginType }[] = []
20 pluginType: PluginType = PluginType.PLUGIN
21
22 pagination: ComponentPagination = {
23 currentPage: 1,
24 itemsPerPage: 10
25 }
26 sort = 'name'
27
28 plugins: PeerTubePlugin[] = []
29
30 constructor (
31 private i18n: I18n,
32 private pluginService: PluginApiService,
33 private notifier: Notifier,
34 private confirmService: ConfirmService,
35 private router: Router,
36 private route: ActivatedRoute
37 ) {
38 this.pluginTypeOptions = this.pluginService.getPluginTypeOptions()
39 }
40
41 ngOnInit () {
42 const query = this.route.snapshot.queryParams
43 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
44
45 this.reloadPlugins()
46 }
47
48 reloadPlugins () {
49 this.pagination.currentPage = 1
50 this.plugins = []
51
52 this.router.navigate([], { queryParams: { pluginType: this.pluginType }})
53
54 this.loadMorePlugins()
55 }
56
57 loadMorePlugins () {
58 this.pluginService.getPlugins(this.pluginType, this.pagination, this.sort)
59 .subscribe(
60 res => {
61 this.plugins = this.plugins.concat(res.data)
62 this.pagination.totalItems = res.total
63 },
64
65 err => this.notifier.error(err.message)
66 )
67 }
68
69 onNearOfBottom () {
70 if (!hasMoreItems(this.pagination)) return
71
72 this.pagination.currentPage += 1
73
74 this.loadMorePlugins()
75 }
76
77 getNoResultMessage () {
78 if (this.pluginType === PluginType.PLUGIN) {
79 return this.i18n('You don\'t have plugins installed yet.')
80 }
81
82 return this.i18n('You don\'t have themes installed yet.')
83 }
84
85 async uninstall (plugin: PeerTubePlugin) {
86 const res = await this.confirmService.confirm(
87 this.i18n('Do you really want to uninstall {{pluginName}}?', { pluginName: plugin.name }),
88 this.i18n('Uninstall')
89 )
90 if (res === false) return
91
92 this.pluginService.uninstall(plugin.name, plugin.type)
93 .subscribe(
94 () => {
95 this.notifier.success(this.i18n('{{pluginName}} uninstalled.', { pluginName: plugin.name }))
96
97 this.plugins = this.plugins.filter(p => p.name !== plugin.name)
98 this.pagination.totalItems--
99 },
100
101 err => this.notifier.error(err.message)
102 )
103 }
104
105 getShowRouterLink (plugin: PeerTubePlugin) {
106 return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, plugin.type) ]
107 }
108 }