diff --git a/lib/core/src/lib/card-view/components/card-view-dateitem/card-view-dateitem.component.spec.ts b/lib/core/src/lib/card-view/components/card-view-dateitem/card-view-dateitem.component.spec.ts index 4a22d7c1849..d87b410bc00 100644 --- a/lib/core/src/lib/card-view/components/card-view-dateitem/card-view-dateitem.component.spec.ts +++ b/lib/core/src/lib/card-view/components/card-view-dateitem/card-view-dateitem.component.spec.ts @@ -17,7 +17,6 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; import { By } from '@angular/platform-browser'; -import moment from 'moment'; import { CardViewDateItemModel } from '../../models/card-view-dateitem.model'; import { CardViewUpdateService } from '../../services/card-view-update.service'; import { CardViewDateItemComponent } from './card-view-dateitem.component'; @@ -25,6 +24,8 @@ import { CoreTestingModule } from '../../../testing/core.testing.module'; import { ClipboardService } from '../../../clipboard/clipboard.service'; import { TranslateModule } from '@ngx-translate/core'; import { AppConfigService } from '@alfresco/adf-core'; +import { endOfDay, startOfDay } from 'date-fns'; +import { CardViewDatetimeItemModel } from '../../models/card-view-datetimeitem.model'; describe('CardViewDateItemComponent', () => { @@ -61,7 +62,7 @@ describe('CardViewDateItemComponent', () => { afterEach(() => fixture.destroy()); it('should pick date format from appConfigService', () => { - expect(component.dateFormat).toEqual('shortDate'); + expect(component.dateFormat).toEqual('MMM d, y'); }); it('should render the label and value', () => { @@ -191,8 +192,8 @@ describe('CardViewDateItemComponent', () => { const itemUpdatedSpy = spyOn(cardViewUpdateService.itemUpdated$, 'next'); component.editable = true; component.property.editable = true; - component.property.value = moment('Jul 10 2017', 'MMM DD YYYY').endOf('day').toDate(); - const expectedDate = moment('Jul 10 2017', 'MMM DD YYYY'); + component.property.value = endOfDay(new Date('Jul 10 2017')); + const expectedDate = new Date('Jul 10 2017'); fixture.detectChanges(); const property = { ...component.property }; @@ -200,7 +201,7 @@ describe('CardViewDateItemComponent', () => { expect(itemUpdatedSpy).toHaveBeenCalledWith({ target: property, changed: { - dateKey: expectedDate.endOf('day').toDate() + dateKey: endOfDay(expectedDate) } }); }); @@ -209,13 +210,13 @@ describe('CardViewDateItemComponent', () => { component.editable = true; component.property.editable = true; component.property.value = null; - const expectedDate = moment('Jul 10 2017', 'MMM DD YY'); + const expectedDate = new Date('Jul 10 2017'); fixture.detectChanges(); component.onDateChanged({ value: expectedDate }); await fixture.whenStable(); - expect(component.property.value).toEqual(expectedDate.endOf('day').toDate()); + expect(component.property.value).toEqual(endOfDay(expectedDate)); }); it('should copy value to clipboard on double click', () => { @@ -314,14 +315,14 @@ describe('CardViewDateItemComponent', () => { }); }); - it('should be possible update a date-time', async () => { + it('should be possible update a date-time using end of day', async () => { component.editable = true; component.property.editable = true; component.property.default = 'Jul 10 2017 00:01:00'; component.property.key = 'fake-key'; component.dateFormat = 'M/d/yy, h:mm a'; component.property.value = 'Jul 10 2017 00:01:00'; - const expectedDate = moment('Jul 10 2018', 'MMM DD YY h:m:s'); + const expectedDate = new Date('Jul 10 2018'); fixture.detectChanges(); await fixture.whenStable(); @@ -332,7 +333,7 @@ describe('CardViewDateItemComponent', () => { component.onDateChanged({ value: expectedDate }); fixture.detectChanges(); - expect(component.property.value).toEqual(expectedDate.endOf('day').toDate()); + expect(component.property.value).toEqual(endOfDay(expectedDate)); }); it('should render chips for multivalue dates when chips are enabled', async () => { @@ -353,4 +354,44 @@ describe('CardViewDateItemComponent', () => { expect(valueChips[1].nativeElement.innerText.trim()).toBe('Jul 11, 2017'); expect(valueChips[2].nativeElement.innerText.trim()).toBe('Jul 12, 2017'); }); + + it('should render chips for multivalue datetimes when chips are enabled', async () => { + component.property = new CardViewDatetimeItemModel({ + label: 'Text label', + value: ['Jul 10 2017 00:01:00', 'Jul 11 2017 00:01:00', 'Jul 12 2017 00:01:00'], + key: 'textkey', + editable: true, + multivalued: true + }); + + fixture.detectChanges(); + await fixture.whenStable(); + const valueChips = fixture.debugElement.queryAll(By.css(`mat-chip`)); + expect(valueChips).not.toBeNull(); + expect(valueChips.length).toBe(3); + expect(valueChips[0].nativeElement.innerText.trim()).toBe('Jul 10, 2017, 0:01'); + expect(valueChips[1].nativeElement.innerText.trim()).toBe('Jul 11, 2017, 0:01'); + expect(valueChips[2].nativeElement.innerText.trim()).toBe('Jul 12, 2017, 0:01'); + }); + + it('should be possible update a date-time using start of day', async () => { + component.editable = true; + component.property.editable = true; + component.property.default = 'Jul 10 2017 00:01:00'; + component.property.key = 'properties.cm:from'; + component.dateFormat = 'M/d/yy, h:mm a'; + component.property.value = 'Jul 10 2017 00:01:00'; + const expectedDate = new Date('Jul 10 2018'); + fixture.detectChanges(); + + await fixture.whenStable(); + fixture.detectChanges(); + const element = fixture.debugElement.nativeElement.querySelector('span[data-automation-id="card-date-value-properties.cm:from"]'); + expect(element).toBeDefined(); + expect(element.innerText).toEqual('Jul 10, 2017'); + component.onDateChanged({ value: expectedDate }); + + fixture.detectChanges(); + expect(component.property.value).toEqual(startOfDay(expectedDate)); + }); }); diff --git a/lib/core/src/lib/card-view/components/card-view-dateitem/card-view-dateitem.component.ts b/lib/core/src/lib/card-view/components/card-view-dateitem/card-view-dateitem.component.ts index fe2deb3ddca..59c22541a41 100644 --- a/lib/core/src/lib/card-view/components/card-view-dateitem/card-view-dateitem.component.ts +++ b/lib/core/src/lib/card-view/components/card-view-dateitem/card-view-dateitem.component.ts @@ -15,28 +15,24 @@ * limitations under the License. */ -import { Component, Input, OnDestroy, OnInit, ViewChild, ViewEncapsulation } from '@angular/core'; -import { DateAdapter, MAT_DATE_FORMATS } from '@angular/material/core'; -import { DatetimeAdapter, MAT_DATETIME_FORMATS, MatDatetimepickerComponent } from '@mat-datetimepicker/core'; -import { MAT_MOMENT_DATETIME_FORMATS, MomentDatetimeAdapter } from '@mat-datetimepicker/moment'; -import moment, { Moment } from 'moment'; +import { Component, Inject, Input, OnDestroy, OnInit, ViewChild, ViewEncapsulation } from '@angular/core'; +import { DateAdapter, MAT_DATE_FORMATS, MatDateFormats } from '@angular/material/core'; +import { DateFnsAdapter, MAT_DATE_FNS_FORMATS } from '@angular/material-date-fns-adapter'; import { CardViewDateItemModel } from '../../models/card-view-dateitem.model'; import { UserPreferencesService, UserPreferenceValues } from '../../../common/services/user-preferences.service'; -import { MomentDateAdapter } from '../../../common/utils/moment-date-adapter'; -import { MOMENT_DATE_FORMATS } from '../../../common/utils/moment-date-formats.model'; -import { AppConfigService } from '../../../app-config/app-config.service'; import { Subject } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; import { BaseCardView } from '../base-card-view'; import { ClipboardService } from '../../../clipboard/clipboard.service'; import { TranslationService } from '../../../translation/translation.service'; +import { endOfDay, parse, startOfDay } from 'date-fns'; +import { MatDatepicker } from '@angular/material/datepicker'; +import { DateFnsUtils } from '../../../common/utils/date-fns-utils'; @Component({ providers: [ - { provide: DateAdapter, useClass: MomentDateAdapter }, - { provide: MAT_DATE_FORMATS, useValue: MOMENT_DATE_FORMATS }, - { provide: DatetimeAdapter, useClass: MomentDatetimeAdapter }, - { provide: MAT_DATETIME_FORMATS, useValue: MAT_MOMENT_DATETIME_FORMATS } + { provide: DateAdapter, useClass: DateFnsAdapter }, + { provide: MAT_DATE_FORMATS, useValue: MAT_DATE_FNS_FORMATS } ], selector: 'adf-card-view-dateitem', templateUrl: './card-view-dateitem.component.html', @@ -58,20 +54,22 @@ export class CardViewDateItemComponent extends BaseCardView; + public datepicker: MatDatepicker; - valueDate: Moment; + valueDate: Date; dateFormat: string; private onDestroy$ = new Subject(); - constructor(private dateAdapter: DateAdapter, + constructor(private dateAdapter: DateAdapter, private userPreferencesService: UserPreferencesService, - private appConfig: AppConfigService, private clipboardService: ClipboardService, - private translateService: TranslationService) { + private translateService: TranslationService, + @Inject(MAT_DATE_FORMATS) private dateFormatConfig: MatDateFormats) { super(); - this.dateFormat = this.appConfig.get('dateValues.defaultDateFormat'); + // need to change this to app.config when will change from moment to date-fns throughout the application + // this.dateFormat = this.appConfig.get('dateValues.defaultDateFormat'); + this.dateFormat = 'MMM d, y'; } ngOnInit() { @@ -79,14 +77,14 @@ export class CardViewDateItemComponent extends BaseCardView { - this.dateAdapter.setLocale(locale); + this.dateAdapter.setLocale(DateFnsUtils.getLocaleFromString(locale)); this.property.locale = locale; }); - (this.dateAdapter as MomentDateAdapter).overrideDisplayFormat = 'MMM DD'; + this.dateFormatConfig.display.dateInput = this.dateFormat; if (this.property.value) { - this.valueDate = moment(this.property.value, this.dateFormat); + this.valueDate = parse(this.property.value, this.dateFormat, new Date()); } else if (this.property.multivalued && !this.property.value) { this.property.value = []; } @@ -115,10 +113,10 @@ export class CardViewDateItemComponent extends BaseCardView