Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cb2-10319): Refactor Dimensions Component #1342

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div *ngIf="hint" id="{{ name }}-hint" class="govuk-hint">{{ hint }}</div>

<app-field-error-message [error]="error" [name]="name"></app-field-error-message>
<app-field-warning-message [warningMessage]="getWarningMessage"></app-field-warning-message>
<app-field-warning-message [warningMessage]="control?.meta?.warning"></app-field-warning-message>
<div class="govuk-input__wrapper">
<div *ngIf="prefix" class="govuk-input__suffix prefix" aria-hidden="true">
<ng-container [ngTemplateOutlet]="prefix.templateRef"></ng-container>
Expand Down
20 changes: 0 additions & 20 deletions src/app/forms/components/number-input/number-input.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,6 @@ export class NumberInputComponent extends BaseControlComponent implements AfterC
return `govuk-input ${this.width ? `govuk-input--width-${this.width}` : ''}`;
}

get getWarningMessage(): string {

if (this.isCorrectVehicleType()) {
if (this.shouldDisplayLengthWarning()) return 'This length dimension field value is greater than 12,000mm. Check your input before proceeding';
if (this.shouldDisplayWidthWarning()) return 'This width dimension field value is greater than 2,600mm. Check your input before proceeding';
}
return '';
}

shouldDisplayLengthWarning(): boolean {
return this.label === 'Length' && parseInt(this.value, 10) > 12000;
}
shouldDisplayWidthWarning(): boolean {
return this.label === 'Width' && parseInt(this.value, 10) > 2600;
}

isCorrectVehicleType(): boolean {
return this.vehicleType === 'hgv' || this.vehicleType === 'trl';
}

