Skip to content

Commit 3ed358d

Browse files
committed
Validate date on frontend
1 parent ab7af98 commit 3ed358d

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

src/app/shared/components/forms/form-body-application/form-body-application.component.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@ export class FormBodyApplicationComponent implements OnInit, OnDestroy {
142142
}
143143

144144
onSubmit(): void {
145+
if (this.serializedStartDate.invalid) {
146+
// Depending on what date component is used, it might use its own highlighting
147+
this.handleError(this.buildErrorMessage('MESSAGE.INVALID-DATE'), 'application.startDate');
148+
return;
149+
} else if (this.serializedEndDate.invalid) {
150+
this.handleError(this.buildErrorMessage('MESSAGE.INVALID-DATE'), 'application.endDate');
151+
return;
152+
}
153+
145154
this.application.organizationId = this.sharedVariableService.getSelectedOrganisationId();
146155
this.application.startDate = this.serializedStartDate.value?.toISOString();
147156
this.application.endDate = this.serializedEndDate.value?.toISOString();
@@ -154,6 +163,14 @@ export class FormBodyApplicationComponent implements OnInit, OnDestroy {
154163
}
155164
}
156165

166+
private buildErrorMessage(message: string): Pick<HttpErrorResponse, 'error'> {
167+
return {
168+
error: {
169+
message,
170+
},
171+
};
172+
}
173+
157174
updateApplication(id: number): void {
158175
this.applicationService
159176
.updateApplication(this.application, id)
@@ -181,21 +198,21 @@ export class FormBodyApplicationComponent implements OnInit, OnDestroy {
181198
);
182199
}
183200

184-
private handleError(error: HttpErrorResponse) {
201+
private handleError(error: Pick<HttpErrorResponse, 'error'>, errorField?: string) {
185202
this.errorFields = [];
186203
this.errorMessages = [];
187204

188205
// Temp fix till we standardise backend error handling
189206
if (error.error?.message[0]?.property) {
190207
this.externalError(error);
191208
} else {
192-
this.backendError(error);
209+
this.backendError(error, errorField);
193210
}
194211

195212
this.formFailedSubmit = true;
196213
}
197214

198-
externalError(error: HttpErrorResponse) {
215+
externalError(error: Pick<HttpErrorResponse, 'error'>) {
199216
error.error.message.forEach((err) => {
200217
this.errorFields.push(err.property);
201218
this.errorMessages = this.errorMessages.concat(
@@ -205,12 +222,12 @@ export class FormBodyApplicationComponent implements OnInit, OnDestroy {
205222
this.formFailedSubmit = true;
206223
}
207224

208-
backendError(error: HttpErrorResponse) {
225+
backendError(error: Pick<HttpErrorResponse, 'error'>, errorField = 'name') {
209226
this.translate.get([error.error.message])
210227
.subscribe(translations => {
211228
this.errorMessages.push(translations[error.error.message]);
212229
});
213-
this.errorFields.push('name');
230+
this.errorFields.push(errorField);
214231
}
215232

216233
routeBack(): void {

src/app/shared/components/metadata-details/metadata-details.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<p *ngIf="status">
1+
<p *ngIf="status && status !== ApplicationStatus.NONE">
22
<strong>{{ 'APPLICATION.METADATA-FIELD.STATUS' | translate }}</strong>{{ 'APPLICATION.STATUS.' + status | translate }}
33
</p>
44
<p *ngIf="startDate">

src/app/shared/components/metadata-details/metadata-details.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export class MetadataDetailsComponent implements OnInit {
2828

2929
entries = Object.entries;
3030
toPascalKebabCase = toPascalKebabCase;
31+
ApplicationStatus = ApplicationStatus;
3132

3233
constructor(private translate: TranslateService) {}
3334

src/assets/i18n/da.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,8 @@
762762
"APPLICATION-DOES-NOT-EXIST": "Den tilhørende applikation findes ikke",
763763
"FAILED-TO-CREATE-OR-UPDATE-IOT-DEVICE": "Enheden kunne ikke oprettes eller opdateres",
764764
"DEVICE-MODEL-ORGANIZATION-DOES-NOT-MATCH": "Organisationsid'et på device modellen matcher ikke den tilhørende applikation",
765-
"DEVICE-MODEL-DOES-NOT-EXIST": "Device model findes ikke"
765+
"DEVICE-MODEL-DOES-NOT-EXIST": "Device model findes ikke",
766+
"INVALID-DATE": "Datoen er ugyldig"
766767
},
767768
"PROFILES": {
768769
"NAME": "LoRaWAN profiler",

0 commit comments

Comments
 (0)