Skip to content

Commit

Permalink
Merge pull request #70 from jkuznik/Task-55-create-toast-builder
Browse files Browse the repository at this point in the history
Task-55-create-toast-builder
  • Loading branch information
Slawek84PL authored Dec 10, 2024
2 parents d563af8 + 5bf5c14 commit 8362ddc
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 17 deletions.
18 changes: 18 additions & 0 deletions src/main/java/pl/ateam/disasteralerts/message/ToastMessage.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pl.ateam.disasteralerts.message;

public enum ToastMessageType {
DANGER, INFO, SUCCESS
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package pl.ateam.disasteralerts.message.dto;

public record ToastMessageDTO(String type,
String icon,
String header,
String message) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +32,7 @@
@RequestMapping("/disasters")
public class DisasterViewController {

private final ToastMessageFacade toastMessageFacade;
@Value("${google.maps.api.key}")
private String googleApiKey;

Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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:/";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +24,7 @@
@RequiredArgsConstructor
public class UserUpdateViewController {

private final ToastMessageFacade toastMessageFacade;
@Value("${google.maps.api.key}")
private String googleApiKey;

Expand All @@ -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";
}

Expand All @@ -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";
}

Expand Down
9 changes: 4 additions & 5 deletions src/main/resources/templates/fragments.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,13 @@

<div th:fragment="toast" class="toast rounded align-items-center border-0 position-fixed bottom-0 end-0 m-3 show"
role="alert" aria-live="assertive" aria-atomic="true" th:if="${message}">
<div class="toast-header"
th:classappend="${messageStatus != 'FAKE'} ? ' text-primary' : ' text-danger'">
<i th:class="${messageStatus != 'FAKE'} ? 'bi bi-balloon-heart-fill' : 'bi bi-exclamation-circle-fill'"></i>
<strong class="me-auto">Dodano zgłoszenie</strong>
<div class="toast-header" th:classappend="' text-' + ${message.type.toLowerCase()}">
<i th:class="${message.icon}"></i>
<strong class="me-auto" th:text="${message.header}">Dodano zgłoszenie</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="d-flex text-secondary bg-light px-3 py-1 fw-light">
<div th:replace="~{fragments :: message}" class="toast-body text-white">
<div th:text="${message.message}" class="toast-body">
</div>
</div>
</div>
Expand Down

0 comments on commit 8362ddc

Please sign in to comment.