From a6829fc3f5398a9fa414a93b5e3ca28437b4c051 Mon Sep 17 00:00:00 2001 From: Jan-Marlon Leibl Date: Tue, 3 Dec 2024 09:23:50 +0100 Subject: [PATCH] add better validation to forms --- src/app/form.guard.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/app/form.guard.ts b/src/app/form.guard.ts index bec14c4..60b62b7 100644 --- a/src/app/form.guard.ts +++ b/src/app/form.guard.ts @@ -3,11 +3,11 @@ import { HotelFormComponent } from './hotel-form/hotel-form.component'; import { HotelDetailsComponent } from './hotel-details/hotel-details.component'; export const formGuard: CanDeactivateFn = (component: HotelFormComponent | HotelDetailsComponent, currentRoute, currentState, nextState) => { - console.log(component) - if (component instanceof HotelFormComponent && component.form?.dirty) { - return confirm('You have unsaved changes. Do you really want to leave?'); - } - else if (component instanceof HotelDetailsComponent && component.hotelForm?.form?.dirty) { + const hasUnsavedChanges = + (component instanceof HotelFormComponent && component.form?.dirty) || + (component instanceof HotelDetailsComponent && component.hotelForm?.form?.dirty); + + if (hasUnsavedChanges) { return confirm('You have unsaved changes. Do you really want to leave?'); }