]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/rest/rest-table.ts
Better spacing beetween comments
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / rest / rest-table.ts
index d04d91c688b046486031653aa9fa115c8dcafd66..c180346af066bc4b45af2cec7636248cb366b142 100644 (file)
@@ -1,15 +1,37 @@
+import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage'
 import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent'
 import { SortMeta } from 'primeng/components/common/sortmeta'
-
 import { RestPagination } from './rest-pagination'
+import { Subject } from 'rxjs'
+import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
 
 export abstract class RestTable {
+
   abstract totalRecords: number
   abstract rowsPerPage: number
   abstract sort: SortMeta
   abstract pagination: RestPagination
 
-  protected abstract loadData (): void
+  protected search: string
+  private searchStream: Subject<string>
+  private sortLocalStorageKey = 'rest-table-sort-' + this.constructor.name
+
+  initialize () {
+    this.loadSort()
+    this.initSearch()
+  }
+
+  loadSort () {
+    const result = peertubeLocalStorage.getItem(this.sortLocalStorageKey)
+
+    if (result) {
+      try {
+        this.sort = JSON.parse(result)
+      } catch (err) {
+        console.error('Cannot load sort of local storage key ' + this.sortLocalStorageKey, err)
+      }
+    }
+  }
 
   loadLazy (event: LazyLoadEvent) {
     this.sort = {
@@ -23,6 +45,30 @@ export abstract class RestTable {
     }
 
     this.loadData()
+    this.saveSort()
   }
 
+  saveSort () {
+    peertubeLocalStorage.setItem(this.sortLocalStorageKey, JSON.stringify(this.sort))
+  }
+
+  initSearch () {
+    this.searchStream = new Subject()
+
+    this.searchStream
+      .pipe(
+        debounceTime(400),
+        distinctUntilChanged()
+      )
+      .subscribe(search => {
+        this.search = search
+        this.loadData()
+      })
+  }
+
+  onSearch (search: string) {
+    this.searchStream.next(search)
+  }
+
+  protected abstract loadData (): void
 }