override ngAfterContentInit(): void {
super.ngAfterContentInit();
if (this.control) {
Expand Down
51 changes: 48 additions & 3 deletions src/app/forms/custom-sections/dimensions/dimensions.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
import { TechRecordType } from '@dvsa/cvs-type-definitions/types/v3/tech-record/tech-record-vehicle-type';
import { DynamicFormService } from '@forms/services/dynamic-form.service';
import {
CustomFormArray, CustomFormGroup, FormNode, FormNodeEditTypes, FormNodeWidth,
CustomFormArray,
CustomFormControl,
CustomFormGroup,
FormNode,
FormNodeEditTypes,
FormNodeWidth,
} from '@forms/services/dynamic-form.types';
import { HgvDimensionsTemplate } from '@forms/templates/hgv/hgv-dimensions.template';
import { PsvDimensionsTemplate } from '@forms/templates/psv/psv-dimensions.template';
import { TrlDimensionsTemplate } from '@forms/templates/trl/trl-dimensions.template';
import { VehicleTypes } from '@models/vehicle-tech-record.model';
import { Subject, debounceTime, takeUntil } from 'rxjs';
import { WarningsEnum } from '@shared/enums/warnings.enum';
import { debounceTime, Subject, takeUntil } from 'rxjs';
import { DimensionLabelEnum } from '@shared/enums/dimension-label.enum';

Check warning on line 20 in src/app/forms/custom-sections/dimensions/dimensions.component.ts

View workflow job for this annotation

GitHub Actions / lint (18.16.0)

'DimensionLabelEnum' is defined but never used

@Component({
selector: 'app-dimensions',
Expand All @@ -32,7 +39,31 @@
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.form = this.dfs.createForm(this.template!, this.techRecord) as CustomFormGroup;

this.form.cleanValueChanges.pipe(debounceTime(400), takeUntil(this.destroy$)).subscribe((e) => this.formChange.emit(e));
this.form.cleanValueChanges.pipe(debounceTime(400), takeUntil(this.destroy$)).subscribe((e) => {
this.initialiseWarnings();
this.formChange.emit(e);
});
}

initialiseWarnings() {
// eslint-disable-next-line no-restricted-syntax, guard-for-in
for (const controlKey in this.form.controls) {
const control = this.form.get(controlKey);
if (control instanceof CustomFormControl) {
if (this.techRecord.techRecord_vehicleType === 'hgv' || this.techRecord.techRecord_vehicleType === 'trl') {
if (this.isLengthControl(control)) {
this.handleWarningChange(control, this.shouldDisplayLengthWarning(control), WarningsEnum.DIMENSIONS_LENGTH_WARNING);
}
if (this.isWidthControl(control)) {
this.handleWarningChange(control, this.shouldDisplayWidthWarning(control), WarningsEnum.DIMENSIONS_WIDTH_WARNING);
}
}
}
}
}

handleWarningChange(control: CustomFormControl, shouldDisplay: boolean, warning: WarningsEnum) {
control.meta.warning = shouldDisplay ? warning : undefined;
}

ngOnChanges(changes: SimpleChanges): void {
Expand All @@ -43,6 +74,20 @@
}
}

isLengthControl(control: CustomFormControl): boolean {
return control.meta.name === 'techRecord_dimensions_length';
}

isWidthControl(control: CustomFormControl): boolean {
return control.meta.name === 'techRecord_dimensions_height';
}
shouldDisplayLengthWarning(control: CustomFormControl): boolean {
return parseInt(control.value, 10) > 12000;
}
shouldDisplayWidthWarning(control: CustomFormControl): boolean {
return parseInt(control.value, 10) > 2600;
}

ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
Expand Down
17 changes: 9 additions & 8 deletions src/app/forms/templates/hgv/hgv-dimensions.template.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ValidatorNames } from '@forms/models/validators.enum';
import { TagType } from '@shared/components/tag/tag.component';
import { DimensionLabelEnum } from '@shared/enums/dimension-label.enum';
import {
FormNode, FormNodeEditTypes, FormNodeTypes, TagTypeLabels,
} from '../../services/dynamic-form.types';
Expand All @@ -11,15 +12,15 @@ export const HgvDimensionsTemplate: FormNode = {
children: [
{
name: 'techRecord_dimensions_length',
label: 'Length (mm)',
label: DimensionLabelEnum.LENGTH,
value: null,
type: FormNodeTypes.CONTROL,
validators: [{ name: ValidatorNames.Max, args: 99999 }],
customTags: [{ colour: TagType.PURPLE, label: TagTypeLabels.PLATES }],
},
{
name: 'techRecord_dimensions_width',
label: 'Width (mm)',
label: DimensionLabelEnum.WIDTH,
value: null,
type: FormNodeTypes.CONTROL,
validators: [{ name: ValidatorNames.Max, args: 99999 }],
Expand All @@ -35,7 +36,7 @@ export const HgvDimensionsTemplate: FormNode = {
children: [
{
name: 'value',
label: 'Axle to axle (mm)',
label: DimensionLabelEnum.AXLE_TO_AXLE,
value: null,
editType: FormNodeEditTypes.NUMBER,
type: FormNodeTypes.CONTROL,
Expand All @@ -47,37 +48,37 @@ export const HgvDimensionsTemplate: FormNode = {
},
{
name: 'techRecord_frontAxleToRearAxle',
label: 'Front axle to rear axle (mm)',
label: DimensionLabelEnum.FRONT_AXLE_TO_REAR_AXLE,
value: null,
type: FormNodeTypes.CONTROL,
validators: [{ name: ValidatorNames.Max, args: 99999 }],
},
{
name: 'techRecord_frontVehicleTo5thWheelCouplingMin',
label: 'Minimum',
label: DimensionLabelEnum.MINIMUM,
value: null,
type: FormNodeTypes.CONTROL,
validators: [{ name: ValidatorNames.Max, args: 99999 }],
customTags: [{ colour: TagType.PURPLE, label: TagTypeLabels.PLATES }],
},
{
name: 'techRecord_frontVehicleTo5thWheelCouplingMax',
label: 'Maximum',
label: DimensionLabelEnum.MAXIMUM,
value: null,
type: FormNodeTypes.CONTROL,
validators: [{ name: ValidatorNames.Max, args: 99999 }],
customTags: [{ colour: TagType.PURPLE, label: TagTypeLabels.PLATES }],
},
{
name: 'techRecord_frontAxleTo5thWheelMin',
label: 'Minimum',
label: DimensionLabelEnum.MINIMUM,
value: null,
type: FormNodeTypes.CONTROL,
validators: [{ name: ValidatorNames.Max, args: 99999 }],
},
{
name: 'techRecord_frontAxleTo5thWheelMax',
label: 'Maximum',
label: DimensionLabelEnum.MAXIMUM,
value: null,
type: FormNodeTypes.CONTROL,
validators: [{ name: ValidatorNames.Max, args: 99999 }],
Expand Down
9 changes: 5 additions & 4 deletions src/app/forms/templates/psv/psv-dimensions.template.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ValidatorNames } from '@forms/models/validators.enum';
import { DimensionLabelEnum } from '@shared/enums/dimension-label.enum';
import { FormNode, FormNodeTypes } from '../../services/dynamic-form.types';

export const PsvDimensionsTemplate: FormNode = {
Expand All @@ -8,28 +9,28 @@ export const PsvDimensionsTemplate: FormNode = {
children: [
{
name: 'techRecord_dimensions_height',
label: 'Height (mm)',
label: DimensionLabelEnum.HEIGHT,
value: null,
type: FormNodeTypes.CONTROL,
validators: [{ name: ValidatorNames.Max, args: 99999 }],
},
{
name: 'techRecord_dimensions_length',
label: 'Length (mm)',
label: DimensionLabelEnum.LENGTH,
value: null,
type: FormNodeTypes.CONTROL,
validators: [{ name: ValidatorNames.Max, args: 99999 }],
},
{
name: 'techRecord_dimensions_width',
label: 'Width (mm)',
label: DimensionLabelEnum.WIDTH,
value: null,
type: FormNodeTypes.CONTROL,
validators: [{ name: ValidatorNames.Max, args: 99999 }],
},
{
name: 'techRecord_frontAxleToRearAxle',
label: 'Front axle to rear axle (mm)',
label: DimensionLabelEnum.FRONT_AXLE_TO_REAR_AXLE,
value: null,
type: FormNodeTypes.CONTROL,
validators: [{ name: ValidatorNames.Max, args: 99999 }],
Expand Down
21 changes: 11 additions & 10 deletions src/app/forms/templates/trl/trl-dimensions.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import {
FormNode, FormNodeEditTypes, FormNodeTypes, TagTypeLabels,
} from '../../services/dynamic-form.types';
import { DimensionLabelEnum } from '@shared/enums/dimension-label.enum';

Check failure on line 6 in src/app/forms/templates/trl/trl-dimensions.template.ts

View workflow job for this annotation

GitHub Actions / lint (18.16.0)

`@shared/enums/dimension-label.enum` import should occur before import of `../../services/dynamic-form.types`

export const TrlDimensionsTemplate: FormNode = {
name: 'dimensionsSection',
Expand All @@ -11,15 +12,15 @@
children: [
{
name: 'techRecord_dimensions_length',
label: 'Length (mm)',
label: DimensionLabelEnum.LENGTH,
value: null,
type: FormNodeTypes.CONTROL,
validators: [{ name: ValidatorNames.Max, args: 99999 }],
customTags: [{ colour: TagType.PURPLE, label: TagTypeLabels.PLATES }],
},
{
name: 'techRecord_dimensions_width',
label: 'Width (mm)',
label: DimensionLabelEnum.WIDTH,
value: null,
type: FormNodeTypes.CONTROL,
validators: [{ name: ValidatorNames.Max, args: 99999 }],
Expand All @@ -36,7 +37,7 @@
children: [
{
name: 'value',
label: 'Axle to axle (mm)',
label: DimensionLabelEnum.AXLE_TO_AXLE,
value: null,
editType: FormNodeEditTypes.NUMBER,
type: FormNodeTypes.CONTROL,
Expand All @@ -48,50 +49,50 @@
},
{
name: 'techRecord_frontAxleToRearAxle',
label: 'Front axle to rear axle (mm)',
label: DimensionLabelEnum.FRONT_AXLE_TO_REAR_AXLE,
value: null,
type: FormNodeTypes.CONTROL,
validators: [{ name: ValidatorNames.Max, args: 99999 }],
},
{
name: 'techRecord_rearAxleToRearTrl',
label: 'Rear axle to rear trailer',
label: DimensionLabelEnum.REAR_AXLE_TO_REAR_TRAILER,
value: null,
type: FormNodeTypes.CONTROL,
validators: [{ name: ValidatorNames.Max, args: 99999 }],
},
{
name: 'techRecord_centreOfRearmostAxleToRearOfTrl',
label: 'Center of Rear axle to rear of trailer',
label: DimensionLabelEnum.CENTER_REAR_AXLE_TO_REAR_TRAILER,
value: null,
type: FormNodeTypes.CONTROL,
validators: [{ name: ValidatorNames.Max, args: 99999 }],
},
{
name: 'techRecord_couplingCenterToRearAxleMin',
label: 'Minimum',
label: DimensionLabelEnum.MINIMUM,
value: null,
type: FormNodeTypes.CONTROL,
validators: [{ name: ValidatorNames.Max, args: 99999 }],
},
{
name: 'techRecord_couplingCenterToRearAxleMax',
label: 'Maximum',
label: DimensionLabelEnum.MAXIMUM,
value: null,
type: FormNodeTypes.CONTROL,
validators: [{ name: ValidatorNames.Max, args: 99999 }],
},
{
name: 'techRecord_couplingCenterToRearTrlMin',
label: 'Minimum',
label: DimensionLabelEnum.MINIMUM,
value: null,
type: FormNodeTypes.CONTROL,
validators: [{ name: ValidatorNames.Max, args: 99999 }],
customTags: [{ colour: TagType.PURPLE, label: TagTypeLabels.PLATES }],
},
{
name: 'techRecord_couplingCenterToRearTrlMax',
label: 'Maximum',
label: DimensionLabelEnum.MAXIMUM,
value: null,
type: FormNodeTypes.CONTROL,
validators: [{ name: ValidatorNames.Max, args: 99999 }],
Expand Down
11 changes: 11 additions & 0 deletions src/app/shared/enums/dimension-label.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export enum DimensionLabelEnum {
LENGTH = 'Length (mm)',
WIDTH = 'Width (mm)',
HEIGHT = 'Height (mm)',
FRONT_AXLE_TO_REAR_AXLE = 'Front axle to rear axle (mm)',
AXLE_TO_AXLE = 'Axle to axle (mm)',
REAR_AXLE_TO_REAR_TRAILER = 'Rear axle to rear trailer',
CENTER_REAR_AXLE_TO_REAR_TRAILER = 'Center of Rear axle to rear of trailer',
MINIMUM = 'Minimum',
MAXIMUM = 'Maximum',
}
4 changes: 4 additions & 0 deletions src/app/shared/enums/warnings.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum WarningsEnum {
DIMENSIONS_LENGTH_WARNING = 'This length dimension field value is greater than 12,000mm. Check your input before proceeding',
DIMENSIONS_WIDTH_WARNING = 'This width dimension field value is greater than 2,600mm. Check your input before proceeding',
}
Loading