[APPS-2134] Remove moment.js dependency from start-task-cloud.compone… (#8845)

* [APPS-2134] Remove moment.js dependency from start-task-cloud.component.ts

* Removed the console log

* Update variable name
This commit is contained in:
Raviraj Bahirsheth 2023-08-25 18:43:14 +05:30 committed by GitHub
parent 377dd5d3a2
commit d839531d08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,11 +17,10 @@
import { Component, EventEmitter, Input, OnInit, Output, ViewEncapsulation, OnDestroy, ViewChild } from '@angular/core'; import { Component, EventEmitter, Input, OnInit, Output, ViewEncapsulation, OnDestroy, ViewChild } 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, Subject } from 'rxjs'; import { Observable, Subject } from 'rxjs';
import { UntypedFormBuilder, Validators, UntypedFormGroup, UntypedFormControl } from '@angular/forms'; import { UntypedFormBuilder, Validators, UntypedFormGroup, UntypedFormControl } from '@angular/forms';
import { import {
MOMENT_DATE_FORMATS, MomentDateAdapter, DateFnsUtils,
LogService, LogService,
UserPreferencesService, UserPreferencesService,
UserPreferenceValues UserPreferenceValues
@ -34,17 +33,19 @@ import { takeUntil } from 'rxjs/operators';
import { TaskPriorityOption } from '../../models/task.model'; import { TaskPriorityOption } from '../../models/task.model';
import { IdentityUserService } from '../../../people/services/identity-user.service'; import { IdentityUserService } from '../../../people/services/identity-user.service';
import { IdentityUserModel } from '../../../people/models/identity-user.model'; import { IdentityUserModel } from '../../../people/models/identity-user.model';
import { DateFnsAdapter, MAT_DATE_FNS_FORMATS } from '@angular/material-date-fns-adapter';
import { isValid, parse } from 'date-fns';
const MAX_NAME_LENGTH = 255; const MAX_NAME_LENGTH = 255;
const DATE_FORMAT: string = 'DD/MM/YYYY'; const DATE_FORMAT: string = 'dd/MM/yyyy';
@Component({ @Component({
selector: 'adf-cloud-start-task', selector: 'adf-cloud-start-task',
templateUrl: './start-task-cloud.component.html', templateUrl: './start-task-cloud.component.html',
styleUrls: ['./start-task-cloud.component.scss'], styleUrls: ['./start-task-cloud.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 StartTaskCloudComponent implements OnInit, OnDestroy { export class StartTaskCloudComponent implements OnInit, OnDestroy {
@ -105,7 +106,7 @@ export class StartTaskCloudComponent implements OnInit, OnDestroy {
private onDestroy$ = new Subject<boolean>(); private onDestroy$ = new Subject<boolean>();
constructor(private taskService: TaskCloudService, constructor(private taskService: TaskCloudService,
private dateAdapter: DateAdapter<Moment>, private dateAdapter: DateAdapter<DateFnsAdapter>,
private userPreferencesService: UserPreferencesService, private userPreferencesService: UserPreferencesService,
private formBuilder: UntypedFormBuilder, private formBuilder: UntypedFormBuilder,
private identityUserService: IdentityUserService, private identityUserService: IdentityUserService,
@ -116,7 +117,7 @@ export class StartTaskCloudComponent 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.loadCurrentUser(); this.loadCurrentUser();
this.buildForm(); this.buildForm();
this.loadDefaultPriorities(); this.loadDefaultPriorities();
@ -182,8 +183,9 @@ export class StartTaskCloudComponent implements OnInit, OnDestroy {
this.dateError = false; this.dateError = false;
if (newDateValue) { if (newDateValue) {
const momentDate = moment(newDateValue, DATE_FORMAT, true); const date = parse(newDateValue, DATE_FORMAT, new Date());
if (!momentDate.isValid()) {
if (!isValid(date)) {
this.dateError = true; this.dateError = true;
} }
} }
@ -219,8 +221,8 @@ export class StartTaskCloudComponent implements OnInit, OnDestroy {
public whitespaceValidator(control: UntypedFormControl) { public whitespaceValidator(control: UntypedFormControl) {
const isWhitespace = (control.value || '').trim().length === 0; const isWhitespace = (control.value || '').trim().length === 0;
const isValid = control.value.length === 0 || !isWhitespace; const isControlValid = control.value.length === 0 || !isWhitespace;
return isValid ? null : { whitespace: true }; return isControlValid ? null : { whitespace: true };
} }
get nameController(): UntypedFormControl { get nameController(): UntypedFormControl {