refactor: PeopleView

This commit is contained in:
effe 2024-09-12 20:44:47 -04:00
parent 88276b8c55
commit 0bb2d06525

View file

@ -84,27 +84,33 @@ public class PeopleView extends Div implements BeforeEnterObserver {
grid.addThemeVariants(GridVariant.LUMO_NO_BORDER); grid.addThemeVariants(GridVariant.LUMO_NO_BORDER);
grid.addHierarchyColumn(this::getNodeName).setHeader("Name"); grid.addHierarchyColumn(this::getNodeName).setHeader("Name");
grid.addColumn(this::getNodeCost).setHeader("Balance").setSortable(true); grid.addColumn(this::getNodeCost).setHeader("Balance").setSortable(true);
grid.addColumn(new ComponentRenderer<>(persona -> { grid.addColumn(new ComponentRenderer<>(personEntry -> {
if (persona instanceof Person) return this.viewsService.createPersonBadge(personService.calculateNetBalance((Person) persona)); if (personEntry instanceof Person person1) return this.viewsService.createPersonBadge(personService.calculateNetBalance(person1));
else return this.viewsService.createExpenseBadge(((Expense) persona)); else return this.viewsService.createExpenseBadge(((Expense) personEntry));
})).setHeader("Balance Status"); })).setHeader("Balance Status");
grid.addColumn(new ComponentRenderer<>(persona -> { grid.addColumn(new ComponentRenderer<>(persona -> {
if (persona instanceof Person) { switch (persona) {
Button setDebtPaidButton = new Button("Set all debt as paid", event -> this.personFacade.setDebtPaid((Person) persona, grid)); case Person person1 -> {
setDebtPaidButton.addThemeVariants(ButtonVariant.LUMO_SMALL, ButtonVariant.LUMO_PRIMARY); Button setDebtPaidButton = new Button("Set all debt as paid", event -> this.personFacade.setDebtPaid(person1, grid));
return setDebtPaidButton; setDebtPaidButton.addThemeVariants(ButtonVariant.LUMO_SMALL, ButtonVariant.LUMO_PRIMARY);
} else if (persona instanceof Expense) { return setDebtPaidButton;
Button setExpensePaidButton = new Button("Set as paid", event -> this.expenseFacade.setExpensePaid((Expense) persona, grid)); }
setExpensePaidButton.addThemeVariants(ButtonVariant.LUMO_SMALL); case Expense expense -> {
if (Boolean.TRUE.equals(((Expense) persona).getIsPaid())) setExpensePaidButton.setEnabled(false); Button setExpensePaidButton = new Button("Set as paid", event -> this.expenseFacade.setExpensePaid(expense, grid));
return setExpensePaidButton; setExpensePaidButton.addThemeVariants(ButtonVariant.LUMO_SMALL);
} else return new Span(); if (Boolean.TRUE.equals((expense).getIsPaid())) setExpensePaidButton.setEnabled(false);
return setExpensePaidButton;
}
default -> {
return new Span();
}
}
})); }));
grid.addColumn(new ComponentRenderer<>(persona -> { grid.addColumn(new ComponentRenderer<>(persona -> {
if (persona instanceof Person) { if (persona instanceof Person person1) {
Button setCreditPaidButton = new Button("Set all credit as paid", event -> this.personFacade.setCreditPaid((Person) persona, grid)); Button setCreditPaidButton = new Button("Set all credit as paid", event -> this.personFacade.setCreditPaid(person1, grid));
setCreditPaidButton.addThemeVariants(ButtonVariant.LUMO_SMALL, ButtonVariant.LUMO_PRIMARY); setCreditPaidButton.addThemeVariants(ButtonVariant.LUMO_SMALL, ButtonVariant.LUMO_PRIMARY);
return setCreditPaidButton; return setCreditPaidButton;
} else return new Span(); } else return new Span();
@ -175,14 +181,14 @@ public class PeopleView extends Div implements BeforeEnterObserver {
} }
private String getNodeName(Object node) { private String getNodeName(Object node) {
if (node instanceof Person) return ((Person) node).getFirstName() + " " + ((Person) node).getLastName(); if (node instanceof Person person1) return (person1).getFirstName() + " " + (person1).getLastName();
else if (node instanceof Expense) return ((Expense) node).getName(); else if (node instanceof Expense expense1) return (expense1).getName();
return ""; return "";
} }
private String getNodeCost(Object node) { private String getNodeCost(Object node) {
if (node instanceof Person) return this.personService.calculateNetBalance((Person) node) + ""; if (node instanceof Person person1) return this.personService.calculateNetBalance(person1) + "";
else if (node instanceof Expense) return ((Expense) node).getCost().toString() + ""; else if (node instanceof Expense expense1) return (expense1).getCost().toString() + "";
return ""; return "";
} }
@ -196,9 +202,7 @@ public class PeopleView extends Div implements BeforeEnterObserver {
Notification.show( Notification.show(
String.format("The requested person was not found, ID = %s", personId.get()), 3000, String.format("The requested person was not found, ID = %s", personId.get()), 3000,
Position.BOTTOM_START); Position.BOTTOM_START);
// when a row is selected but the data is no longer available, refreshGrid(); // when a row is selected but the data is no longer available refresh grid
// refresh grid
refreshGrid();
event.forwardTo(PeopleView.class); event.forwardTo(PeopleView.class);
} }
} }
@ -207,7 +211,6 @@ public class PeopleView extends Div implements BeforeEnterObserver {
private void createEditorLayout(SplitLayout splitLayout) { private void createEditorLayout(SplitLayout splitLayout) {
Div editorLayoutDiv = new Div(); Div editorLayoutDiv = new Div();
editorLayoutDiv.setClassName("editor-layout"); editorLayoutDiv.setClassName("editor-layout");
Div editorDiv = new Div(); Div editorDiv = new Div();
editorDiv.setClassName("editor"); editorDiv.setClassName("editor");
editorLayoutDiv.add(editorDiv); editorLayoutDiv.add(editorDiv);