-
Notifications
You must be signed in to change notification settings - Fork 29
/
search-parameters-dialog.component.ts
112 lines (99 loc) · 3.4 KB
/
search-parameters-dialog.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { ChangeDetectorRef, Component, Inject } from "@angular/core";
import { FormControl, FormGroup, Validators } from "@angular/forms";
import { MatDialogRef, MAT_DIALOG_DATA } from "@angular/material/dialog";
import { AppConfigService } from "app-config.service";
import { map, startWith } from "rxjs/operators";
import { UnitsService } from "shared/services/units.service";
import { ScientificCondition } from "../../../state-management/models";
@Component({
selector: "search-parameters-dialog",
templateUrl: "./search-parameters-dialog.component.html",
})
export class SearchParametersDialogComponent {
appConfig = this.appConfigService.getConfig();
unitsEnabled = this.appConfig.scienceSearchUnitsEnabled;
parameterKeys = this.data.parameterKeys;
units: string[] = [];
parametersForm = new FormGroup({
lhs: new FormControl(this.data.condition?.lhs || "", [
Validators.required,
Validators.minLength(2),
]),
relation: new FormControl(this.data.condition?.relation || "GREATER_THAN", [
Validators.required,
Validators.minLength(9),
]),
rhs: new FormControl<string | number>(this.data.condition?.rhs || "", [
Validators.required,
Validators.minLength(1),
]),
unit: new FormControl(""),
});
filteredUnits$ = this.parametersForm.get("unit")?.valueChanges.pipe(
startWith(""),
map((value: string) =>
this.units.filter((unit) =>
unit.toLowerCase().includes(value.toLowerCase()),
),
),
);
filteredKeys$ = this.parametersForm.get("lhs")?.valueChanges.pipe(
startWith(""),
map((value: string) =>
this.parameterKeys.filter((key) =>
key.toLowerCase().includes(value.toLowerCase()),
),
),
);
constructor(
public appConfigService: AppConfigService,
@Inject(MAT_DIALOG_DATA)
public data: {
parameterKeys: string[];
condition?: ScientificCondition;
},
public dialogRef: MatDialogRef<SearchParametersDialogComponent>,
private unitsService: UnitsService,
) {
if (this.data.condition?.lhs) {
this.getUnits(this.data.condition.lhs);
}
}
add = (): void => {
const { lhs, relation, unit } = this.parametersForm.value;
const rawRhs = this.parametersForm.get("rhs")?.value;
const rhs =
relation === "EQUAL_TO_STRING" ? String(rawRhs) : Number(rawRhs);
this.parametersForm.patchValue({ rhs });
this.dialogRef.close({ data: { lhs, relation, rhs, unit } });
};
cancel = (): void => this.dialogRef.close();
getUnits = (parameterKey: string): void => {
this.units = this.unitsService.getUnits(parameterKey);
this.toggleUnitField();
};
toggleUnitField = (): void => {
const lhsInvalid = this.parametersForm.get("lhs")?.invalid;
const { relation } = this.parametersForm.value;
const isStringRelation = relation === "EQUAL_TO_STRING" ? true : false;
const unitField = this.parametersForm.get("unit");
unitField?.enable();
if (lhsInvalid || isStringRelation) {
unitField?.disable();
}
};
isInvalid = (): boolean => {
const { invalid } = this.parametersForm;
const { lhs, relation, rhs } = this.parametersForm.value;
if (invalid) {
return invalid;
}
if (relation !== "EQUAL_TO_STRING" && isNaN(Number(rhs))) {
return true;
}
return lhs.length * (rhs as string).length === 0;
};
get lhs(): string {
return this.parametersForm.get("lhs")?.value;
}
}