feat: validation & improvements
This commit is contained in:
parent
b72103653b
commit
6ca9747180
1 changed files with 62 additions and 28 deletions
|
@ -1,5 +1,6 @@
|
||||||
package com.application.munera.views.settings;
|
package com.application.munera.views.settings;
|
||||||
|
|
||||||
|
import com.application.munera.data.User;
|
||||||
import com.application.munera.services.UserService;
|
import com.application.munera.services.UserService;
|
||||||
import com.application.munera.views.MainLayout;
|
import com.application.munera.views.MainLayout;
|
||||||
import com.vaadin.flow.component.button.Button;
|
import com.vaadin.flow.component.button.Button;
|
||||||
|
@ -7,16 +8,20 @@ import com.vaadin.flow.component.dependency.Uses;
|
||||||
import com.vaadin.flow.component.formlayout.FormLayout;
|
import com.vaadin.flow.component.formlayout.FormLayout;
|
||||||
import com.vaadin.flow.component.icon.Icon;
|
import com.vaadin.flow.component.icon.Icon;
|
||||||
import com.vaadin.flow.component.notification.Notification;
|
import com.vaadin.flow.component.notification.Notification;
|
||||||
|
import com.vaadin.flow.component.notification.NotificationVariant;
|
||||||
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
|
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
|
||||||
import com.vaadin.flow.component.textfield.EmailField;
|
import com.vaadin.flow.component.textfield.EmailField;
|
||||||
import com.vaadin.flow.component.textfield.PasswordField;
|
import com.vaadin.flow.component.textfield.PasswordField;
|
||||||
import com.vaadin.flow.component.textfield.TextField;
|
import com.vaadin.flow.component.textfield.TextField;
|
||||||
|
import com.vaadin.flow.data.binder.BeanValidationBinder;
|
||||||
|
import com.vaadin.flow.data.binder.ValidationException;
|
||||||
import com.vaadin.flow.router.BeforeEnterEvent;
|
import com.vaadin.flow.router.BeforeEnterEvent;
|
||||||
import com.vaadin.flow.router.BeforeEnterObserver;
|
import com.vaadin.flow.router.BeforeEnterObserver;
|
||||||
import com.vaadin.flow.router.PageTitle;
|
import com.vaadin.flow.router.PageTitle;
|
||||||
import com.vaadin.flow.router.Route;
|
import com.vaadin.flow.router.Route;
|
||||||
import jakarta.annotation.security.PermitAll;
|
import jakarta.annotation.security.PermitAll;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.orm.ObjectOptimisticLockingFailureException;
|
||||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
|
|
||||||
@PageTitle("Settings")
|
@PageTitle("Settings")
|
||||||
|
@ -27,59 +32,88 @@ public class SettingsView extends VerticalLayout implements BeforeEnterObserver
|
||||||
|
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
|
|
||||||
private TextField firstNameField;
|
private TextField firstName;
|
||||||
private TextField lastNameField;
|
private TextField lastName;
|
||||||
private PasswordField passwordField;
|
private PasswordField password;
|
||||||
private TextField monthlyIncomeField;
|
private TextField monthlyIncome;
|
||||||
private EmailField emailField;
|
private EmailField email;
|
||||||
|
private final BeanValidationBinder<User> binder;
|
||||||
|
private final Button save = new Button("Save");
|
||||||
|
private final User loggedInUser;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public SettingsView(UserService userService) {
|
public SettingsView(UserService userService) {
|
||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
|
|
||||||
createForm();
|
createForm();
|
||||||
|
|
||||||
|
loggedInUser = userService.getLoggedInUser().orElseThrow(() -> new UsernameNotFoundException("User not found"));
|
||||||
|
|
||||||
|
binder = new BeanValidationBinder<>(User.class);
|
||||||
|
// Bind fields. This is where you'd define e.g. validation rules
|
||||||
|
binder.bindInstanceFields(this);
|
||||||
|
binder.forField(firstName)
|
||||||
|
.asRequired("First name is required")
|
||||||
|
.bind(User::getFirstName, User::setFirstName);
|
||||||
|
|
||||||
|
binder.forField(lastName)
|
||||||
|
.asRequired("Last name is required")
|
||||||
|
.bind(User::getLastName, User::setLastName);
|
||||||
|
|
||||||
|
binder.forField(password)
|
||||||
|
.asRequired("Password is required")
|
||||||
|
.bind(User::getPassword, User::setPassword);
|
||||||
|
|
||||||
|
save.addClickListener(e -> {
|
||||||
|
try {
|
||||||
|
binder.writeBean(this.loggedInUser);
|
||||||
|
this.saveUserData();
|
||||||
|
Notification.show("User details updated successfully");
|
||||||
|
} 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);
|
||||||
|
} catch (ValidationException validationException) {
|
||||||
|
Notification.show("Failed to update the user. Check again that all values are valid");
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createForm() {
|
private void createForm() {
|
||||||
FormLayout formLayout = new FormLayout();
|
FormLayout formLayout = new FormLayout();
|
||||||
|
|
||||||
firstNameField = new TextField("First Name");
|
firstName = new TextField("First Name");
|
||||||
lastNameField = new TextField("Last Name");
|
lastName = new TextField("Last Name");
|
||||||
passwordField = new PasswordField("Password");
|
password = new PasswordField("Password");
|
||||||
emailField = new EmailField("Email");
|
email = new EmailField("Email");
|
||||||
monthlyIncomeField = new TextField("Monthly Income");
|
monthlyIncome = new TextField("Monthly Income");
|
||||||
|
|
||||||
formLayout.add(firstNameField, lastNameField, passwordField, emailField, monthlyIncomeField);
|
formLayout.add(firstName, lastName, password, email, monthlyIncome);
|
||||||
|
|
||||||
Button saveButton = new Button("Save", click -> saveUserData());
|
add(formLayout, this.save);
|
||||||
|
|
||||||
add(formLayout, saveButton);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveUserData() {
|
private void saveUserData() {
|
||||||
final var loggedInUser = userService.getLoggedInUser().orElseThrow(() -> new UsernameNotFoundException("User not found"));
|
|
||||||
|
|
||||||
loggedInUser.setFirstName(firstNameField.getValue());
|
loggedInUser.setFirstName(firstName.getValue());
|
||||||
loggedInUser.setLastName(lastNameField.getValue());
|
loggedInUser.setLastName(lastName.getValue());
|
||||||
loggedInUser.setEmail(emailField.getValue());
|
loggedInUser.setEmail(email.getValue());
|
||||||
|
loggedInUser.setPassword(password.getValue());
|
||||||
// Only update the password if it's not empty
|
|
||||||
if (!passwordField.isEmpty()) {
|
|
||||||
loggedInUser.setPassword(passwordField.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: implement
|
// TODO: implement
|
||||||
String monthlyIncome = monthlyIncomeField.getValue();
|
String monthlyIncome = this.monthlyIncome.getValue();
|
||||||
|
|
||||||
userService.updateUserAndConnectedPerson(loggedInUser);
|
userService.updateUserAndConnectedPerson(loggedInUser);
|
||||||
Notification.show("User details updated successfully");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beforeEnter(BeforeEnterEvent event) {
|
public void beforeEnter(BeforeEnterEvent event) {
|
||||||
final var loggedInUser = userService.getLoggedInUser().orElseThrow(() -> new UsernameNotFoundException("User not found"));
|
final var loggedInUser = userService.getLoggedInUser().orElseThrow(() -> new UsernameNotFoundException("User not found"));
|
||||||
firstNameField.setValue(loggedInUser.getFirstName());
|
firstName.setValue(loggedInUser.getFirstName());
|
||||||
lastNameField.setValue(loggedInUser.getLastName());
|
lastName.setValue(loggedInUser.getLastName());
|
||||||
monthlyIncomeField.setValue("");
|
password.setValue(loggedInUser.getPassword());
|
||||||
|
email.setValue(loggedInUser.getEmail());
|
||||||
|
monthlyIncome.setValue("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue