current view persists during session, code cleanup

This commit is contained in:
Jonas Winkler
2020-11-08 00:07:31 +01:00
parent 3d67af1411
commit 0181651375
8 changed files with 99 additions and 66 deletions

View File

@@ -3,8 +3,8 @@ import { Observable } from 'rxjs';
import { cloneFilterRules, FilterRule } from '../data/filter-rule';
import { PaperlessDocument } from '../data/paperless-document';
import { SavedViewConfig } from '../data/saved-view-config';
import { GENERAL_SETTINGS } from '../data/storage-keys';
import { DocumentService, SORT_DIRECTION_DESCENDING } from './rest/document.service';
import { DOCUMENT_LIST_SERVICE, GENERAL_SETTINGS } from '../data/storage-keys';
import { DocumentService } from './rest/document.service';
@Injectable({
@@ -18,33 +18,24 @@ export class DocumentListViewService {
currentPage = 1
currentPageSize: number = +localStorage.getItem(GENERAL_SETTINGS.DOCUMENT_LIST_SIZE) || GENERAL_SETTINGS.DOCUMENT_LIST_SIZE_DEFAULT
collectionSize: number
currentFilterRules: FilterRule[] = []
currentSortDirection = SORT_DIRECTION_DESCENDING
currentSortField = DocumentListViewService.DEFAULT_SORT_FIELD
viewConfig: SavedViewConfig
private currentViewConfig: SavedViewConfig
//TODO: make private
viewConfigOverride: SavedViewConfig
get viewId() {
return this.viewConfigOverride?.id
}
reload(onFinish?) {
let sortField: string
let sortDirection: string
let filterRules: FilterRule[]
if (this.viewConfig) {
sortField = this.viewConfig.sortField
sortDirection = this.viewConfig.sortDirection
filterRules = this.viewConfig.filterRules
} else {
sortField = this.currentSortField
sortDirection = this.currentSortDirection
filterRules = this.currentFilterRules
}
let viewConfig = this.viewConfigOverride || this.currentViewConfig
this.documentService.list(
this.currentPage,
this.currentPageSize,
sortField,
sortDirection,
filterRules).subscribe(
viewConfig.sortField,
viewConfig.sortDirection,
viewConfig.filterRules).subscribe(
result => {
this.collectionSize = result.count
this.documents = result.results
@@ -60,9 +51,43 @@ export class DocumentListViewService {
})
}
set filterRules(filterRules: FilterRule[]) {
this.currentViewConfig.filterRules = cloneFilterRules(filterRules)
this.saveCurrentViewConfig()
this.reload()
}
setFilterRules(filterRules: FilterRule[]) {
this.currentFilterRules = cloneFilterRules(filterRules)
get filterRules(): FilterRule[] {
return cloneFilterRules(this.currentViewConfig.filterRules)
}
set sortField(field: string) {
this.currentViewConfig.sortField = field
this.saveCurrentViewConfig()
this.reload()
}
get sortField(): string {
return this.currentViewConfig.sortField
}
set sortDirection(direction: string) {
this.currentViewConfig.sortDirection = direction
this.saveCurrentViewConfig()
this.reload()
}
get sortDirection(): string {
return this.currentViewConfig.sortDirection
}
loadViewConfig(config: SavedViewConfig) {
Object.assign(this.currentViewConfig, config)
this.reload()
}
private saveCurrentViewConfig() {
sessionStorage.setItem(DOCUMENT_LIST_SERVICE.CURRENT_VIEW_CONFIG, JSON.stringify(this.currentViewConfig))
}
getLastPage(): number {
@@ -108,5 +133,22 @@ export class DocumentListViewService {
}
}
constructor(private documentService: DocumentService) { }
constructor(private documentService: DocumentService) {
let currentViewConfigJson = sessionStorage.getItem(DOCUMENT_LIST_SERVICE.CURRENT_VIEW_CONFIG)
if (currentViewConfigJson) {
try {
this.currentViewConfig = JSON.parse(currentViewConfigJson)
} catch (e) {
sessionStorage.removeItem(DOCUMENT_LIST_SERVICE.CURRENT_VIEW_CONFIG)
this.currentViewConfig = null
}
}
if (!this.currentViewConfig) {
this.currentViewConfig = {
filterRules: [],
sortDirection: 'des',
sortField: 'created'
}
}
}
}

View File

@@ -10,7 +10,11 @@ export class SavedViewConfigService {
constructor() {
let savedConfigs = localStorage.getItem('saved-view-config-service:savedConfigs')
if (savedConfigs) {
this.configs = JSON.parse(savedConfigs)
try {
this.configs = JSON.parse(savedConfigs)
} catch (e) {
this.configs = []
}
}
}