mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-02 13:45:10 -05:00
new saved view service replaces old local storage based service
This commit is contained in:
parent
926e746005
commit
13d934dc6e
@ -1,10 +1,8 @@
|
||||
import { FilterRuleType } from './filter-rule-type';
|
||||
|
||||
export function cloneFilterRules(filterRules: FilterRule[]): FilterRule[] {
|
||||
if (filterRules) {
|
||||
let newRules: FilterRule[] = []
|
||||
for (let rule of filterRules) {
|
||||
newRules.push({type: rule.type, value: rule.value})
|
||||
newRules.push({rule_type: rule.rule_type, value: rule.value})
|
||||
}
|
||||
return newRules
|
||||
} else {
|
||||
@ -13,6 +11,6 @@ export function cloneFilterRules(filterRules: FilterRule[]): FilterRule[] {
|
||||
}
|
||||
|
||||
export interface FilterRule {
|
||||
type: FilterRuleType
|
||||
rule_type: number
|
||||
value: any
|
||||
}
|
18
src-ui/src/app/data/paperless-saved-view.ts
Normal file
18
src-ui/src/app/data/paperless-saved-view.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { FilterRule } from './filter-rule';
|
||||
import { ObjectWithId } from './object-with-id';
|
||||
|
||||
export interface PaperlessSavedView extends ObjectWithId {
|
||||
|
||||
name?: string
|
||||
|
||||
show_on_dashboard?: boolean
|
||||
|
||||
show_in_sidebar?: boolean
|
||||
|
||||
sort_field: string
|
||||
|
||||
sort_reverse: boolean
|
||||
|
||||
filter_rules: FilterRule[]
|
||||
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
import { FilterRule } from './filter-rule';
|
||||
|
||||
export interface SavedViewConfig {
|
||||
|
||||
id?: string
|
||||
|
||||
filterRules: FilterRule[]
|
||||
|
||||
sortField: string
|
||||
|
||||
sortDirection: string
|
||||
|
||||
title?: string
|
||||
|
||||
showInSideBar?: boolean
|
||||
|
||||
showInDashboard?: boolean
|
||||
|
||||
}
|
16
src-ui/src/app/services/rest/saved-view.service.spec.ts
Normal file
16
src-ui/src/app/services/rest/saved-view.service.spec.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { SavedViewService } from './saved-view.service';
|
||||
|
||||
describe('SavedViewService', () => {
|
||||
let service: SavedViewService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(SavedViewService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
53
src-ui/src/app/services/rest/saved-view.service.ts
Normal file
53
src-ui/src/app/services/rest/saved-view.service.ts
Normal file
@ -0,0 +1,53 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Subject } from 'rxjs';
|
||||
import { map, tap } from 'rxjs/operators';
|
||||
import { PaperlessSavedView } from 'src/app/data/paperless-saved-view';
|
||||
import { AbstractPaperlessService } from './abstract-paperless-service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class SavedViewService extends AbstractPaperlessService<PaperlessSavedView> {
|
||||
|
||||
constructor(http: HttpClient) {
|
||||
super(http, 'saved_views')
|
||||
this.reload()
|
||||
}
|
||||
|
||||
private reload() {
|
||||
this.listAll().subscribe(r => this.savedViews = r.results)
|
||||
}
|
||||
|
||||
private savedViews: PaperlessSavedView[] = []
|
||||
|
||||
get allViews() {
|
||||
return this.savedViews
|
||||
}
|
||||
|
||||
get sidebarViews() {
|
||||
return this.savedViews.filter(v => v.show_in_sidebar)
|
||||
}
|
||||
|
||||
get dashboardViews() {
|
||||
return this.savedViews.filter(v => v.show_on_dashboard)
|
||||
}
|
||||
|
||||
create(o: PaperlessSavedView) {
|
||||
return super.create(o).pipe(
|
||||
tap(() => this.reload())
|
||||
)
|
||||
}
|
||||
|
||||
update(o: PaperlessSavedView) {
|
||||
return super.update(o).pipe(
|
||||
tap(() => this.reload())
|
||||
)
|
||||
}
|
||||
|
||||
delete(o: PaperlessSavedView) {
|
||||
return super.delete(o).pipe(
|
||||
tap(() => this.reload())
|
||||
)
|
||||
}
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { SavedViewConfigService } from './saved-view-config.service';
|
||||
|
||||
describe('SavedViewConfigService', () => {
|
||||
let service: SavedViewConfigService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(SavedViewConfigService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
@ -1,66 +0,0 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { SavedViewConfig } from '../data/saved-view-config';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class SavedViewConfigService {
|
||||
|
||||
constructor() {
|
||||
let savedConfigs = localStorage.getItem('saved-view-config-service:savedConfigs')
|
||||
if (savedConfigs) {
|
||||
try {
|
||||
this.configs = JSON.parse(savedConfigs)
|
||||
} catch (e) {
|
||||
this.configs = []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private configs: SavedViewConfig[] = []
|
||||
|
||||
getConfigs(): SavedViewConfig[] {
|
||||
return this.configs
|
||||
}
|
||||
|
||||
getDashboardConfigs(): SavedViewConfig[] {
|
||||
return this.configs.filter(sf => sf.showInDashboard)
|
||||
}
|
||||
|
||||
getSideBarConfigs(): SavedViewConfig[] {
|
||||
return this.configs.filter(sf => sf.showInSideBar)
|
||||
}
|
||||
|
||||
getConfig(id: string): SavedViewConfig {
|
||||
return this.configs.find(sf => sf.id == id)
|
||||
}
|
||||
|
||||
newConfig(config: SavedViewConfig) {
|
||||
config.id = uuidv4()
|
||||
this.configs.push(config)
|
||||
|
||||
this.save()
|
||||
}
|
||||
|
||||
updateConfig(config: SavedViewConfig) {
|
||||
let savedConfig = this.configs.find(c => c.id == config.id)
|
||||
if (savedConfig) {
|
||||
Object.assign(savedConfig, config)
|
||||
this.save()
|
||||
}
|
||||
}
|
||||
|
||||
private save() {
|
||||
localStorage.setItem('saved-view-config-service:savedConfigs', JSON.stringify(this.configs))
|
||||
}
|
||||
|
||||
deleteConfig(config: SavedViewConfig) {
|
||||
let index = this.configs.findIndex(vc => vc.id == config.id)
|
||||
if (index != -1) {
|
||||
this.configs.splice(index, 1)
|
||||
this.save()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user