-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmetadata-input-base.ts
147 lines (146 loc) · 4.97 KB
/
metadata-input-base.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import {
AbstractControl,
FormGroup,
ValidationErrors,
ValidatorFn,
Validators,
} from "@angular/forms";
import { Observable } from "rxjs";
import { UnitsService } from "shared/services/units.service";
import { startWith, map } from "rxjs/operators";
import { DateTimeService } from "shared/services/date-time.service";
export enum Type {
quantity = "quantity",
date = "date",
number = "number",
string = "string",
boolean = "boolean",
}
export class MetadataInputBase {
unitsService: UnitsService;
metadataForm: FormGroup;
filteredUnits$: Observable<string[]>;
units: string[];
typeValues: string[] = Object.values(Type);
dateTimeService: DateTimeService;
constructor() {
this.unitsService = new UnitsService();
this.unitsService.getUnits();
this.dateTimeService = new DateTimeService();
}
unitValidator(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const allowed = this.unitsService.getUnits().includes(control.value);
return allowed ? null : { forbiddenUnit: "Invalid unit" };
};
}
dateValidator(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const isValid = this.dateTimeService.isValidDateTime(control.value);
return isValid ? null : { invalidDate: "Invalid date or format" };
};
}
booleanValidator(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const value = String(control.value).toLowerCase();
const valid = value === "true" || value === "false";
return valid
? null
: { invalidBoolean: 'Boolean must be "true" or "false"' }; // eslint-disable-line @typescript-eslint/quotes
};
}
numberValidator(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const invalid = control.value === "" || isNaN(Number(control.value));
return invalid ? { invalidNumber: "Invalid number" } : null;
};
}
detectType() {
const type = this.metadataForm.get("type").value;
this.metadataForm.get("value").clearValidators();
this.metadataForm.get("date").disable();
switch (type) {
case Type.quantity:
this.metadataForm.get("unit").enable();
this.metadataForm
.get("value")
.setValidators([Validators.required, this.numberValidator()]);
break;
case Type.date:
this.metadataForm.get("unit").disable();
this.metadataForm.get("date").enable();
this.metadataForm
.get("date")
.setValidators([Validators.required, this.dateValidator()]);
break;
case Type.boolean:
this.metadataForm.get("unit").disable();
this.metadataForm
.get("value")
.setValidators([Validators.required, this.booleanValidator()]);
break;
case Type.number:
this.metadataForm.get("unit").disable();
this.metadataForm
.get("value")
.setValidators([Validators.required, this.numberValidator()]);
break;
default:
this.metadataForm.get("unit").disable();
this.metadataForm
.get("value")
.setValidators([Validators.required, Validators.minLength(1)]);
}
this.metadataForm.get("unit").updateValueAndValidity();
this.metadataForm.get("value").updateValueAndValidity();
}
getType() {
return this.metadataForm.get("type").value;
}
getUnits(fieldName: string): void {
const name = this.metadataForm.get(fieldName).value;
this.units = this.unitsService.getUnits(name);
this.filteredUnits$ = this.metadataForm.get("unit").valueChanges.pipe(
startWith(""),
map((value: string) => {
const filterValue = value.toLowerCase();
return this.units.filter((unit) =>
unit.toLowerCase().includes(filterValue),
);
}),
);
}
fieldHasError(field: string): boolean {
return this.metadataForm.get(field).errors ? true : false;
}
getErrorMessage(field: string) {
switch (field) {
case "value":
if (this.metadataForm.get(field).hasError("required")) {
return "Value is required";
}
if (this.metadataForm.get(field).hasError("invalidNumber")) {
return this.metadataForm.get(field).getError("invalidNumber");
}
if (this.metadataForm.get(field).hasError("invalidBoolean")) {
return this.metadataForm.get(field).getError("invalidBoolean");
}
return null;
case "date":
if (this.metadataForm.get(field).hasError("invalidDate")) {
return this.metadataForm.get(field).getError("invalidDate");
}
return null;
case "unit":
if (this.metadataForm.get(field).hasError("required")) {
return "A unit is required for quantities";
}
if (this.metadataForm.get(field).hasError("forbiddenUnit")) {
return this.metadataForm.get(field).getError("forbiddenUnit");
}
return null;
default:
return null;
}
}
}