Skip to content

Commit b1fbf15

Browse files
feat: Add cost per kilometer below soft max to transition attributes (#225)
1 parent d568471 commit b1fbf15

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

application/frontend/src/app/core/components/transition-attributes-dialog/transition-attributes-dialog.component.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,23 @@
169169
Must be greater than zero
170170
</mat-error>
171171
</div>
172+
<div class="settings-item">
173+
<mat-form-field appearance="outline" class="unpadded-form-field">
174+
<mat-label>Distance limit cost below soft max</mat-label>
175+
<input matInput type="number" formControlName="distanceLimitCostBelowSoftMax" />
176+
</mat-form-field>
177+
<mat-error
178+
*ngIf="transitionAttributes.get('distanceLimitCostBelowSoftMax').errors?.min">
179+
Must be greater than zero
180+
</mat-error>
181+
</div>
172182
</div>
173183
<mat-error
174184
*ngIf="
175185
transitionAttributes.errors?.distanceLimitSoftCostRequired ||
176186
transitionAttributes.errors?.distanceLimitSoftMaxRequired
177187
">
178-
<span> Soft max and cost must both be defined </span>
188+
<span> Soft max and at least one cost must both be defined </span>
179189
</mat-error>
180190
</div>
181191
<!-- Delay -->

application/frontend/src/app/core/components/transition-attributes-dialog/transition-attributes-dialog.component.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { BehaviorSubject, combineLatest } from 'rxjs';
1919
import {
2020
aRequiredIfB,
2121
durationMinutesSeconds,
22+
oneOfARequiredIfB,
2223
requireAxorB,
2324
secondsToDuration,
2425
} from 'src/app/util';
@@ -52,6 +53,10 @@ export class TransitionAttributesDialogComponent {
5253
cost: [value?.cost, Validators.min(0)],
5354
costPerKilometer: [value?.costPerKilometer, Validators.min(0)],
5455
distanceLimitSoftMax: [value?.distanceLimit?.softMaxMeters, Validators.min(0)],
56+
distanceLimitCostBelowSoftMax: [
57+
value?.distanceLimit?.costPerKilometerBelowSoftMax,
58+
Validators.min(0),
59+
],
5560
distanceLimitCostAboveSoftMax: [
5661
value?.distanceLimit?.costPerKilometerAboveSoftMax,
5762
Validators.min(0),
@@ -66,8 +71,8 @@ export class TransitionAttributesDialogComponent {
6671
validators: [
6772
requireAxorB('srcTag', 'excludedSrcTag', 'srcTagOrExcludeSrcTag'),
6873
requireAxorB('dstTag', 'excludedDstTag', 'dstTagOrExcludeDstTag'),
69-
aRequiredIfB(
70-
'distanceLimitCostAboveSoftMax',
74+
oneOfARequiredIfB(
75+
['distanceLimitCostAboveSoftMax', 'distanceLimitCostBelowSoftMax'],
7176
'distanceLimitSoftMax',
7277
'distanceLimitSoftCostRequired'
7378
),
@@ -76,6 +81,11 @@ export class TransitionAttributesDialogComponent {
7681
'distanceLimitCostAboveSoftMax',
7782
'distanceLimitSoftMaxRequired'
7883
),
84+
aRequiredIfB(
85+
'distanceLimitSoftMax',
86+
'distanceLimitCostBelowSoftMax',
87+
'distanceLimitSoftMaxRequired'
88+
),
7989
],
8090
}
8191
);
@@ -160,6 +170,7 @@ export class TransitionAttributesDialogComponent {
160170
costPerKilometer: transition.costPerKilometer,
161171
distanceLimit: {
162172
softMaxMeters: transition.distanceLimitSoftMax,
173+
costPerKilometerBelowSoftMax: transition.distanceLimitCostBelowSoftMax,
163174
costPerKilometerAboveSoftMax: transition.distanceLimitCostAboveSoftMax,
164175
},
165176
delay: secondsToDuration(transition.delay.min * 60 + transition.delay.sec),

application/frontend/src/app/util/validators.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,17 @@ export function requireAxorB(a: string, b: string, id = 'requireAxorB'): Validat
259259
return (!aValue && !bValue) || (aValue && bValue) ? { [id]: true } : null;
260260
};
261261
}
262+
263+
export function oneOfARequiredIfB(a: string[], b: string, id = 'oneOfARequiredIfB'): ValidatorFn {
264+
return (fg: UntypedFormGroup) => {
265+
const bValue = fg.get(b).value;
266+
if (bValue == null) {
267+
return null;
268+
}
269+
270+
const returnVal = !a.some((element) => fg.get(element).value) && {
271+
[id]: { ...a },
272+
};
273+
return returnVal;
274+
};
275+
}

0 commit comments

Comments
 (0)