Skip to content

Commit

Permalink
[HXCS-2988] fix dates in CardViweDateItemComponent (#9276)
Browse files Browse the repository at this point in the history
* [HXCS-2988] fix dates in CardViweDateItemComponent

* [HXCS-2988] remove utc conversion + refactoring

* [HXCS-2988] force property.value to be Date object(s)

* [HXCS-2988] format dates

* [HXCS-2988] revert last commit

* [HXCS-2988] update date-only tests

---------

Co-authored-by: Adriano Costa <Adriano.Costa@hyland.comgit>
  • Loading branch information
wideLandscape and Adriano Costa committed Jan 27, 2024
1 parent 9473f76 commit 2e4afa0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { CoreTestingModule } from '../../../testing/core.testing.module';
import { ClipboardService } from '../../../clipboard/clipboard.service';
import { CardViewDatetimeItemModel } from '../../models/card-view-datetimeitem.model';
import { TranslateModule } from '@ngx-translate/core';
import { AppConfigService } from '@alfresco/adf-core';
import { AppConfigService } from '../../../app-config/app-config.service';
import { MatDatetimepickerInputEvent } from '@mat-datetimepicker/core';
import { HarnessLoader } from '@angular/cdk/testing';
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { ClipboardService } from '../../../clipboard/clipboard.service';
import { TranslationService } from '../../../translation/translation.service';
import { ADF_DATE_FORMATS, AdfDateFnsAdapter } from '../../../common/utils/date-fns-adapter';
import { ADF_DATETIME_FORMATS, AdfDateTimeFnsAdapter } from '../../../common/utils/datetime-fns-adapter';
import { DateFnsUtils } from '../../../common';
import { isValid } from 'date-fns';

@Component({
Expand Down Expand Up @@ -74,13 +73,9 @@ export class CardViewDateItemComponent extends BaseCardView<CardViewDateItemMode
(this.dateAdapter as AdfDateFnsAdapter).displayFormat = 'MMM DD';

if (this.property.multivalued) {
if (!this.property.value) {
this.property.value = [];
}
this.initMultivaluedProperty();
} else {
if (this.property.value && !Array.isArray(this.property.value)) {
this.valueDate = DateFnsUtils.localToUtc(new Date(this.property.value));
}
this.initSingleValueProperty();
}
}

Expand All @@ -103,8 +98,11 @@ export class CardViewDateItemComponent extends BaseCardView<CardViewDateItemMode
onDateChanged(event: MatDatetimepickerInputEvent<Date>) {
if (event.value) {
if (isValid(event.value)) {
this.property.value = new Date(event.value);
if (this.property.type === 'date') {
this.property.value.setHours(0, 0, 0, 0);
}
this.valueDate = event.value;
this.property.value = DateFnsUtils.utcToLocal(event.value);
this.update();
}
}
Expand All @@ -127,7 +125,11 @@ export class CardViewDateItemComponent extends BaseCardView<CardViewDateItemMode
addDateToList(event: MatDatetimepickerInputEvent<Date>) {
if (event.value) {
if (isValid(event.value) && this.property.multivalued && Array.isArray(this.property.value)) {
this.property.value.push(DateFnsUtils.utcToLocal(event.value));
const localDate = event.value;
if (this.property.type === 'date') {
localDate.setHours(0, 0, 0, 0);
}
this.property.value.push(localDate);
this.update();
}
}
Expand All @@ -143,4 +145,30 @@ export class CardViewDateItemComponent extends BaseCardView<CardViewDateItemMode
update() {
this.cardViewUpdateService.update({ ...this.property } as CardViewDateItemModel, this.property.value);
}

private initSingleValueProperty() {
if (this.property.value && !Array.isArray(this.property.value)) {
this.property.value = new Date(this.property.value);
if (this.property.type === 'date') {
this.property.value.setHours(0, 0, 0, 0);
}
this.valueDate = this.property.value;
}
}

private initMultivaluedProperty() {
if (!this.property.value) {
this.property.value = [];
}
if (Array.isArray(this.property.value) && this.property.value.length > 0) {
this.property.value = this.property.value.map((date: Date) => {
const localDate = new Date(date);
if (this.property.type === 'date') {
localDate.setHours(0, 0, 0, 0);
}
return localDate;
});
this.valueDate = this.property.value[0];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ describe('ProcessHeaderCloudComponent', () => {
const lastModifiedElement = fixture.debugElement.query(By.css('[data-automation-id="header-lastModified"] .adf-property-value'));

expect(component.dateFormat).toEqual('full');
expect(startedDateElement.nativeElement.innerText.trim()).toBe('Saturday, March 9, 2019 at 5:23:07 PM GMT+00:00');
expect(lastModifiedElement.nativeElement.innerText.trim()).toBe('Saturday, March 9, 2019 at 5:23:07 PM GMT+00:00');
expect(startedDateElement.nativeElement.innerText.trim()).toBe('Saturday, March 9, 2019 at 12:00:00 AM GMT+00:00');
expect(lastModifiedElement.nativeElement.innerText.trim()).toBe('Saturday, March 9, 2019 at 12:00:00 AM GMT+00:00');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ describe('TaskHeaderCloudComponent', () => {
const createdDateElement = fixture.debugElement.query(By.css('[data-automation-id="header-created"] .adf-property-value'));

expect(component.dateFormat).toEqual('full');
expect(createdDateElement.nativeElement.innerText.trim()).toBe('Monday, December 17, 2018 at 12:00:55 PM GMT+00:00');
expect(createdDateElement.nativeElement.innerText.trim()).toBe('Monday, December 17, 2018 at 12:00:00 AM GMT+00:00');
});
});

Expand Down

0 comments on commit 2e4afa0

Please sign in to comment.