From 7e2c693c8afe9e133b7be28d1b500e1ad8ce20f6 Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Sun, 7 Aug 2022 08:37:18 -0700 Subject: [PATCH] Make date suggestions timezone-unaware --- .../common/input/date/date.component.ts | 35 ++++++++----------- .../data/paperless-document-suggestions.ts | 2 +- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src-ui/src/app/components/common/input/date/date.component.ts b/src-ui/src/app/components/common/input/date/date.component.ts index 63a15709b..76bfd08d0 100644 --- a/src-ui/src/app/components/common/input/date/date.component.ts +++ b/src-ui/src/app/components/common/input/date/date.component.ts @@ -1,8 +1,8 @@ import { Component, forwardRef, Input, OnInit } from '@angular/core' import { NG_VALUE_ACCESSOR } from '@angular/forms' import { + NgbDateAdapter, NgbDateParserFormatter, - NgbDateStruct, } from '@ng-bootstrap/ng-bootstrap' import { SettingsService } from 'src/app/services/settings.service' import { AbstractInputComponent } from '../abstract-input' @@ -25,36 +25,31 @@ export class DateComponent { constructor( private settings: SettingsService, - private ngbDateParserFormatter: NgbDateParserFormatter + private ngbDateParserFormatter: NgbDateParserFormatter, + private isoDateAdapter: NgbDateAdapter<string> ) { super() } @Input() - suggestions: Date[] + suggestions: string[] getSuggestions() { - if (this.suggestions == null) return [] - - return this.suggestions - .map((s) => new Date(s)) // required to call the date functions below - .filter( - (d) => - this.value === null || // if value is not set, take all suggestions - d.toISOString().slice(0, 10) != this.value // otherwise filter out the current value - ) - .map((s) => - this.ngbDateParserFormatter.format({ - year: s.getFullYear(), - month: s.getMonth() + 1, // month of Date is zero based - day: s.getDate(), - }) - ) + return this.suggestions == null + ? [] + : this.suggestions + .map((s) => this.ngbDateParserFormatter.parse(s)) + .filter( + (d) => + this.value === null || // if value is not set, take all suggestions + this.value != this.isoDateAdapter.toModel(d) // otherwise filter out current date + ) + .map((s) => this.ngbDateParserFormatter.format(s)) } onSuggestionClick(dateString: string) { const parsedDate = this.ngbDateParserFormatter.parse(dateString) - this.writeValue(`${parsedDate.year}-${parsedDate.month}-${parsedDate.day}`) + this.writeValue(this.isoDateAdapter.toModel(parsedDate)) this.onChange(this.value) } diff --git a/src-ui/src/app/data/paperless-document-suggestions.ts b/src-ui/src/app/data/paperless-document-suggestions.ts index 9f976d71b..295a1ab0e 100644 --- a/src-ui/src/app/data/paperless-document-suggestions.ts +++ b/src-ui/src/app/data/paperless-document-suggestions.ts @@ -7,5 +7,5 @@ export interface PaperlessDocumentSuggestions { storage_paths?: number[] - dates?: Date[] + dates?: string[] // ISO-formatted date string e.g. 2022-11-03 }