Query vars for filtering

This commit is contained in:
Michael Shamoon 2022-03-26 23:18:12 -07:00
parent 7c2ae129d7
commit 34b317da7a
2 changed files with 69 additions and 10 deletions

View File

@ -1,4 +1,5 @@
import { import {
AfterViewInit,
Component, Component,
OnDestroy, OnDestroy,
OnInit, OnInit,
@ -6,11 +7,14 @@ import {
ViewChild, ViewChild,
ViewChildren, ViewChildren,
} from '@angular/core' } from '@angular/core'
import { ActivatedRoute, Router } from '@angular/router' import { ActivatedRoute, ParamMap, Router } from '@angular/router'
import { NgbModal } from '@ng-bootstrap/ng-bootstrap' import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
import { Subscription } from 'rxjs' import { filter, Subscription } from 'rxjs'
import { FilterRule, isFullTextFilterRule } from 'src/app/data/filter-rule' import { FilterRule, isFullTextFilterRule } from 'src/app/data/filter-rule'
import { FILTER_FULLTEXT_MORELIKE } from 'src/app/data/filter-rule-type' import {
FILTER_FULLTEXT_MORELIKE,
FILTER_RULE_TYPES,
} from 'src/app/data/filter-rule-type'
import { PaperlessDocument } from 'src/app/data/paperless-document' import { PaperlessDocument } from 'src/app/data/paperless-document'
import { PaperlessSavedView } from 'src/app/data/paperless-saved-view' import { PaperlessSavedView } from 'src/app/data/paperless-saved-view'
import { import {
@ -20,6 +24,7 @@ import {
import { ConsumerStatusService } from 'src/app/services/consumer-status.service' import { ConsumerStatusService } from 'src/app/services/consumer-status.service'
import { DocumentListViewService } from 'src/app/services/document-list-view.service' import { DocumentListViewService } from 'src/app/services/document-list-view.service'
import { import {
DocumentService,
DOCUMENT_SORT_FIELDS, DOCUMENT_SORT_FIELDS,
DOCUMENT_SORT_FIELDS_FULLTEXT, DOCUMENT_SORT_FIELDS_FULLTEXT,
} from 'src/app/services/rest/document.service' } from 'src/app/services/rest/document.service'
@ -28,14 +33,17 @@ import { ToastService } from 'src/app/services/toast.service'
import { FilterEditorComponent } from './filter-editor/filter-editor.component' import { FilterEditorComponent } from './filter-editor/filter-editor.component'
import { SaveViewConfigDialogComponent } from './save-view-config-dialog/save-view-config-dialog.component' import { SaveViewConfigDialogComponent } from './save-view-config-dialog/save-view-config-dialog.component'
const filterQueryVars: string[] = FILTER_RULE_TYPES.map((rt) => rt.filtervar)
@Component({ @Component({
selector: 'app-document-list', selector: 'app-document-list',
templateUrl: './document-list.component.html', templateUrl: './document-list.component.html',
styleUrls: ['./document-list.component.scss'], styleUrls: ['./document-list.component.scss'],
}) })
export class DocumentListComponent implements OnInit, OnDestroy { export class DocumentListComponent implements OnInit, OnDestroy, AfterViewInit {
constructor( constructor(
public list: DocumentListViewService, public list: DocumentListViewService,
private documentService: DocumentService,
public savedViewService: SavedViewService, public savedViewService: SavedViewService,
public route: ActivatedRoute, public route: ActivatedRoute,
private router: Router, private router: Router,
@ -85,13 +93,16 @@ export class DocumentListComponent implements OnInit, OnDestroy {
if (localStorage.getItem('document-list:displayMode') != null) { if (localStorage.getItem('document-list:displayMode') != null) {
this.displayMode = localStorage.getItem('document-list:displayMode') this.displayMode = localStorage.getItem('document-list:displayMode')
} }
this.consumptionFinishedSubscription = this.consumerStatusService this.consumptionFinishedSubscription = this.consumerStatusService
.onDocumentConsumptionFinished() .onDocumentConsumptionFinished()
.subscribe(() => { .subscribe(() => {
this.list.reload() this.list.reload()
}) })
this.route.paramMap.subscribe((params) => {
if (params.has('id')) { this.route.paramMap
.pipe(filter((params) => params.has('id'))) // only on saved view
.subscribe((params) => {
this.savedViewService.getCached(+params.get('id')).subscribe((view) => { this.savedViewService.getCached(+params.get('id')).subscribe((view) => {
if (!view) { if (!view) {
this.router.navigate(['404']) this.router.navigate(['404'])
@ -101,11 +112,57 @@ export class DocumentListComponent implements OnInit, OnDestroy {
this.list.reload() this.list.reload()
this.unmodifiedFilterRules = view.filter_rules this.unmodifiedFilterRules = view.filter_rules
}) })
} else { })
this.route.queryParamMap
.pipe(filter((qp) => !this.route.snapshot.paramMap.has('id'))) // only when not on saved view
.subscribe((queryParams) => {
const queryVarsFilterRules = []
filterQueryVars.forEach((filterQueryVar) => {
if (queryParams.has(filterQueryVar)) {
const value = queryParams.get(filterQueryVar)
if (value.split(',').length > 1) {
value.split(',').forEach((splitVal) => {
queryVarsFilterRules.push({
rule_type: FILTER_RULE_TYPES.find(
(rt) => rt.filtervar == filterQueryVar
).id,
value: splitVal,
})
})
} else {
queryVarsFilterRules.push({
rule_type: FILTER_RULE_TYPES.find(
(rt) => rt.filtervar == filterQueryVar
).id,
value: value,
})
}
}
})
this.list.activateSavedView(null) this.list.activateSavedView(null)
this.list.filterRules = queryVarsFilterRules
this.list.reload() this.list.reload()
this.unmodifiedFilterRules = [] this.unmodifiedFilterRules = []
} })
}
ngAfterViewInit(): void {
this.filterEditor.filterRulesChange.subscribe({
next: (rules) => {
const params = this.documentService.queryParams
// if we were on a saved view we navigate 'away' to /documents
let base = []
if (this.route.snapshot.paramMap.has('id')) base = ['/documents']
this.router.navigate(base, {
relativeTo: this.route,
queryParams: params,
})
},
}) })
} }

View File

@ -47,6 +47,7 @@ export interface SelectionData {
}) })
export class DocumentService extends AbstractPaperlessService<PaperlessDocument> { export class DocumentService extends AbstractPaperlessService<PaperlessDocument> {
private _searchQuery: string private _searchQuery: string
public queryParams: Object = {}
constructor( constructor(
http: HttpClient, http: HttpClient,
@ -57,7 +58,7 @@ export class DocumentService extends AbstractPaperlessService<PaperlessDocument>
super(http, 'documents') super(http, 'documents')
} }
private filterRulesToQueryParams(filterRules: FilterRule[]) { private filterRulesToQueryParams(filterRules: FilterRule[]): Object {
if (filterRules) { if (filterRules) {
let params = {} let params = {}
for (let rule of filterRules) { for (let rule of filterRules) {
@ -101,12 +102,13 @@ export class DocumentService extends AbstractPaperlessService<PaperlessDocument>
filterRules?: FilterRule[], filterRules?: FilterRule[],
extraParams = {} extraParams = {}
): Observable<Results<PaperlessDocument>> { ): Observable<Results<PaperlessDocument>> {
this.queryParams = this.filterRulesToQueryParams(filterRules)
return this.list( return this.list(
page, page,
pageSize, pageSize,
sortField, sortField,
sortReverse, sortReverse,
Object.assign(extraParams, this.filterRulesToQueryParams(filterRules)) Object.assign(extraParams, this.queryParams)
).pipe( ).pipe(
map((results) => { map((results) => {
results.results.forEach((doc) => this.addObservablesToDocument(doc)) results.results.forEach((doc) => this.addObservablesToDocument(doc))