]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/jobs/jobs-list/jobs-list.component.ts
Improve admin tables
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / jobs / jobs-list / jobs-list.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { NotificationsService } from 'angular2-notifications'
3 import { SortMeta } from 'primeng/primeng'
4 import { Job } from '../../../../../../shared/index'
5 import { JobState } from '../../../../../../shared/models'
6 import { RestPagination, RestTable } from '../../../shared'
7 import { viewportHeight } from '../../../shared/misc/utils'
8 import { JobService } from '../shared'
9 import { RestExtractor } from '../../../shared/rest/rest-extractor.service'
10
11 @Component({
12 selector: 'my-jobs-list',
13 templateUrl: './jobs-list.component.html',
14 styleUrls: [ './jobs-list.component.scss' ]
15 })
16 export class JobsListComponent extends RestTable implements OnInit {
17 private static JOB_STATE_LOCAL_STORAGE_STATE = 'jobs-list-state'
18
19 jobState: JobState = 'inactive'
20 jobStates: JobState[] = [ 'active', 'complete', 'failed', 'inactive', 'delayed' ]
21 jobs: Job[] = []
22 totalRecords: number
23 rowsPerPage = 10
24 sort: SortMeta = { field: 'createdAt', order: -1 }
25 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
26 scrollHeight = ''
27
28 constructor (
29 private notificationsService: NotificationsService,
30 private restExtractor: RestExtractor,
31 private jobsService: JobService
32 ) {
33 super()
34 }
35
36 ngOnInit () {
37 // 380 -> headers + footer...
38 this.scrollHeight = (viewportHeight() - 380) + 'px'
39
40 this.loadJobState()
41 this.loadSort()
42 }
43
44 onJobStateChanged () {
45 this.loadData()
46 this.saveJobState()
47 }
48
49 protected loadData () {
50 this.jobsService
51 .getJobs(this.jobState, this.pagination, this.sort)
52 .subscribe(
53 resultList => {
54 this.jobs = resultList.data
55 this.totalRecords = resultList.total
56 },
57
58 err => this.notificationsService.error('Error', err.message)
59 )
60 }
61
62 private loadJobState () {
63 const result = localStorage.getItem(JobsListComponent.JOB_STATE_LOCAL_STORAGE_STATE)
64
65 if (result) this.jobState = result as JobState
66 }
67
68 private saveJobState () {
69 localStorage.setItem(JobsListComponent.JOB_STATE_LOCAL_STORAGE_STATE, this.jobState)
70 }
71 }