From 85dad4900ca30b448d4df3c1f72b9ae3258f3fea Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Fri, 18 Oct 2024 13:33:47 -0400 Subject: [PATCH] =?UTF-8?q?#55=20Perfil=20de=20Personal=20Administrativo?= =?UTF-8?q?=20-=20A=C3=B1adir=20Vacaciones=20del=20Empleado?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../views/MainLayout.java | 2 + .../views/RequestRegisterView.java | 128 ++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 src/main/java/com/primefactorsolutions/views/RequestRegisterView.java diff --git a/src/main/java/com/primefactorsolutions/views/MainLayout.java b/src/main/java/com/primefactorsolutions/views/MainLayout.java index e64ad07..b5602f5 100644 --- a/src/main/java/com/primefactorsolutions/views/MainLayout.java +++ b/src/main/java/com/primefactorsolutions/views/MainLayout.java @@ -144,6 +144,8 @@ public class MainLayout extends AppLayout { LineAwesomeIcon.PLANE_DEPARTURE_SOLID.create()); timeOff.addItem(new SideNavItem("Vacations", RequestsListView.class, LineAwesomeIcon.SUN.create())); + timeOff.addItem(new SideNavItem("Add Vacation", RequestRegisterView.class, + LineAwesomeIcon.SUN.create())); SideNavItem timesheet = new SideNavItem("My Timesheet", TimesheetView.class, LineAwesomeIcon.HOURGLASS_START_SOLID.create()); timesheet.addItem(new SideNavItem("Hours Worked", HoursWorkedView.class, diff --git a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java new file mode 100644 index 0000000..3ff6798 --- /dev/null +++ b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java @@ -0,0 +1,128 @@ +package com.primefactorsolutions.views; + +import com.primefactorsolutions.model.*; +import com.primefactorsolutions.service.EmployeeService; +import com.primefactorsolutions.service.TimeOffRequestService; +import com.vaadin.flow.component.button.Button; +import com.vaadin.flow.component.combobox.ComboBox; +import com.vaadin.flow.component.datepicker.DatePicker; +import com.vaadin.flow.component.html.H3; +import com.vaadin.flow.component.notification.Notification; +import com.vaadin.flow.component.orderedlayout.HorizontalLayout; +import com.vaadin.flow.component.orderedlayout.VerticalLayout; +import com.vaadin.flow.component.textfield.NumberField; +import com.vaadin.flow.data.binder.Binder; +import com.vaadin.flow.router.*; +import com.vaadin.flow.spring.annotation.SpringComponent; +import jakarta.annotation.security.PermitAll; +import org.springframework.context.annotation.Scope; + +import java.util.List; + +@SpringComponent +@PermitAll +@Scope("prototype") +@PageTitle("Request") +@Route(value = "/requests/new", layout = MainLayout.class) +public class RequestRegisterView extends VerticalLayout { + + private final ComboBox employeeComboBox = new ComboBox<>("Employee"); + private final ComboBox categoryComboBox = new ComboBox<>("Category"); + private final DatePicker startDatePicker = new DatePicker("Start Date"); + private final DatePicker endDatePicker = new DatePicker("End Date"); + private final NumberField availableDaysField = new NumberField("Available Days"); + private final NumberField daysToBeTakenField = new NumberField("Days To Be Taken"); + private final NumberField balanceDaysField = new NumberField("Balance Days"); + + private final TimeOffRequestService requestService; + private final EmployeeService employeeService; + private final Binder binder; + + private Button saveButton; + private Button closeButton; + + public RequestRegisterView(final TimeOffRequestService requestService, + final EmployeeService employeeService) { + this.requestService = requestService; + this.employeeService = employeeService; + this.binder = new Binder<>(TimeOffRequest.class); + + configureFormFields(); + configureButtons(); + configureBinder(); + setupFormLayout(); + } + + private void configureFormFields() { + List employees = employeeService.findAllEmployees(); + employeeComboBox.setItems(employees); + employeeComboBox.setItemLabelGenerator(emp -> emp.getFirstName() + " " + emp.getLastName()); + categoryComboBox.setItems(TimeOffRequestType.values()); + } + + private void configureButtons() { + saveButton = new Button("Save", event -> saveRequest()); + closeButton = new Button("Close", event -> closeForm()); + } + + private void configureBinder() { + binder.forField(employeeComboBox) + .asRequired("Employee is required") + .bind(TimeOffRequest::getEmployee, TimeOffRequest::setEmployee); + + binder.forField(categoryComboBox) + .asRequired("Category is required") + .bind(TimeOffRequest::getCategory, TimeOffRequest::setCategory); + + binder.forField(startDatePicker) + .asRequired("Start date is required") + .bind(TimeOffRequest::getStartDate, TimeOffRequest::setStartDate); + + binder.forField(endDatePicker) + .asRequired("End date is required") + .bind(TimeOffRequest::getEndDate, TimeOffRequest::setEndDate); + + binder.forField(availableDaysField) + .asRequired("Available days is required") + .bind(TimeOffRequest::getAvailableDays, TimeOffRequest::setAvailableDays); + + binder.forField(daysToBeTakenField) + .asRequired("Days to be taken is required") + .bind(TimeOffRequest::getDaysToBeTake, TimeOffRequest::setDaysToBeTake); + + binder.forField(balanceDaysField) + .asRequired("Balance days is required") + .bind(TimeOffRequest::getDaysBalance, TimeOffRequest::setDaysBalance); + + binder.setBean(new TimeOffRequest()); + } + + private void setupFormLayout() { + add( + new H3("Add Vacation Request"), + employeeComboBox, + categoryComboBox, + startDatePicker, + endDatePicker, + availableDaysField, + daysToBeTakenField, + balanceDaysField, + new HorizontalLayout(saveButton, closeButton) + ); + } + + private void saveRequest() { + if (binder.validate().isOk()) { + TimeOffRequest request = binder.getBean(); + requestService.saveTimeOffRequest(request); + Notification.show("Request saved successfully."); + closeForm(); + } else { + Notification.show("Please fill all required fields correctly."); + } + } + + private void closeForm() { + getUI().ifPresent(ui -> ui.navigate(RequestsListView.class)); + } +}