From b52cb193e16d604f36e39077c9bdca35be149dd1 Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Tue, 25 Oct 2022 13:34:36 -0700 Subject: [PATCH] Remove relative date query strings from dropdown --- .../date-dropdown.component.html | 6 +- .../date-dropdown/date-dropdown.component.ts | 62 +++++------- .../filter-editor.component.html | 4 +- .../filter-editor/filter-editor.component.ts | 97 ++++++++++++++----- 4 files changed, 98 insertions(+), 71 deletions(-) diff --git a/src-ui/src/app/components/common/date-dropdown/date-dropdown.component.html b/src-ui/src/app/components/common/date-dropdown/date-dropdown.component.html index 0327ee963..c99c71cda 100644 --- a/src-ui/src/app/components/common/date-dropdown/date-dropdown.component.html +++ b/src-ui/src/app/components/common/date-dropdown/date-dropdown.component.html @@ -7,13 +7,13 @@ diff --git a/src-ui/src/app/components/document-list/filter-editor/filter-editor.component.ts b/src-ui/src/app/components/document-list/filter-editor/filter-editor.component.ts index 734242be1..0c4cc37cb 100644 --- a/src-ui/src/app/components/document-list/filter-editor/filter-editor.component.ts +++ b/src-ui/src/app/components/document-list/filter-editor/filter-editor.component.ts @@ -44,6 +44,7 @@ import { DocumentService } from 'src/app/services/rest/document.service' import { PaperlessDocument } from 'src/app/data/paperless-document' import { PaperlessStoragePath } from 'src/app/data/paperless-storage-path' import { StoragePathService } from 'src/app/services/rest/storage-path.service' +import { RelativeDate } from '../../common/date-dropdown/date-dropdown.component' const TEXT_FILTER_TARGET_TITLE = 'title' const TEXT_FILTER_TARGET_TITLE_CONTENT = 'title-content' @@ -59,6 +60,24 @@ const TEXT_FILTER_MODIFIER_LT = 'less' const RELATIVE_DATE_QUERY_REGEXP_CREATED = /created:\[([^\]]+)\]/g const RELATIVE_DATE_QUERY_REGEXP_ADDED = /added:\[([^\]]+)\]/g +const RELATIVE_DATE_QUERYSTRINGS = [ + { + relativeDate: RelativeDate.LAST_7_DAYS, + dateQuery: '-1 week to now', + }, + { + relativeDate: RelativeDate.LAST_MONTH, + dateQuery: '-1 month to now', + }, + { + relativeDate: RelativeDate.LAST_3_MONTHS, + dateQuery: '-3 month to now', + }, + { + relativeDate: RelativeDate.LAST_YEAR, + dateQuery: '-1 year to now', + }, +] @Component({ selector: 'app-filter-editor', @@ -200,8 +219,8 @@ export class FilterEditorComponent implements OnInit, OnDestroy { dateCreatedAfter: string dateAddedBefore: string dateAddedAfter: string - dateCreatedQuery: string - dateAddedQuery: string + dateCreatedRelativeDate: RelativeDate + dateAddedRelativeDate: RelativeDate _unmodifiedFilterRules: FilterRule[] = [] _filterRules: FilterRule[] = [] @@ -233,8 +252,8 @@ export class FilterEditorComponent implements OnInit, OnDestroy { this.dateAddedAfter = null this.dateCreatedBefore = null this.dateCreatedAfter = null - this.dateCreatedQuery = null - this.dateAddedQuery = null + this.dateCreatedRelativeDate = null + this.dateAddedRelativeDate = null this.textFilterModifier = TEXT_FILTER_MODIFIER_EQUALS value.forEach((rule) => { @@ -258,7 +277,10 @@ export class FilterEditorComponent implements OnInit, OnDestroy { ;[...arg.matchAll(RELATIVE_DATE_QUERY_REGEXP_CREATED)].forEach( (match) => { if (match[1]?.length) { - this.dateCreatedQuery = match[1] + this.dateCreatedRelativeDate = + RELATIVE_DATE_QUERYSTRINGS.find( + (qS) => qS.dateQuery == match[1] + )?.relativeDate } } ) @@ -268,7 +290,10 @@ export class FilterEditorComponent implements OnInit, OnDestroy { ;[...arg.matchAll(RELATIVE_DATE_QUERY_REGEXP_ADDED)].forEach( (match) => { if (match[1]?.length) { - this.dateAddedQuery = match[1] + this.dateAddedRelativeDate = + RELATIVE_DATE_QUERYSTRINGS.find( + (qS) => qS.dateQuery == match[1] + )?.relativeDate } } ) @@ -501,26 +526,45 @@ export class FilterEditorComponent implements OnInit, OnDestroy { value: this.dateAddedAfter, }) } - if (this.dateAddedQuery || this.dateCreatedQuery) { + if ( + this.dateAddedRelativeDate !== null || + this.dateCreatedRelativeDate !== null + ) { let queryArgs: Array = [] - if (this.dateCreatedQuery) - queryArgs.push(`created:[${this.dateCreatedQuery}]`) - if (this.dateAddedQuery) queryArgs.push(`added:[${this.dateAddedQuery}]`) const existingRule = filterRules.find( (fr) => fr.rule_type == FILTER_FULLTEXT_QUERY ) + let existingRuleArgs = existingRule?.value.split(',') + if (this.dateCreatedRelativeDate !== null) { + queryArgs.push( + `created:[${ + RELATIVE_DATE_QUERYSTRINGS.find( + (qS) => qS.relativeDate == this.dateCreatedRelativeDate + ).dateQuery + }]` + ) + if (existingRule) { + queryArgs = existingRuleArgs + .filter((arg) => !arg.match(RELATIVE_DATE_QUERY_REGEXP_CREATED)) + .concat(queryArgs) + } + } + if (this.dateAddedRelativeDate !== null) { + queryArgs.push( + `added:[${ + RELATIVE_DATE_QUERYSTRINGS.find( + (qS) => qS.relativeDate == this.dateAddedRelativeDate + ).dateQuery + }]` + ) + if (existingRule) { + queryArgs = existingRuleArgs + .filter((arg) => !arg.match(RELATIVE_DATE_QUERY_REGEXP_ADDED)) + .concat(queryArgs) + } + } + if (existingRule) { - let existingRuleArgs = existingRule.value.split(',') - if (this.dateCreatedQuery) { - queryArgs = existingRuleArgs - .filter((arg) => !arg.includes('created:')) - .concat(queryArgs) - } - if (this.dateAddedQuery) { - queryArgs = existingRuleArgs - .filter((arg) => !arg.includes('added:')) - .concat(queryArgs) - } existingRule.value = queryArgs.join(',') } else { filterRules.push({ @@ -529,13 +573,16 @@ export class FilterEditorComponent implements OnInit, OnDestroy { }) } } - if (!this.dateAddedQuery && !this.dateCreatedQuery) { + if ( + this.dateCreatedRelativeDate == null && + this.dateAddedRelativeDate == null + ) { const existingRule = filterRules.find( (fr) => fr.rule_type == FILTER_FULLTEXT_QUERY ) if ( - existingRule?.value.includes('created:') || - existingRule?.value.includes('added:') + existingRule?.value.match(RELATIVE_DATE_QUERY_REGEXP_CREATED) || + existingRule?.value.match(RELATIVE_DATE_QUERY_REGEXP_ADDED) ) { // remove any existing date query existingRule.value = existingRule.value @@ -660,8 +707,6 @@ export class FilterEditorComponent implements OnInit, OnDestroy { target != TEXT_FILTER_TARGET_FULLTEXT_MORELIKE ) { this._textFilter = '' - this.dateAddedQuery = '' - this.dateCreatedQuery = '' } this.textFilterTarget = target this.textFilterInput.nativeElement.focus()