From b2ee23c81deb1c994f7f94919e9265d811c36621 Mon Sep 17 00:00:00 2001 From: filippo-ferrari Date: Sat, 25 May 2024 15:50:56 +0200 Subject: [PATCH] feat: DELETE button to categories --- .../munera/views/expenses/CategoriesView.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/application/munera/views/expenses/CategoriesView.java b/src/main/java/com/application/munera/views/expenses/CategoriesView.java index 17dfd1c..7b82b3c 100644 --- a/src/main/java/com/application/munera/views/expenses/CategoriesView.java +++ b/src/main/java/com/application/munera/views/expenses/CategoriesView.java @@ -39,11 +39,11 @@ public class CategoriesView extends Div implements BeforeEnterObserver { private final Button cancel = new Button("Cancel"); private final Button save = new Button("Save"); + private final Button delete = new Button("Delete"); private final BeanValidationBinder binder; private Category category; - private final CategoryService categoryService; private TextField name; @@ -112,6 +112,22 @@ public class CategoriesView extends Div implements BeforeEnterObserver { Notification.show("Failed to update the data. Check again that all values are valid"); } }); + + delete.addClickListener(e -> { + try { + if (this.category == null) throw new RuntimeException("Category is null!"); //TODO: create proper exception + categoryService.delete(this.category); + clearForm(); + refreshGrid(); + Notification.show("Data deleted"); + UI.getCurrent().navigate(CategoriesView.class); + } catch (ObjectOptimisticLockingFailureException exception) { + Notification n = Notification.show( + "Error updating the data. Somebody else has updated the record while you were making changes."); + n.setPosition(Notification.Position.MIDDLE); + n.addThemeVariants(NotificationVariant.LUMO_ERROR); + } + }); } @Override @@ -156,7 +172,8 @@ public class CategoriesView extends Div implements BeforeEnterObserver { buttonLayout.setClassName("button-layout"); cancel.addThemeVariants(ButtonVariant.LUMO_TERTIARY); save.addThemeVariants(ButtonVariant.LUMO_PRIMARY); - buttonLayout.add(save, cancel); + delete.addThemeVariants(ButtonVariant.LUMO_PRIMARY); + buttonLayout.add(save, delete, cancel); editorLayoutDiv.add(buttonLayout); }