some refactoring.

This commit is contained in:
jonaswinkler 2020-12-14 11:22:24 +01:00
parent 98ab79ad5a
commit 02c1d496d6
3 changed files with 24 additions and 31 deletions

View File

@ -3,7 +3,7 @@
<div class="text-muted mt-1">Filter by:</div>
</div>
<div class="col">
<input class="form-control form-control-sm" type="text" [(ngModel)]="filterText" placeholder="Title">
<input class="form-control form-control-sm" type="text" [(ngModel)]="titleFilter" placeholder="Title">
</div>
<app-filter-dropdown class="col-auto" [(items)]="filterEditorService.tags" [itemsSelected]="filterEditorService.selectedTags" [title]="'Tags'" (toggle)="onToggleTag($event)"></app-filter-dropdown>

View File

@ -25,29 +25,31 @@ export class FilterEditorComponent implements OnInit, OnDestroy {
@Output()
apply = new EventEmitter()
get filterText() {
return this.filterEditorService.filterText
get titleFilter() {
return this.filterEditorService.titleFilter
}
set filterText(value) {
this.filterTextDebounce.next(value)
set titleFilter(value) {
this.titleFilterDebounce.next(value)
}
filterTextDebounce: Subject<string>
titleFilterDebounce: Subject<string>
subscription: Subscription
ngOnInit() {
this.filterTextDebounce = new Subject<string>()
this.subscription = this.filterTextDebounce.pipe(
this.titleFilterDebounce = new Subject<string>()
this.subscription = this.titleFilterDebounce.pipe(
debounceTime(400),
distinctUntilChanged()
).subscribe(title => {
this.filterEditorService.filterText = title
this.filterEditorService.titleFilter = title
this.applyFilters()
})
}
ngOnDestroy() {
this.titleFilterDebounce.complete()
// TODO: not sure if both is necessary
this.subscription.unsubscribe()
}

View File

@ -16,9 +16,6 @@ import { NgbDate, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';
providedIn: 'root'
})
export class FilterEditorViewService {
private tags$: Observable<Results<PaperlessTag>>
private correspondents$: Observable<Results<PaperlessCorrespondent>>
private documentTypes$: Observable<Results<PaperlessDocumentType>>
tags: PaperlessTag[] = []
correspondents: PaperlessCorrespondent[]
@ -27,12 +24,9 @@ export class FilterEditorViewService {
filterRules: FilterRule[] = []
constructor(private tagService: TagService, private documentTypeService: DocumentTypeService, private correspondentService: CorrespondentService) {
this.tags$ = this.tagService.listAll()
this.tags$.subscribe(result => this.tags = result.results)
this.correspondents$ = this.correspondentService.listAll()
this.correspondents$.subscribe(result => this.correspondents = result.results)
this.documentTypes$ = this.documentTypeService.listAll()
this.documentTypes$.subscribe(result => this.documentTypes = result.results)
this.tagService.listAll().subscribe(result => this.tags = result.results)
this.correspondentService.listAll().subscribe(result => this.correspondents = result.results)
this.documentTypeService.listAll().subscribe(result => this.documentTypes = result.results)
}
clear() {
@ -43,22 +37,19 @@ export class FilterEditorViewService {
return this.filterRules.length > 0
}
set filterText(text: string) {
let filterRules = this.filterRules
let existingRule = filterRules.find(rule => rule.type.id == FILTER_TITLE)
if (existingRule && (!text || text.length == 0)) {
filterRules.splice(filterRules.findIndex(rule => rule.type.id == FILTER_TITLE), 1)
} else if (existingRule && existingRule.value == text) {
return
} else if (existingRule) {
existingRule.value = text
} else {
filterRules.push({type: FILTER_RULE_TYPES.find(t => t.id == FILTER_TITLE), value: text})
set titleFilter(title: string) {
let existingRule = this.filterRules.find(rule => rule.type.id == FILTER_TITLE)
if (!existingRule && title) {
this.filterRules.push({type: FILTER_RULE_TYPES.find(t => t.id == FILTER_TITLE), value: title})
} else if (existingRule && !title) {
this.filterRules.splice(this.filterRules.findIndex(rule => rule.type.id == FILTER_TITLE), 1)
} else if (existingRule && title) {
existingRule.value = title
}
this.filterRules = filterRules
}
get filterText(): string {
get titleFilter(): string {
let existingRule = this.filterRules.find(rule => rule.type.id == FILTER_TITLE)
return existingRule ? existingRule.value : ''
}