1 import debug from 'debug'
2 import { LazyLoadEvent, SortMeta } from 'primeng/api'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { logger } from '@root-helpers/logger'
5 import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'
6 import { RestPagination } from './rest-pagination'
8 const debugLogger = debug('peertube:tables:RestTable')
10 export abstract class RestTable {
12 abstract totalRecords: number
13 abstract sort: SortMeta
14 abstract pagination: RestPagination
16 rowsPerPageOptions = [ 10, 20, 50, 100 ]
17 rowsPerPage = this.rowsPerPageOptions[0]
22 protected route: ActivatedRoute
23 protected router: Router
25 abstract getIdentifier (): string
32 const result = peertubeLocalStorage.getItem(this.getSortLocalStorageKey())
36 this.sort = JSON.parse(result)
38 logger.error('Cannot load sort of local storage key ' + this.getSortLocalStorageKey(), err)
44 peertubeLocalStorage.setItem(this.getSortLocalStorageKey(), JSON.stringify(this.sort))
47 loadLazy (event: LazyLoadEvent) {
48 debugLogger('Load lazy %o.', event)
51 order: event.sortOrder,
52 field: event.sortField
55 this.rowsPerPage = event.rows
59 count: this.rowsPerPage
62 this.expandedRows = {}
68 onSearch (search: string) {
71 count: this.rowsPerPage
78 protected abstract reloadData (): void
80 private getSortLocalStorageKey () {
81 return 'rest-table-sort-' + this.getIdentifier()