]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/plugins/plugin-search/plugin-search.component.ts
Update angular
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / plugin-search / plugin-search.component.ts
CommitLineData
6702a1b2 1import { Component, OnInit } from '@angular/core'
89c344db 2import { Notifier, ServerService } from '@app/core'
6702a1b2 3import { ConfirmService } from '../../../core'
d00dc28d 4import { I18n } from '@ngx-translate/i18n-polyfill'
d00dc28d
C
5import { PluginType } from '@shared/models/plugins/plugin.type'
6import { PluginApiService } from '@app/+admin/plugins/shared/plugin-api.service'
6702a1b2
C
7import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model'
8import { ActivatedRoute, Router } from '@angular/router'
9import { PeerTubePluginIndex } from '@shared/models/plugins/peertube-plugin-index.model'
10import { Subject } from 'rxjs'
11import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
d00dc28d
C
12
13@Component({
14 selector: 'my-plugin-search',
15 templateUrl: './plugin-search.component.html',
dba85a1e
C
16 styleUrls: [
17 '../shared/toggle-plugin-type.scss',
6702a1b2 18 '../shared/plugin-list.component.scss',
dba85a1e
C
19 './plugin-search.component.scss'
20 ]
d00dc28d
C
21})
22export class PluginSearchComponent implements OnInit {
23 pluginTypeOptions: { label: string, value: PluginType }[] = []
6702a1b2
C
24 pluginType: PluginType = PluginType.PLUGIN
25
26 pagination: ComponentPagination = {
27 currentPage: 1,
28 itemsPerPage: 10
29 }
30 sort = '-popularity'
31
32 search = ''
33 isSearching = false
34
35 plugins: PeerTubePluginIndex[] = []
36 installing: { [name: string]: boolean } = {}
89c344db 37 pluginInstalled = false
6702a1b2
C
38
39 private searchSubject = new Subject<string>()
d00dc28d
C
40
41 constructor (
89c344db 42 private server: ServerService,
d00dc28d 43 private i18n: I18n,
6702a1b2
C
44 private pluginService: PluginApiService,
45 private notifier: Notifier,
46 private confirmService: ConfirmService,
47 private router: Router,
48 private route: ActivatedRoute
d00dc28d
C
49 ) {
50 this.pluginTypeOptions = this.pluginService.getPluginTypeOptions()
51 }
52
53 ngOnInit () {
6702a1b2
C
54 const query = this.route.snapshot.queryParams
55 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
56
57 this.searchSubject.asObservable()
58 .pipe(
59 debounceTime(400),
60 distinctUntilChanged()
61 )
62 .subscribe(search => {
63 this.search = search
64 this.reloadPlugins()
65 })
66
67 this.reloadPlugins()
68 }
69
70 onSearchChange (search: string) {
71 this.searchSubject.next(search)
72 }
73
74 reloadPlugins () {
75 this.pagination.currentPage = 1
76 this.plugins = []
77
78 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
79
80 this.loadMorePlugins()
81 }
82
83 loadMorePlugins () {
84 this.isSearching = true
85
86 this.pluginService.searchAvailablePlugins(this.pluginType, this.pagination, this.sort, this.search)
87 .subscribe(
88 res => {
89 this.isSearching = false
90
91 this.plugins = this.plugins.concat(res.data)
92 this.pagination.totalItems = res.total
93 },
94
f0c5e8b6
C
95 err => {
96 console.error(err)
97
98 const message = this.i18n('The plugin index is not available. Please retry later.')
99 this.notifier.error(message)
100 }
6702a1b2
C
101 )
102 }
103
104 onNearOfBottom () {
105 if (!hasMoreItems(this.pagination)) return
106
107 this.pagination.currentPage += 1
108
109 this.loadMorePlugins()
110 }
111
112 isInstalling (plugin: PeerTubePluginIndex) {
113 return !!this.installing[plugin.npmName]
114 }
115
116 async install (plugin: PeerTubePluginIndex) {
117 if (this.installing[plugin.npmName]) return
118
119 const res = await this.confirmService.confirm(
120 this.i18n('Please only install plugins or themes you trust, since they can execute any code on your instance.'),
121 this.i18n('Install {{pluginName}}?', { pluginName: plugin.name })
122 )
123 if (res === false) return
124
125 this.installing[plugin.npmName] = true
126
127 this.pluginService.install(plugin.npmName)
128 .subscribe(
129 () => {
130 this.installing[plugin.npmName] = false
89c344db 131 this.pluginInstalled = true
6702a1b2
C
132
133 this.notifier.success(this.i18n('{{pluginName}} installed.', { pluginName: plugin.name }))
134
135 plugin.installed = true
136 },
137
138 err => this.notifier.error(err.message)
139 )
d00dc28d
C
140 }
141}