Erweiterungen
This commit is contained in:
@@ -3,8 +3,6 @@ package de.assecutor.votianlt.pages.view;
|
||||
import com.vaadin.flow.component.UI;
|
||||
import com.vaadin.flow.component.button.Button;
|
||||
import com.vaadin.flow.component.button.ButtonVariant;
|
||||
import com.vaadin.flow.component.combobox.ComboBox;
|
||||
import com.vaadin.flow.component.dialog.Dialog;
|
||||
import com.vaadin.flow.component.grid.Grid;
|
||||
import com.vaadin.flow.component.html.H2;
|
||||
import com.vaadin.flow.component.html.Main;
|
||||
@@ -14,25 +12,20 @@ import com.vaadin.flow.component.notification.Notification;
|
||||
import com.vaadin.flow.component.notification.NotificationVariant;
|
||||
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
|
||||
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
|
||||
import com.vaadin.flow.component.textfield.TextArea;
|
||||
import com.vaadin.flow.component.textfield.TextField;
|
||||
import com.vaadin.flow.data.renderer.ComponentRenderer;
|
||||
import com.vaadin.flow.router.Menu;
|
||||
import com.vaadin.flow.router.PageTitle;
|
||||
import com.vaadin.flow.router.Route;
|
||||
import de.assecutor.votianlt.dto.ClientMessageSummary;
|
||||
import de.assecutor.votianlt.model.AppUser;
|
||||
import de.assecutor.votianlt.model.Job;
|
||||
import de.assecutor.votianlt.model.Message;
|
||||
import de.assecutor.votianlt.model.MessageDirection;
|
||||
import de.assecutor.votianlt.model.User;
|
||||
import de.assecutor.votianlt.pages.service.AppUserService;
|
||||
import de.assecutor.votianlt.repository.JobRepository;
|
||||
import de.assecutor.votianlt.security.SecurityService;
|
||||
import de.assecutor.votianlt.service.MessageService;
|
||||
import jakarta.annotation.security.RolesAllowed;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bson.types.ObjectId;
|
||||
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
@@ -83,16 +76,12 @@ public class MessagesView extends Main {
|
||||
}
|
||||
|
||||
private HorizontalLayout createHeaderLayout() {
|
||||
H2 title = new H2("Nachrichten nach Client");
|
||||
|
||||
Button sendMessageButton = new Button("Nachricht senden", VaadinIcon.ENVELOPE_O.create());
|
||||
sendMessageButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
|
||||
sendMessageButton.addClickListener(e -> openSendMessageDialog());
|
||||
H2 title = new H2("Nachrichten");
|
||||
|
||||
Button refreshButton = new Button("Aktualisieren", VaadinIcon.REFRESH.create());
|
||||
refreshButton.addClickListener(e -> loadClientSummaries());
|
||||
|
||||
HorizontalLayout layout = new HorizontalLayout(title, sendMessageButton, refreshButton);
|
||||
HorizontalLayout layout = new HorizontalLayout(title, refreshButton);
|
||||
layout.setWidthFull();
|
||||
layout.setAlignItems(com.vaadin.flow.component.orderedlayout.FlexComponent.Alignment.CENTER);
|
||||
layout.expand(title);
|
||||
@@ -313,85 +302,4 @@ public class MessagesView extends Main {
|
||||
return summaries;
|
||||
}
|
||||
|
||||
private void openSendMessageDialog() {
|
||||
Dialog dialog = new Dialog();
|
||||
dialog.setHeaderTitle("Neue Nachricht senden");
|
||||
dialog.setWidth("600px");
|
||||
|
||||
VerticalLayout layout = new VerticalLayout();
|
||||
layout.setPadding(false);
|
||||
layout.setSpacing(true);
|
||||
|
||||
// Receiver selection
|
||||
ComboBox<AppUser> receiverCombo = new ComboBox<>("Empfänger (App-Benutzer)");
|
||||
receiverCombo.setWidthFull();
|
||||
receiverCombo.setItems(appUserService.findAll());
|
||||
receiverCombo.setItemLabelGenerator(appUser ->
|
||||
appUser.getVorname() + " " + appUser.getNachname() + " (" + appUser.getEmail() + ")"
|
||||
);
|
||||
|
||||
// Job selection (optional)
|
||||
ComboBox<Job> jobCombo = new ComboBox<>("Auftrag (optional)");
|
||||
jobCombo.setWidthFull();
|
||||
jobCombo.setItems(jobRepository.findAll());
|
||||
jobCombo.setItemLabelGenerator(job ->
|
||||
job.getJobNumber() + " - " + (job.getPickupCity() != null ? job.getPickupCity() : "")
|
||||
);
|
||||
|
||||
// Message content
|
||||
TextArea contentArea = new TextArea("Nachricht");
|
||||
contentArea.setWidthFull();
|
||||
contentArea.setHeight("200px");
|
||||
|
||||
layout.add(receiverCombo, jobCombo, contentArea);
|
||||
|
||||
// Buttons
|
||||
Button sendButton = new Button("Senden", e -> {
|
||||
if (receiverCombo.getValue() == null || contentArea.getValue().isBlank()) {
|
||||
Notification.show("Bitte Empfänger und Nachricht eingeben", 3000, Notification.Position.MIDDLE)
|
||||
.addThemeVariants(NotificationVariant.LUMO_ERROR);
|
||||
return;
|
||||
}
|
||||
|
||||
sendMessage(receiverCombo.getValue(), jobCombo.getValue(), contentArea.getValue());
|
||||
dialog.close();
|
||||
});
|
||||
sendButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
|
||||
|
||||
Button cancelButton = new Button("Abbrechen", e -> dialog.close());
|
||||
|
||||
dialog.getFooter().add(cancelButton, sendButton);
|
||||
dialog.add(layout);
|
||||
dialog.open();
|
||||
}
|
||||
|
||||
private void sendMessage(AppUser receiver, Job job, String content) {
|
||||
try {
|
||||
User currentUser = securityService.getCurrentDatabaseUser();
|
||||
if (currentUser == null) {
|
||||
Notification.show("Benutzer nicht gefunden", 3000, Notification.Position.MIDDLE)
|
||||
.addThemeVariants(NotificationVariant.LUMO_ERROR);
|
||||
return;
|
||||
}
|
||||
|
||||
String sender = currentUser.getEmail();
|
||||
String receiverUsername = receiver.getEmail(); // Using email as username for app users
|
||||
|
||||
if (job != null) {
|
||||
messageService.sendJobMessageToClient(content, sender, receiverUsername,
|
||||
job.getId(), job.getJobNumber());
|
||||
} else {
|
||||
messageService.sendGeneralMessageToClient(content, sender, receiverUsername);
|
||||
}
|
||||
|
||||
Notification.show("Nachricht erfolgreich gesendet", 2000, Notification.Position.BOTTOM_START)
|
||||
.addThemeVariants(NotificationVariant.LUMO_SUCCESS);
|
||||
loadClientSummaries();
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Error sending message: {}", e.getMessage(), e);
|
||||
Notification.show("Fehler beim Senden der Nachricht", 3000, Notification.Position.MIDDLE)
|
||||
.addThemeVariants(NotificationVariant.LUMO_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user