Skip to content

Commit

Permalink
[APPS-2133] migration of dependency from moment to date-fns in start-…
Browse files Browse the repository at this point in the history
…task.component (#8844)

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

* used parse() instead of format()

* [APPS-2133] Migration from moment to date-fns with converter approach

* [APPS-2133] Changed varible name to avoid lint issue

---------

Co-authored-by: kritagya09 <[email protected]>
  • Loading branch information
jatin2008 and kritagya09 authored Oct 5, 2023
1 parent 93fe295 commit ecbee58
Showing 1 changed file with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
*/

import {
LogService, UserPreferencesService, UserPreferenceValues, FormFieldModel, FormModel,
MOMENT_DATE_FORMATS, MomentDateAdapter
LogService, UserPreferencesService, UserPreferenceValues, FormFieldModel, FormModel, DateFnsUtils
} from '@alfresco/adf-core';
import { Component, EventEmitter, Input, OnInit, Output, ViewEncapsulation, OnDestroy } from '@angular/core';
import { DateAdapter, MAT_DATE_FORMATS } from '@angular/material/core';
import moment, { Moment } from 'moment';
import { EMPTY, Observable, Subject } from 'rxjs';
import { Form } from '../models/form.model';
import { TaskDetailsModel } from '../models/task-details.model';
import { TaskListService } from './../services/tasklist.service';
import { switchMap, defaultIfEmpty, takeUntil } from 'rxjs/operators';
import { UntypedFormBuilder, AbstractControl, Validators, UntypedFormGroup, UntypedFormControl } from '@angular/forms';
import { UserProcessModel } from '../../common/models/user-process.model';
import { isValid } from 'date-fns';
import { DateFnsAdapter, MAT_DATE_FNS_FORMATS } from '@angular/material-date-fns-adapter';

const FORMAT_DATE = 'DD/MM/YYYY';
const MAX_LENGTH = 255;
Expand All @@ -38,8 +38,8 @@ const MAX_LENGTH = 255;
templateUrl: './start-task.component.html',
styleUrls: ['./start-task.component.scss'],
providers: [
{ provide: DateAdapter, useClass: MomentDateAdapter },
{ provide: MAT_DATE_FORMATS, useValue: MOMENT_DATE_FORMATS }],
{ provide: DateAdapter, useClass: DateFnsAdapter },
{ provide: MAT_DATE_FORMATS, useValue: MAT_DATE_FNS_FORMATS }],
encapsulation: ViewEncapsulation.None
})
export class StartTaskComponent implements OnInit, OnDestroy {
Expand Down Expand Up @@ -75,7 +75,7 @@ export class StartTaskComponent implements OnInit, OnDestroy {
private onDestroy$ = new Subject<boolean>();

constructor(private taskService: TaskListService,
private dateAdapter: DateAdapter<Moment>,
private dateAdapter: DateAdapter<DateFnsAdapter>,
private userPreferencesService: UserPreferencesService,
private formBuilder: UntypedFormBuilder,
private logService: LogService) {
Expand All @@ -93,7 +93,7 @@ export class StartTaskComponent implements OnInit, OnDestroy {
this.userPreferencesService
.select(UserPreferenceValues.Locale)
.pipe(takeUntil(this.onDestroy$))
.subscribe(locale => this.dateAdapter.setLocale(locale));
.subscribe(locale => this.dateAdapter.setLocale(DateFnsUtils.getLocaleFromString(locale)));

this.loadFormsTask();
this.buildForm();
Expand All @@ -119,8 +119,8 @@ export class StartTaskComponent implements OnInit, OnDestroy {
whitespaceValidator(control: UntypedFormControl): any {
if (control.value) {
const isWhitespace = (control.value || '').trim().length === 0;
const isValid = control.value.length === 0 || !isWhitespace;
return isValid ? null : { whitespace: true };
const isControlValid = control.value.length === 0 || !isWhitespace;
return isControlValid ? null : { whitespace: true };
}
return null;
}
Expand Down Expand Up @@ -187,16 +187,16 @@ export class StartTaskComponent implements OnInit, OnDestroy {
this.dateError = false;

if (newDateValue) {
let momentDate: moment.Moment;
let date: Date;

if (typeof newDateValue === 'string') {
momentDate = moment(newDateValue, FORMAT_DATE, true);
date = DateFnsUtils.parseDate(newDateValue, FORMAT_DATE);
} else {
momentDate = newDateValue;
date = newDateValue;
}

if (momentDate.isValid()) {
this.taskDetailsModel.dueDate = momentDate.toDate();
if (isValid(date)) {
this.taskDetailsModel.dueDate = date;
} else {
this.dateError = true;
this.taskDetailsModel.dueDate = null;
Expand Down

0 comments on commit ecbee58

Please sign in to comment.