Compare commits

...
Author SHA1 Message Date
Denys Vuika fe9300c8e1 fixes 2023-10-09 15:32:21 +01:00
rbahirsheth fc147074ce [APPS-2136] search datetime range moment changes 2023-10-09 13:54:09 +05:30
2 changed files with 38 additions and 39 deletions
@@ -19,15 +19,15 @@ import { SearchDatetimeRangeComponent } from './search-datetime-range.component'
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ContentTestingModule } from '../../../testing/content.testing.module';
import { TranslateModule } from '@ngx-translate/core';
declare let moment: any;
import { isValid } from 'date-fns';
import { DateFnsUtils } from '@alfresco/adf-core';
describe('SearchDatetimeRangeComponent', () => {
let fixture: ComponentFixture<SearchDatetimeRangeComponent>;
let component: SearchDatetimeRangeComponent;
const fromDatetime = '2016-10-16 12:30';
const toDatetime = '2017-10-16 20:00';
const maxDatetime = '10-Mar-20 20:00';
const fromDatetime = '2016-10-16 12:30 GMT';
const toDatetime = '2017-10-16 20:00 GMT';
const maxDatetime = '10-Mar-20 20:00 GMT';
const datetimeFormatFixture = 'DD-MMM-YY HH:mm';
beforeEach(() => {
@@ -59,13 +59,14 @@ describe('SearchDatetimeRangeComponent', () => {
await fixture.whenStable();
const inputString = '20-feb-18 20:00';
const momentFromInput = moment(inputString, datetimeFormatFixture);
expect(momentFromInput.isValid()).toBeTruthy();
const dateFromInput = DateFnsUtils.parseDate(inputString, datetimeFormatFixture);
expect(isValid(dateFromInput)).toBeTruthy();
component.onChangedHandler({ value: inputString }, component.from);
expect(component.from.value.toString()).toEqual(momentFromInput.toString());
expect(component.from.value.toString()).toEqual(dateFromInput.toString());
});
it('should NOT setup form control with invalid datetime on change', async () => {
@@ -75,13 +76,14 @@ describe('SearchDatetimeRangeComponent', () => {
await fixture.whenStable();
const inputString = '2017-10-16 20:f:00';
const momentFromInput = moment(inputString, datetimeFormatFixture);
expect(momentFromInput.isValid()).toBeFalsy();
const dateFromInput = DateFnsUtils.parseDate(inputString, datetimeFormatFixture);
expect(isValid(dateFromInput)).toBeFalsy();
component.onChangedHandler({ value: inputString }, component.from);
expect(component.from.value.toString()).not.toEqual(momentFromInput.toString());
expect(component.from.value.toString()).not.toEqual(dateFromInput.toString());
});
it('should reset form', async () => {
@@ -152,7 +154,7 @@ describe('SearchDatetimeRangeComponent', () => {
to: toDatetime
}, true);
const expectedQuery = `cm:created:['2016-10-16T12:30:00Z' TO '2017-10-16T20:00:59Z']`;
const expectedQuery = `cm:created:['2016-10-16T12:30:00.000Z' TO '2017-10-16T20:00:59.999Z']`;
expect(context.queryFragments[component.id]).toEqual(expectedQuery);
expect(context.update).toHaveBeenCalled();
@@ -180,7 +182,7 @@ describe('SearchDatetimeRangeComponent', () => {
to: toInGmt
}, true);
const expectedQuery = `cm:created:['2021-02-24T15:00:00Z' TO '2021-02-28T13:00:59Z']`;
const expectedQuery = `cm:created:['2021-02-24T15:00:00.000Z' TO '2021-02-28T13:00:59.999Z']`;
expect(context.queryFragments[component.id]).toEqual(expectedQuery);
expect(context.update).toHaveBeenCalled();
@@ -16,26 +16,24 @@
*/
import { Component, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core';
import { UntypedFormControl, UntypedFormGroup, Validators } from '@angular/forms';
import { UserPreferencesService, UserPreferenceValues } from '@alfresco/adf-core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { DateFnsUtils, UserPreferencesService, UserPreferenceValues } from '@alfresco/adf-core';
import { SearchWidget } from '../../models/search-widget.interface';
import { SearchWidgetSettings } from '../../models/search-widget-settings.interface';
import { SearchQueryBuilderService } from '../../services/search-query-builder.service';
import { LiveErrorStateMatcher } from '../../forms/live-error-state-matcher';
import { Moment } from 'moment';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { DatetimeAdapter, MAT_DATETIME_FORMATS } from '@mat-datetimepicker/core';
import { MAT_MOMENT_DATETIME_FORMATS } from '@mat-datetimepicker/moment';
import { startOfMinute, isBefore, isValid, endOfMinute } from 'date-fns';
export interface DatetimeRangeValue {
from: string;
to: string;
}
declare let moment: any;
const DEFAULT_DATETIME_FORMAT: string = 'DD/MM/YYYY HH:mm';
@Component({
@@ -47,10 +45,10 @@ const DEFAULT_DATETIME_FORMAT: string = 'DD/MM/YYYY HH:mm';
host: { class: 'adf-search-date-range' }
})
export class SearchDatetimeRangeComponent implements SearchWidget, OnInit, OnDestroy {
from: UntypedFormControl;
to: UntypedFormControl;
from: FormControl;
to: FormControl;
form: UntypedFormGroup;
form: FormGroup;
matcher = new LiveErrorStateMatcher();
id: string;
@@ -66,7 +64,7 @@ export class SearchDatetimeRangeComponent implements SearchWidget, OnInit, OnDes
private onDestroy$ = new Subject<boolean>();
constructor(private dateAdapter: DatetimeAdapter<Moment>, private userPreferencesService: UserPreferencesService) {}
constructor(private dateAdapter: DatetimeAdapter<string>, private userPreferencesService: UserPreferencesService) {}
getFromValidationMessage(): string {
return this.from.hasError('invalidOnChange') || this.hasParseError(this.from)
@@ -101,21 +99,21 @@ export class SearchDatetimeRangeComponent implements SearchWidget, OnInit, OnDes
const validators = Validators.compose([Validators.required]);
if (this.settings?.maxDatetime) {
this.maxDatetime = moment(this.settings.maxDatetime);
this.maxDatetime = new Date(this.settings.maxDatetime);
}
if (this.startValue) {
const splitValue = this.startValue.split('||');
const fromValue = this.dateAdapter.parse(splitValue[0], this.datetimePickerFormat);
const toValue = this.dateAdapter.parse(splitValue[1], this.datetimePickerFormat);
this.from = new UntypedFormControl(fromValue, validators);
this.to = new UntypedFormControl(toValue, validators);
this.from = new FormControl(fromValue, validators);
this.to = new FormControl(toValue, validators);
} else {
this.from = new UntypedFormControl('', validators);
this.to = new UntypedFormControl('', validators);
this.from = new FormControl('', validators);
this.to = new FormControl('', validators);
}
this.form = new UntypedFormGroup({
this.form = new FormGroup({
from: this.from,
to: this.to
});
@@ -129,12 +127,12 @@ export class SearchDatetimeRangeComponent implements SearchWidget, OnInit, OnDes
this.onDestroy$.complete();
}
apply(model: { from: string; to: string }, isValid: boolean) {
if (isValid && this.id && this.context && this.settings && this.settings.field) {
apply(model: { from: string; to: string }, isFieldValid: boolean) {
if (isFieldValid && this.id && this.context && this.settings && this.settings.field) {
this.isActive = true;
const start = moment.utc(model.from).startOf('minute').format();
const end = moment.utc(model.to).endOf('minute').format();
const start = startOfMinute(new Date(model.from)).toISOString();
const end = endOfMinute(new Date(model.to)).toISOString();
this.context.queryFragments[this.id] = `${this.settings.field}:['${start}' TO '${end}']`;
this.updateDisplayValue();
@@ -211,10 +209,10 @@ export class SearchDatetimeRangeComponent implements SearchWidget, OnInit, OnDes
}
}
onChangedHandler(event: any, formControl: UntypedFormControl) {
onChangedHandler(event: any, formControl: FormControl) {
const inputValue = event.value;
const formatDate = this.dateAdapter.parse(inputValue, this.datetimePickerFormat);
if (formatDate?.isValid()) {
if (formatDate && isValid(formatDate)) {
formControl.setValue(formatDate);
} else if (formatDate) {
formControl.setErrors({
@@ -225,12 +223,11 @@ export class SearchDatetimeRangeComponent implements SearchWidget, OnInit, OnDes
this.setFromMaxDatetime();
}
setLocale(locale) {
this.dateAdapter.setLocale(locale);
moment.locale(locale);
setLocale(locale: string) {
this.dateAdapter.setLocale(DateFnsUtils.getLocaleFromString(locale));
}
hasParseError(formControl): boolean {
hasParseError(formControl: FormControl): boolean {
return formControl.hasError('matDatepickerParse') && formControl.getError('matDatepickerParse').text;
}
@@ -240,6 +237,6 @@ export class SearchDatetimeRangeComponent implements SearchWidget, OnInit, OnDes
setFromMaxDatetime() {
this.fromMaxDatetime =
!this.to.value || (this.maxDatetime && moment(this.maxDatetime).isBefore(this.to.value)) ? this.maxDatetime : moment(this.to.value);
(!this.to.value || (this.maxDatetime && isBefore(this.maxDatetime, this.to.value))) ? this.maxDatetime : this.to.value;
}
}