feat: expenses name filtering
This commit is contained in:
parent
770da8b805
commit
86671693d2
2 changed files with 18 additions and 17 deletions
|
@ -4,9 +4,13 @@ import com.application.munera.data.BadgeMessage;
|
|||
import com.application.munera.data.Expense;
|
||||
import com.application.munera.data.ExpenseType;
|
||||
import com.vaadin.flow.component.html.Span;
|
||||
import com.vaadin.flow.component.textfield.TextField;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.vaadin.klaudeta.PaginatedGrid;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Service
|
||||
public class ViewsService {
|
||||
|
@ -43,6 +47,19 @@ public class ViewsService {
|
|||
return badge;
|
||||
}
|
||||
|
||||
public void applyFilter(TextField nameFilter, Long userId, PaginatedGrid<Expense, Objects> grid) {
|
||||
String filterValue = nameFilter.getValue().trim();
|
||||
List<Expense> filteredExpenses;
|
||||
if (filterValue.isEmpty()) filteredExpenses = expenseService.findAllOrderByDateDescending(userId); // If the filter is empty, return all expenses
|
||||
else {
|
||||
// Apply the filter (e.g., by name)
|
||||
filteredExpenses = expenseService.findAllOrderByDateDescending(userId)
|
||||
.stream()
|
||||
.filter(expense -> expense.getName().toLowerCase().contains(filterValue.toLowerCase())).toList();
|
||||
}
|
||||
grid.setItems(filteredExpenses);
|
||||
}
|
||||
|
||||
private BadgeMessage determineBadgeMessage(ExpenseType type, boolean isPaid) {
|
||||
return switch (type) {
|
||||
case CREDIT -> isPaid ? BadgeMessage.PAID_TO_ME : BadgeMessage.OWED_TO_ME;
|
||||
|
|
|
@ -41,7 +41,6 @@ import org.springframework.orm.ObjectOptimisticLockingFailureException;
|
|||
import org.vaadin.klaudeta.PaginatedGrid;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
|
@ -119,7 +118,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
|
|||
nameFilter.setPlaceholder("Filter by Name...");
|
||||
nameFilter.setClearButtonVisible(true);
|
||||
nameFilter.setValueChangeMode(ValueChangeMode.LAZY);
|
||||
nameFilter.addValueChangeListener(e -> applyFilter());
|
||||
nameFilter.addValueChangeListener(e -> this.viewsService.applyFilter(nameFilter, userId, grid));
|
||||
|
||||
// Add nameFilter field to layout (above the grid)
|
||||
VerticalLayout layout = new VerticalLayout();
|
||||
|
@ -333,19 +332,4 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
|
|||
payer.setValue(loggedInPerson);
|
||||
beneficiary.setValue(loggedInPerson);
|
||||
}
|
||||
private void applyFilter() {
|
||||
String filterValue = nameFilter.getValue().trim();
|
||||
List<Expense> filteredExpenses;
|
||||
|
||||
if (filterValue.isEmpty()) {
|
||||
// If the filter is empty, return all expenses
|
||||
filteredExpenses = expenseService.findAllOrderByDateDescending(userId);
|
||||
} else {
|
||||
filteredExpenses = expenseService.findAllOrderByDateDescending(userId)
|
||||
.stream()
|
||||
.filter(expense1 -> expense1.getName().toLowerCase().contains(filterValue.toLowerCase()))
|
||||
.toList();
|
||||
}
|
||||
grid.setItems(filteredExpenses);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue