feat: DELETE button to categories

This commit is contained in:
filippo-ferrari 2024-05-25 15:50:56 +02:00
parent a6063690e9
commit b2ee23c81d

View file

@ -39,11 +39,11 @@ public class CategoriesView extends Div implements BeforeEnterObserver {
private final Button cancel = new Button("Cancel"); private final Button cancel = new Button("Cancel");
private final Button save = new Button("Save"); private final Button save = new Button("Save");
private final Button delete = new Button("Delete");
private final BeanValidationBinder<Category> binder; private final BeanValidationBinder<Category> binder;
private Category category; private Category category;
private final CategoryService categoryService; private final CategoryService categoryService;
private TextField name; 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"); 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 @Override
@ -156,7 +172,8 @@ public class CategoriesView extends Div implements BeforeEnterObserver {
buttonLayout.setClassName("button-layout"); buttonLayout.setClassName("button-layout");
cancel.addThemeVariants(ButtonVariant.LUMO_TERTIARY); cancel.addThemeVariants(ButtonVariant.LUMO_TERTIARY);
save.addThemeVariants(ButtonVariant.LUMO_PRIMARY); save.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
buttonLayout.add(save, cancel); delete.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
buttonLayout.add(save, delete, cancel);
editorLayoutDiv.add(buttonLayout); editorLayoutDiv.add(buttonLayout);
} }