From b0b0ffb9f3881af1a7f92284e19903c82b617f90 Mon Sep 17 00:00:00 2001 From: SheenaMalhotra182 Date: Wed, 4 Oct 2023 16:03:01 +0530 Subject: [PATCH] [ACS-5857] fixed code smell by creating a method --- .../date-time/date-time.widget.spec.ts | 8 ++--- .../widgets/date-time/date-time.widget.ts | 35 ++++++++----------- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/lib/core/src/lib/form/components/widgets/date-time/date-time.widget.spec.ts b/lib/core/src/lib/form/components/widgets/date-time/date-time.widget.spec.ts index db7a7059a0..c99f8af20b 100644 --- a/lib/core/src/lib/form/components/widgets/date-time/date-time.widget.spec.ts +++ b/lib/core/src/lib/form/components/widgets/date-time/date-time.widget.spec.ts @@ -62,9 +62,7 @@ describe('DateTimeWidgetComponent', () => { fixture.detectChanges(); - if (!minValue.includes('00.')) { - minValue = DateFnsUtils.addSeconds(minValue); - } + widget.parseDateAndSetMinMaxValue(minValue, 'minDate'); const [year, month, day, hours, minutes, seconds] = minValue.split(/[-T:.Z]/).map(Number); const expected = new Date(Date.UTC(year, month - 1, day, hours, minutes, seconds)).toISOString(); @@ -90,9 +88,7 @@ describe('DateTimeWidgetComponent', () => { }); fixture.detectChanges(); - if (!maxValue.includes('00.')) { - maxValue = DateFnsUtils.addSeconds(maxValue); - } + widget.parseDateAndSetMinMaxValue(maxValue, 'minDate'); const [year, month, day, hours, minutes, seconds] = maxValue.split(/[-T:.Z]/).map(Number); const expected = new Date(Date.UTC(year, month - 1, day, hours, minutes, seconds)).toISOString(); diff --git a/lib/core/src/lib/form/components/widgets/date-time/date-time.widget.ts b/lib/core/src/lib/form/components/widgets/date-time/date-time.widget.ts index 249abe28f0..dfbed80ec2 100644 --- a/lib/core/src/lib/form/components/widgets/date-time/date-time.widget.ts +++ b/lib/core/src/lib/form/components/widgets/date-time/date-time.widget.ts @@ -63,33 +63,28 @@ export class DateTimeWidgetComponent extends WidgetComponent implements OnInit, if (this.field) { if (this.field.minValue) { - if (!this.field.minValue.includes('00.')) { - this.field.minValue = DateFnsUtils.addSeconds(this.field.minValue); - } - - const [year, month, day, hours, minutes, seconds] = this.field.minValue.split(/[-T:.Z]/).map(Number); - const minDate = new Date(Date.UTC(year, month - 1, day, hours, minutes, seconds)); - - if (isValid(minDate)) { - this.minDate = minDate.toISOString(); - } + this.parseDateAndSetMinMaxValue(this.field.minValue, 'minDate'); } if (this.field.maxValue) { - if (!this.field.maxValue.includes('00.')) { - this.field.maxValue = DateFnsUtils.addSeconds(this.field.maxValue); - } - - const [year, month, day, hours, minutes, seconds] = this.field.maxValue.split(/[-T:.Z]/).map(Number); - const maxDate = new Date(Date.UTC(year, month - 1, day, hours, minutes, seconds)); - - if (isValid(maxDate)) { - this.maxDate = maxDate.toISOString(); - } + this.parseDateAndSetMinMaxValue(this.field.maxValue, 'maxDate'); } } } + parseDateAndSetMinMaxValue = (dateString, targetProperty) => { + if (dateString && !dateString.includes('00.')) { + this.field[targetProperty] = DateFnsUtils.addSeconds(dateString); + } + + const [year, month, day, hours, minutes, seconds] = dateString.split(/[-T:.Z]/).map(Number); + const parsedDate = new Date(Date.UTC(year, month - 1, day, hours, minutes, seconds)); + + if (isValid(parsedDate)) { + this[targetProperty] = parsedDate.toISOString(); + } + } + ngOnDestroy() { this.onDestroy$.next(true); this.onDestroy$.complete();