diff --git a/src/main/java/pl/ateam/disasteralerts/message/ToastMessage.java b/src/main/java/pl/ateam/disasteralerts/message/ToastMessage.java new file mode 100644 index 0000000..b5b6288 --- /dev/null +++ b/src/main/java/pl/ateam/disasteralerts/message/ToastMessage.java @@ -0,0 +1,18 @@ +package pl.ateam.disasteralerts.message; + +import jakarta.validation.constraints.NotNull; +import lombok.Builder; +import lombok.Getter; + +@Builder +@Getter +class ToastMessage { + + @NotNull + private ToastMessageType type; + private String icon; + @NotNull + private String header; + @NotNull + private String message; +} diff --git a/src/main/java/pl/ateam/disasteralerts/message/ToastMessageFacade.java b/src/main/java/pl/ateam/disasteralerts/message/ToastMessageFacade.java new file mode 100644 index 0000000..83b74a4 --- /dev/null +++ b/src/main/java/pl/ateam/disasteralerts/message/ToastMessageFacade.java @@ -0,0 +1,11 @@ +package pl.ateam.disasteralerts.message; + +import org.springframework.stereotype.Component; +import pl.ateam.disasteralerts.message.dto.ToastMessageDTO; + +@Component +public interface ToastMessageFacade { + + ToastMessageDTO buildMessage(ToastMessageType type, String header, String message); + ToastMessageDTO buildMessage(ToastMessageType type, String icon, String header, String message); +} diff --git a/src/main/java/pl/ateam/disasteralerts/message/ToastMessageImpl.java b/src/main/java/pl/ateam/disasteralerts/message/ToastMessageImpl.java new file mode 100644 index 0000000..6059411 --- /dev/null +++ b/src/main/java/pl/ateam/disasteralerts/message/ToastMessageImpl.java @@ -0,0 +1,40 @@ +package pl.ateam.disasteralerts.message; + +import org.springframework.stereotype.Service; +import pl.ateam.disasteralerts.message.dto.ToastMessageDTO; + +@Service +class ToastMessageImpl implements ToastMessageFacade { + + private final ToastMessageMapper toastMessageMapper; + + public ToastMessageImpl(ToastMessageMapper toastMessageMapper) { + this.toastMessageMapper = toastMessageMapper; + } + + @Override + public ToastMessageDTO buildMessage(ToastMessageType type, String header, String message) { + String icon = getDefaultIcon(type); + return buildMessage(type, icon, header, message); + } + + @Override + public ToastMessageDTO buildMessage(ToastMessageType type, String icon, String header, String message) { + ToastMessage built = ToastMessage.builder() + .type(type) + .icon(icon) + .header(header) + .message(message) + .build(); + return toastMessageMapper.mapToastMessageToToastMessageDTO(built); + } + + private String getDefaultIcon(ToastMessageType type) { + String icon = "bi bi-balloon-heart-fill"; + switch (type) { + case DANGER -> icon = "bi bi-exclamation-circle-fill"; + case SUCCESS -> icon = "bi bi-check2-square"; + } + return icon; + } +} diff --git a/src/main/java/pl/ateam/disasteralerts/message/ToastMessageMapper.java b/src/main/java/pl/ateam/disasteralerts/message/ToastMessageMapper.java new file mode 100644 index 0000000..db4ac51 --- /dev/null +++ b/src/main/java/pl/ateam/disasteralerts/message/ToastMessageMapper.java @@ -0,0 +1,9 @@ +package pl.ateam.disasteralerts.message; + +import org.mapstruct.Mapper; +import pl.ateam.disasteralerts.message.dto.ToastMessageDTO; + +@Mapper(componentModel = "spring") +interface ToastMessageMapper { + ToastMessageDTO mapToastMessageToToastMessageDTO(ToastMessage toastMessage); +} diff --git a/src/main/java/pl/ateam/disasteralerts/message/ToastMessageType.java b/src/main/java/pl/ateam/disasteralerts/message/ToastMessageType.java new file mode 100644 index 0000000..713470f --- /dev/null +++ b/src/main/java/pl/ateam/disasteralerts/message/ToastMessageType.java @@ -0,0 +1,5 @@ +package pl.ateam.disasteralerts.message; + +public enum ToastMessageType { + DANGER, INFO, SUCCESS +} diff --git a/src/main/java/pl/ateam/disasteralerts/message/dto/ToastMessageDTO.java b/src/main/java/pl/ateam/disasteralerts/message/dto/ToastMessageDTO.java new file mode 100644 index 0000000..039b5c0 --- /dev/null +++ b/src/main/java/pl/ateam/disasteralerts/message/dto/ToastMessageDTO.java @@ -0,0 +1,7 @@ +package pl.ateam.disasteralerts.message.dto; + +public record ToastMessageDTO(String type, + String icon, + String header, + String message) { +} diff --git a/src/main/java/pl/ateam/disasteralerts/web/DisasterViewController.java b/src/main/java/pl/ateam/disasteralerts/web/DisasterViewController.java index aa31d22..7754b6c 100644 --- a/src/main/java/pl/ateam/disasteralerts/web/DisasterViewController.java +++ b/src/main/java/pl/ateam/disasteralerts/web/DisasterViewController.java @@ -7,15 +7,21 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import pl.ateam.disasteralerts.disaster.DisasterFacade; -import pl.ateam.disasteralerts.util.enums.DisasterStatus; -import pl.ateam.disasteralerts.util.enums.DisasterType; import pl.ateam.disasteralerts.disaster.dto.DisasterAddDTO; import pl.ateam.disasteralerts.disaster.dto.DisasterDTO; +import pl.ateam.disasteralerts.message.ToastMessageFacade; +import pl.ateam.disasteralerts.message.ToastMessageType; import pl.ateam.disasteralerts.security.AppUser; import pl.ateam.disasteralerts.util.CitiesInPoland; +import pl.ateam.disasteralerts.util.enums.DisasterStatus; +import pl.ateam.disasteralerts.util.enums.DisasterType; import java.util.List; import java.util.Map; @@ -26,6 +32,7 @@ @RequestMapping("/disasters") public class DisasterViewController { + private final ToastMessageFacade toastMessageFacade; @Value("${google.maps.api.key}") private String googleApiKey; @@ -62,14 +69,21 @@ public String createDisaster(Model model, @AuthenticationPrincipal AppUser userD DisasterDTO disasterDTO = disasterAlertFacade.createDisaster(disasterAddDTO, USER_AS_DISASTER_SOURCE); if (DisasterStatus.FAKE.equals(disasterDTO.status())) { + String message = String.format(""" - Zdarzenie zostało uznane za fałszywe. - Jeśli chcesz je aktywować skontatuj się z adminiastratorem i podaj id zgłoszenia %s. + Jeśli chcesz je aktywować skontatuj się z adminiastratorem i podaj id zgłoszenia %s """, disasterDTO.id()); + redirectAttributes.addFlashAttribute("messageStatus", DisasterStatus.FAKE.toString()); - redirectAttributes.addFlashAttribute("message", message); + redirectAttributes.addFlashAttribute("message", toastMessageFacade.buildMessage( + ToastMessageType.DANGER, + "Zdarzenie zostało uznane za fałszywe", + message)); } else { - redirectAttributes.addFlashAttribute("message", "Dodano zdarzenie"); + redirectAttributes.addFlashAttribute("message", toastMessageFacade.buildMessage( + ToastMessageType.SUCCESS, + "Dodano zdarzenie", + "Inni użytkownicy zostaną powiadomieni")); } return "redirect:/disasters/add"; diff --git a/src/main/java/pl/ateam/disasteralerts/web/SignupViewController.java b/src/main/java/pl/ateam/disasteralerts/web/SignupViewController.java index 801e88d..05a4e3e 100644 --- a/src/main/java/pl/ateam/disasteralerts/web/SignupViewController.java +++ b/src/main/java/pl/ateam/disasteralerts/web/SignupViewController.java @@ -11,6 +11,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.mvc.support.RedirectAttributes; +import pl.ateam.disasteralerts.message.ToastMessageFacade; +import pl.ateam.disasteralerts.message.ToastMessageType; import pl.ateam.disasteralerts.user.UserService; import pl.ateam.disasteralerts.user.dto.UserRegisterDTO; import pl.ateam.disasteralerts.util.CitiesInPoland; @@ -19,13 +21,16 @@ @RequestMapping("/signup") public class SignupViewController { + private final ToastMessageFacade toastMessageFacade; + @Value("${google.maps.api.key}") private String googleApiKey; private final UserService userService; - public SignupViewController(UserService userService) { + public SignupViewController(UserService userService, ToastMessageFacade toastMessageFacade) { this.userService = userService; + this.toastMessageFacade = toastMessageFacade; } @GetMapping @@ -52,14 +57,17 @@ public String signupView(@RequestParam(value = "confirmPassword") String confirm return "signup"; } - if(!confirmPassword.contains(userDto.password())) { + if (!confirmPassword.contains(userDto.password())) { model.addAttribute("confirmPasswordError", "Hasła są różne"); return "signup"; } userService.save(userDto); - redirectAttributes.addFlashAttribute("message", "Konto zostało utworzone. Zaloguj się."); + redirectAttributes.addFlashAttribute("message", toastMessageFacade.buildMessage( + ToastMessageType.SUCCESS, + "Konto utworzone", + "Konto zostało utworzone. Zaloguj się.")); return "redirect:/"; } diff --git a/src/main/java/pl/ateam/disasteralerts/web/UserUpdateViewController.java b/src/main/java/pl/ateam/disasteralerts/web/UserUpdateViewController.java index 6203363..f5c3d59 100644 --- a/src/main/java/pl/ateam/disasteralerts/web/UserUpdateViewController.java +++ b/src/main/java/pl/ateam/disasteralerts/web/UserUpdateViewController.java @@ -12,6 +12,8 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.mvc.support.RedirectAttributes; +import pl.ateam.disasteralerts.message.ToastMessageFacade; +import pl.ateam.disasteralerts.message.ToastMessageType; import pl.ateam.disasteralerts.security.AppUser; import pl.ateam.disasteralerts.user.UserFacade; import pl.ateam.disasteralerts.user.dto.UserUpdateDTO; @@ -22,6 +24,7 @@ @RequiredArgsConstructor public class UserUpdateViewController { + private final ToastMessageFacade toastMessageFacade; @Value("${google.maps.api.key}") private String googleApiKey; @@ -48,7 +51,10 @@ public String updateUser(@AuthenticationPrincipal AppUser appUser, } userFacade.updateUser(userUpdateDto, appUser.getUserDTO().id()); - redirectAttributes.addFlashAttribute("message", "Dane użytkownika zostały pomyślnie zaktualizowane."); + redirectAttributes.addFlashAttribute("message", toastMessageFacade.buildMessage( + ToastMessageType.SUCCESS, + "Aktualizacja", + "Dane użytkownika zostały pomyślnie zaktualizowane.")); return "redirect:/disasters/add"; } @@ -57,7 +63,9 @@ public String removePhoneNumber(@AuthenticationPrincipal AppUser appUser, RedirectAttributes redirectAttributes) { userFacade.removePhoneNumber(appUser.getUsername()); - redirectAttributes.addFlashAttribute("message", "Numer telefonu został usunięty"); + redirectAttributes.addFlashAttribute("message", toastMessageFacade.buildMessage( + ToastMessageType.SUCCESS, + "Aktualizacja", "Numer telefonu został usunięty")); return "redirect:/users/edit"; } diff --git a/src/main/resources/templates/fragments.html b/src/main/resources/templates/fragments.html index 6c96890..0c00074 100644 --- a/src/main/resources/templates/fragments.html +++ b/src/main/resources/templates/fragments.html @@ -67,14 +67,13 @@