feat: improve ExpenseView labels

This commit is contained in:
filippo-ferrari 2024-09-07 17:38:18 +02:00
parent 3070ed7571
commit db4a0ad407
3 changed files with 47 additions and 10 deletions

View file

@ -38,6 +38,7 @@ public class Expense extends AbstractEntity {
@Column(name = "PeriodicExpense", nullable = false) @Column(name = "PeriodicExpense", nullable = false)
private Boolean isPeriodic; private Boolean isPeriodic;
@Enumerated(EnumType.ORDINAL)
@Column(name = "PeriodUnit") @Column(name = "PeriodUnit")
private PeriodUnit periodUnit; private PeriodUnit periodUnit;

View file

@ -0,0 +1,27 @@
package com.application.munera.views.expenses;
public enum BadgeMessage {
PAID_TO_SOMEONE("Paid to someone", "badge success"),
PAID_TO_YOU("Paid to you", "badge success"),
PAID("Paid", "badge success"),
OWED_BY_SOMEONE("Owed by someone", "badge warning"),
OWED_TO_YOU("Owed to you", "badge warning"),
NOT_PAID("Not paid", "badge warning"),
UNKNOWN("Unknown status", "badge error");
private final String text;
private final String theme;
BadgeMessage(String text, String theme) {
this.text = text;
this.theme = theme;
}
public String getText() {
return text;
}
public String getTheme() {
return theme;
}
}

View file

@ -98,7 +98,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
grid.addColumn(Expense::getDate).setHeader("Date").setSortable(true).setSortProperty("date"); grid.addColumn(Expense::getDate).setHeader("Date").setSortable(true).setSortProperty("date");
// grid.addColumn(expenseEvent -> expenseEvent.getEvent().getName()).setHeader("Event").setSortable(true); // grid.addColumn(expenseEvent -> expenseEvent.getEvent().getName()).setHeader("Event").setSortable(true);
grid.addColumn(new ComponentRenderer<>(expense1 -> createBadge(expenseService.isExpensePaid(expense1)))).setHeader("Status").setSortable(true); grid.addColumn(new ComponentRenderer<>(this::createBadge)).setHeader("Status").setSortable(true);
grid.getColumns().forEach(col -> col.setAutoWidth(true)); grid.getColumns().forEach(col -> col.setAutoWidth(true));
grid.setItems(this.expenseService.findAllOrderByDateDescending()); grid.setItems(this.expenseService.findAllOrderByDateDescending());
@ -292,15 +292,24 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
periodInterval.setVisible(isPeriodicChecked); periodInterval.setVisible(isPeriodicChecked);
} }
private Span createBadge(Boolean isExpensePaid) { private Span createBadge(final Expense expense) {
Span badge = new Span(); final var isExpensePaid = Boolean.TRUE.equals(this.expenseService.isExpensePaid(expense));
if (Boolean.TRUE.equals(isExpensePaid)) { final var badgeMessage = determineBadgeMessage(expense.getExpenseType(), isExpensePaid);
badge.setText("Paid");
badge.getElement().getThemeList().add("badge success"); final var badge = new Span();
} else { badge.setText(badgeMessage.getText());
badge.setText("Owed"); badge.getElement().getThemeList().add(badgeMessage.getTheme());
badge.getElement().getThemeList().add("badge error");
}
return badge; return badge;
} }
private BadgeMessage determineBadgeMessage(ExpenseType type, boolean isPaid) {
return switch (type) {
case CREDIT -> isPaid ? BadgeMessage.PAID_TO_SOMEONE : BadgeMessage.OWED_BY_SOMEONE;
case DEBIT -> isPaid ? BadgeMessage.PAID_TO_YOU : BadgeMessage.OWED_TO_YOU;
case NONE -> isPaid ? BadgeMessage.PAID : BadgeMessage.NOT_PAID;
default -> BadgeMessage.UNKNOWN;
};
}
} }