migration of dependency from moment to date-fns in task-list.component

This commit is contained in:
Jatin_Chugh
2023-08-23 17:07:06 +05:30
parent de0b44d568
commit 5a31f240f1

View File

@@ -16,12 +16,10 @@
*/ */
import { import {
LogService, UserPreferencesService, UserPreferenceValues, FormFieldModel, FormModel, LogService, UserPreferencesService, UserPreferenceValues, FormFieldModel, FormModel, DateFnsUtils
MOMENT_DATE_FORMATS, MomentDateAdapter
} from '@alfresco/adf-core'; } from '@alfresco/adf-core';
import { Component, EventEmitter, Input, OnInit, Output, ViewEncapsulation, OnDestroy } from '@angular/core'; import { Component, EventEmitter, Input, OnInit, Output, ViewEncapsulation, OnDestroy } from '@angular/core';
import { DateAdapter, MAT_DATE_FORMATS } from '@angular/material/core'; import { DateAdapter, MAT_DATE_FORMATS } from '@angular/material/core';
import moment, { Moment } from 'moment';
import { Observable, of, Subject } from 'rxjs'; import { Observable, of, Subject } from 'rxjs';
import { Form } from '../models/form.model'; import { Form } from '../models/form.model';
import { TaskDetailsModel } from '../models/task-details.model'; import { TaskDetailsModel } from '../models/task-details.model';
@@ -29,8 +27,10 @@ import { TaskListService } from './../services/tasklist.service';
import { switchMap, defaultIfEmpty, takeUntil } from 'rxjs/operators'; import { switchMap, defaultIfEmpty, takeUntil } from 'rxjs/operators';
import { UntypedFormBuilder, AbstractControl, Validators, UntypedFormGroup, UntypedFormControl } from '@angular/forms'; import { UntypedFormBuilder, AbstractControl, Validators, UntypedFormGroup, UntypedFormControl } from '@angular/forms';
import { UserProcessModel } from '../../common/models/user-process.model'; import { UserProcessModel } from '../../common/models/user-process.model';
import { format } from 'date-fns';
import { DateFnsAdapter, MAT_DATE_FNS_FORMATS } from '@angular/material-date-fns-adapter';
const FORMAT_DATE = 'DD/MM/YYYY'; const FORMAT_DATE = 'dd/MM/yyyy';
const MAX_LENGTH = 255; const MAX_LENGTH = 255;
@Component({ @Component({
@@ -38,8 +38,8 @@ const MAX_LENGTH = 255;
templateUrl: './start-task.component.html', templateUrl: './start-task.component.html',
styleUrls: ['./start-task.component.scss'], styleUrls: ['./start-task.component.scss'],
providers: [ providers: [
{ provide: DateAdapter, useClass: MomentDateAdapter }, { provide: DateAdapter, useClass: DateFnsAdapter },
{ provide: MAT_DATE_FORMATS, useValue: MOMENT_DATE_FORMATS }], { provide: MAT_DATE_FORMATS, useValue: MAT_DATE_FNS_FORMATS }],
encapsulation: ViewEncapsulation.None encapsulation: ViewEncapsulation.None
}) })
export class StartTaskComponent implements OnInit, OnDestroy { export class StartTaskComponent implements OnInit, OnDestroy {
@@ -75,7 +75,7 @@ export class StartTaskComponent implements OnInit, OnDestroy {
private onDestroy$ = new Subject<boolean>(); private onDestroy$ = new Subject<boolean>();
constructor(private taskService: TaskListService, constructor(private taskService: TaskListService,
private dateAdapter: DateAdapter<Moment>, private dateAdapter: DateAdapter<DateFnsAdapter>,
private userPreferencesService: UserPreferencesService, private userPreferencesService: UserPreferencesService,
private formBuilder: UntypedFormBuilder, private formBuilder: UntypedFormBuilder,
private logService: LogService) { private logService: LogService) {
@@ -93,7 +93,7 @@ export class StartTaskComponent implements OnInit, OnDestroy {
this.userPreferencesService this.userPreferencesService
.select(UserPreferenceValues.Locale) .select(UserPreferenceValues.Locale)
.pipe(takeUntil(this.onDestroy$)) .pipe(takeUntil(this.onDestroy$))
.subscribe(locale => this.dateAdapter.setLocale(locale)); .subscribe(locale => this.dateAdapter.setLocale(DateFnsUtils.getLocaleFromString(locale)));
this.loadFormsTask(); this.loadFormsTask();
this.buildForm(); this.buildForm();
@@ -187,16 +187,16 @@ export class StartTaskComponent implements OnInit, OnDestroy {
this.dateError = false; this.dateError = false;
if (newDateValue) { if (newDateValue) {
let momentDate: moment.Moment; let date: string | Date;
if (typeof newDateValue === 'string') { if (typeof newDateValue === 'string') {
momentDate = moment(newDateValue, FORMAT_DATE, true); date = format(new Date(newDateValue), FORMAT_DATE);
} else { } else {
momentDate = newDateValue; date = newDateValue;
} }
if (momentDate.isValid()) { if (date) {
this.taskDetailsModel.dueDate = momentDate.toDate(); this.taskDetailsModel.dueDate = new Date(date);
} else { } else {
this.dateError = true; this.dateError = true;
this.taskDetailsModel.dueDate = null; this.taskDetailsModel.dueDate = null;