1 import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage'
2 import { LazyLoadEvent, SortMeta } from 'primeng/api'
3 import { RestPagination } from './rest-pagination'
4 import { Subject } from 'rxjs'
5 import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
7 export abstract class RestTable {
9 abstract totalRecords: number
10 abstract sort: SortMeta
11 abstract pagination: RestPagination
14 rowsPerPageOptions = [ 10, 20, 50, 100 ]
15 rowsPerPage = this.rowsPerPageOptions[0]
17 private searchStream: Subject<string>
19 abstract getIdentifier (): string
27 const result = peertubeLocalStorage.getItem(this.getSortLocalStorageKey())
31 this.sort = JSON.parse(result)
33 console.error('Cannot load sort of local storage key ' + this.getSortLocalStorageKey(), err)
38 loadLazy (event: LazyLoadEvent) {
40 order: event.sortOrder,
41 field: event.sortField
46 count: this.rowsPerPage
54 peertubeLocalStorage.setItem(this.getSortLocalStorageKey(), JSON.stringify(this.sort))
58 this.searchStream = new Subject()
63 distinctUntilChanged()
65 .subscribe(search => {
71 onSearch (event: Event) {
72 const target = event.target as HTMLInputElement
73 this.searchStream.next(target.value)
76 protected abstract loadData (): void
78 private getSortLocalStorageKey () {
79 return 'rest-table-sort-' + this.getIdentifier()