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

fix(dh): request calculation validation issues #3542

Merged
merged 12 commits into from
Sep 17, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,7 @@
"gridArea": "Netområde",
"request": "Anmod",
"maxPeriodLength": "Perioden må max være 31 dage",
"monthOnlyError": "Perioden skal være en hel måned",
"calculationTypes": {
"BALANCE_FIXING": "Balancefiksering",
"AGGREGATION": "Aggregering",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,7 @@
"gridarea": "Grid area",
"request": "Request",
"maxPeriodLength": "Period cannot be longer than 31 days",
"monthOnlyError": "Period must be a full month",
"calculationTypes": {
"BALANCE_FIXING": "Balance fixing",
"AGGREGATION": "Aggregation",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
-->
@if (isReady) {
<watt-card vater inset="0" *transloco="let t; read: 'wholesale.requestCalculation'">
<form [formGroup]="form" (ngSubmit)="requestCalculation()">
<form [formGroup]="form" (ngSubmit)="form.valid && requestCalculation()">
<vater-flex fill="vertical" gap="m">
<vater-stack align="flex-start" direction="column" gap="s">
<watt-dropdown
Expand All @@ -38,6 +38,8 @@
>
@if (form.controls.period.errors?.max31DaysDateRange) {
<watt-field-error> {{ t('maxPeriodLength')}} </watt-field-error>
} @else if (form.controls.period.errors?.monthOnly) {
<watt-field-error> {{ t('monthOnlyError')}} </watt-field-error>
}
</watt-datepicker>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
import { RxPush } from '@rx-angular/template/push';
import { Apollo, MutationResult } from 'apollo-angular';
import { TranslocoDirective, TranslocoService } from '@ngneat/transloco';
import { catchError, filter, map, of, startWith, switchMap, tap } from 'rxjs';
import { catchError, filter, map, of, tap } from 'rxjs';

import {
RequestCalculationDocument,
Expand Down Expand Up @@ -244,23 +244,15 @@ export class DhWholesaleRequestCalculationComponent {
this.form.controls.energySupplierId.setValue(glnOrEicNumber);
}
}),
switchMap(({ marketRole }) =>
this.form.controls.calculationType.valueChanges.pipe(
startWith(this.form.controls.calculationType.value),
exists(),
map((value) => wholesaleCalculationTypes.includes(value)),
tap((isWholesale) => {
if (isWholesale && marketRole !== EicFunction.GridAccessProvider) {
this.gridAreaIsRequired.set(false);
this.form.controls.gridArea.removeValidators(Validators.required);
} else {
this.gridAreaIsRequired.set(true);
this.form.controls.gridArea.setValidators(Validators.required);
}
}),
tap(() => this.form.controls.gridArea.updateValueAndValidity())
)
),
tap(({ marketRole }) => {
if (marketRole !== EicFunction.GridAccessProvider) {
this.gridAreaIsRequired.set(false);
this.form.controls.gridArea.removeValidators(Validators.required);
} else {
this.gridAreaIsRequired.set(true);
this.form.controls.gridArea.setValidators(Validators.required);
}
ManBearTM marked this conversation as resolved.
Show resolved Hide resolved
}),
takeUntilDestroyed()
)
.subscribe();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
aria-label="start-date-input"
matStartDate
#startDateInput
[readOnly]="rangeMonthOnlyMode"
[readOnly]="rangeMonthOnlyMode()"
autocomplete="off"
spellcheck="false"
[hidden]="true"
Expand All @@ -40,7 +40,7 @@
aria-label="end-date-input"
matEndDate
#endDateInput
[readOnly]="rangeMonthOnlyMode"
[readOnly]="rangeMonthOnlyMode()"
autocomplete="off"
spellcheck="false"
[hidden]="true"
Expand All @@ -65,8 +65,8 @@
/>

<mat-date-range-picker
[panelClass]="rangeMonthOnlyMode ? 'watt-datepicker-range__panel--month-only' : ''"
[startView]="rangeMonthOnlyMode ? 'multi-year' : 'month'"
[panelClass]="rangeMonthOnlyMode() ? 'watt-datepicker-range__panel--month-only' : ''"
[startView]="rangeMonthOnlyMode() ? 'multi-year' : 'month'"
[startAt]="startAt"
(monthSelected)="onMonthSelected($event)"
(closed)="rangePickerClosed()"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import {
computed,
inject,
input,
AfterViewInit,
effect,
} from '@angular/core';
import { NgControl } from '@angular/forms';
import { AbstractControl, NgControl, Validator } from '@angular/forms';
import {
MAT_DATEPICKER_SCROLL_STRATEGY_FACTORY_PROVIDER,
MatCalendarCellClassFunction,
Expand All @@ -48,7 +50,7 @@ import { WattLocaleService } from '@energinet-datahub/watt/locale';
import { MaskitoModule } from '@maskito/angular';
import { maskitoDateOptionsGenerator, maskitoDateRangeOptionsGenerator } from '@maskito/kit';
import { WattSupportedLocales } from '../../../configuration/watt-date-adapter';
import { WattDateRange, dayjs } from '../../../utils/date';
import { WattDateRange, WattRange, dayjs } from '../../../utils/date';
import { WattButtonComponent } from '../../button';
import { WattPlaceholderMaskComponent } from '../shared/placeholder-mask/watt-placeholder-mask.component';
import { WattPickerBase } from '../shared/watt-picker-base';
Expand Down Expand Up @@ -84,7 +86,7 @@ export const danishTimeZoneIdentifier = 'Europe/Copenhagen';
WattPlaceholderMaskComponent,
],
})
export class WattDatepickerComponent extends WattPickerBase {
export class WattDatepickerComponent extends WattPickerBase implements Validator, AfterViewInit {
protected override elementRef = inject<ElementRef<HTMLElement>>(ElementRef);
protected override changeDetectionRef = inject(ChangeDetectorRef);
protected override ngControl = inject(NgControl, { optional: true, self: true });
Expand All @@ -94,9 +96,9 @@ export class WattDatepickerComponent extends WattPickerBase {

max = input<Date>();
min = input<Date>();
rangeMonthOnlyMode = input(false);

@Input() startAt: Date | null = null;
@Input() rangeMonthOnlyMode = false;
@Input() label = '';

/**
Expand Down Expand Up @@ -205,6 +207,25 @@ export class WattDatepickerComponent extends WattPickerBase {
this.datePlaceholder = this.getPlaceholderByLocale(locale);
this.rangePlaceholder = this.getRangePlaceholder();
});

effect(() => {
this.rangeMonthOnlyMode();
this.ngControl?.control?.updateValueAndValidity();
});
}

override ngAfterViewInit() {
dzhavat marked this conversation as resolved.
Show resolved Hide resolved
this.ngControl?.control?.setValidators(this.validate.bind(this));
}

validate({ value }: AbstractControl<WattRange<string>>) {
if (!value?.end || !value?.start) return null;
if (!this.rangeMonthOnlyMode()) return null;
const start = dayjs(value.start);
const end = dayjs(value.end);
return start.isSame(start.startOf('month')) && end.isSame(start.endOf('month'))
? null
: { monthOnly: true };
dzhavat marked this conversation as resolved.
Show resolved Hide resolved
}

protected initSingleInput() {
Expand Down Expand Up @@ -242,7 +263,7 @@ export class WattDatepickerComponent extends WattPickerBase {
}

onMonthSelected(date: Date) {
if (this.rangeMonthOnlyMode && date) {
if (this.rangeMonthOnlyMode() && date) {
this.matDateRangePicker.select(dayjs(date).startOf('month').toDate());
this.matDateRangePicker.select(dayjs(date).endOf('month').toDate());
this.matDateRangePicker.close();
Expand Down
Loading