From 76c269080eb3787284bd60e09d44e2b9f43d9f62 Mon Sep 17 00:00:00 2001 From: filippo-ferrari Date: Sun, 8 Sep 2024 18:31:11 +0200 Subject: [PATCH] feat: SettingsView --- .../munera/services/UserService.java | 15 +++- .../application/munera/views/MainLayout.java | 6 +- .../munera/views/settings/SettingsView.java | 84 +++++++++++++++++++ 3 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/application/munera/views/settings/SettingsView.java diff --git a/src/main/java/com/application/munera/services/UserService.java b/src/main/java/com/application/munera/services/UserService.java index b7433c2..edf5862 100644 --- a/src/main/java/com/application/munera/services/UserService.java +++ b/src/main/java/com/application/munera/services/UserService.java @@ -1,6 +1,8 @@ package com.application.munera.services; +import com.application.munera.data.Person; import com.application.munera.data.User; +import com.application.munera.repositories.PersonRepository; import com.application.munera.repositories.UserRepository; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Service; @@ -11,9 +13,11 @@ import static com.application.munera.SecurityUtils.getLoggedInUserDetails; public class UserService { private final UserRepository userRepository; + private final PersonRepository personRepository; - public UserService(UserRepository userRepository) { + public UserService(UserRepository userRepository, PersonRepository personRepository) { this.userRepository = userRepository; + this.personRepository = personRepository; } public User findByUsername (String username) { @@ -33,4 +37,11 @@ public class UserService { return null; } -} + public void updateUser(User user) { + userRepository.save(user); + Person person = personRepository.findByUserId(user.getId()) + .orElseThrow(() -> new IllegalStateException("Associated Person not found")); + person.setFirstName(user.getFirstName()); + person.setLastName(user.getLastName()); + personRepository.save(person); + }} diff --git a/src/main/java/com/application/munera/views/MainLayout.java b/src/main/java/com/application/munera/views/MainLayout.java index 2f8db29..1e39091 100644 --- a/src/main/java/com/application/munera/views/MainLayout.java +++ b/src/main/java/com/application/munera/views/MainLayout.java @@ -3,8 +3,9 @@ package com.application.munera.views; import com.application.munera.views.categories.CategoriesView; import com.application.munera.views.dashboard.DashboardView; import com.application.munera.views.events.EventsView; -import com.application.munera.views.expenses.*; +import com.application.munera.views.expenses.ExpensesView; import com.application.munera.views.people.PeopleView; +import com.application.munera.views.settings.SettingsView; import com.vaadin.flow.component.applayout.AppLayout; import com.vaadin.flow.component.applayout.DrawerToggle; import com.vaadin.flow.component.button.Button; @@ -22,8 +23,6 @@ import com.vaadin.flow.spring.security.AuthenticationContext; import com.vaadin.flow.theme.lumo.LumoUtility; import org.vaadin.lineawesome.LineAwesomeIcon; -import javax.swing.*; - /** * The main view is a top-level placeholder for other views. */ @@ -94,6 +93,7 @@ public class MainLayout extends AppLayout { nav.addItem(new SideNavItem("People", PeopleView.class, LineAwesomeIcon.USER.create())); nav.addItem(new SideNavItem("Events", EventsView.class, LineAwesomeIcon.BANDCAMP.create())); nav.addItem(new SideNavItem("Dashboard", DashboardView.class, LineAwesomeIcon.CHART_LINE_SOLID.create())); + nav.addItem(new SideNavItem("Settings", SettingsView.class, LineAwesomeIcon.COG_SOLID.create())); return nav; } diff --git a/src/main/java/com/application/munera/views/settings/SettingsView.java b/src/main/java/com/application/munera/views/settings/SettingsView.java new file mode 100644 index 0000000..f91a5f5 --- /dev/null +++ b/src/main/java/com/application/munera/views/settings/SettingsView.java @@ -0,0 +1,84 @@ +package com.application.munera.views.settings; + +import com.application.munera.data.User; +import com.application.munera.services.UserService; +import com.application.munera.views.MainLayout; +import com.vaadin.flow.component.button.Button; +import com.vaadin.flow.component.dependency.Uses; +import com.vaadin.flow.component.formlayout.FormLayout; +import com.vaadin.flow.component.icon.Icon; +import com.vaadin.flow.component.notification.Notification; +import com.vaadin.flow.component.orderedlayout.VerticalLayout; +import com.vaadin.flow.component.textfield.PasswordField; +import com.vaadin.flow.component.textfield.TextField; +import com.vaadin.flow.router.BeforeEnterEvent; +import com.vaadin.flow.router.BeforeEnterObserver; +import com.vaadin.flow.router.PageTitle; +import com.vaadin.flow.router.Route; +import jakarta.annotation.security.PermitAll; +import org.springframework.beans.factory.annotation.Autowired; + +@PageTitle("Settings") +@PermitAll +@Uses(Icon.class) +@Route(value = "settings", layout = MainLayout.class) +public class SettingsView extends VerticalLayout implements BeforeEnterObserver { + + private final UserService userService; + + private TextField firstNameField; + private TextField lastNameField; + private PasswordField passwordField; + private TextField monthlyIncomeField; + + @Autowired + public SettingsView(UserService userService) { + this.userService = userService; + + createForm(); + } + + private void createForm() { + FormLayout formLayout = new FormLayout(); + + firstNameField = new TextField("First Name"); + lastNameField = new TextField("Last Name"); + passwordField = new PasswordField("Password"); + monthlyIncomeField = new TextField("Monthly Income"); + + formLayout.add(firstNameField, lastNameField, passwordField, monthlyIncomeField); + + Button saveButton = new Button("Save", click -> saveUserData()); + + add(formLayout, saveButton); + } + + private void saveUserData() { + User loggedInUser = userService.getLoggedInUser(); + + // Assuming you have methods to update user details + loggedInUser.setFirstName(firstNameField.getValue()); + loggedInUser.setLastName(lastNameField.getValue()); + + // Only update the password if it's not empty + if (!passwordField.isEmpty()) { + loggedInUser.setPassword(passwordField.getValue()); + } + + // Handle saving the monthly income separately if needed + // For now, we'll just print it out + String monthlyIncome = monthlyIncomeField.getValue(); + System.out.println("Monthly Income: " + monthlyIncome); + + userService.updateUser(loggedInUser); + Notification.show("User details updated successfully"); + } + + @Override + public void beforeEnter(BeforeEnterEvent event) { + final var loggedInUser = userService.getLoggedInUser(); + firstNameField.setValue(loggedInUser.getFirstName()); + lastNameField.setValue(loggedInUser.getLastName()); + monthlyIncomeField.setValue(""); //TODO: implement monthly income + } +}