Skip to content

Commit

Permalink
add search
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Gotsman committed Jun 1, 2024
1 parent 33097a1 commit 5e24f8e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
19 changes: 16 additions & 3 deletions customer_data_app/customer_data_app/backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sqlmodel import select, asc, desc, or_, func
from datetime import datetime, timedelta

LiteralStatus = Literal["Delivered", "Pending", "Cancelled"]
#LiteralStatus = Literal["Delivered", "Pending", "Cancelled"]


def _get_percentage_change(value: Union[int, float], prev_value: Union[int, float]) -> float:
Expand Down Expand Up @@ -44,6 +44,7 @@ class State(rx.State):
users: list[Customer] = []
sort_value: str = ""
sort_reverse: bool = False
search_value: str = ""
current_user: Customer = Customer()
# Values for current and previous month
current_month_values: MonthValues = MonthValues()
Expand All @@ -54,6 +55,17 @@ def load_entries(self) -> list[Customer]:
"""Get all users from the database."""
with rx.session() as session:
query = select(Customer)
if self.search_value:
search_value = f"%{str(self.search_value).lower()}%"
query = query.where(
or_(
*[
getattr(Customer, field).ilike(search_value)
for field in Customer.get_fields()
],
)
)

if self.sort_value:
sort_column = getattr(Customer, self.sort_value)
if self.sort_value == "payments":
Expand All @@ -62,7 +74,6 @@ def load_entries(self) -> list[Customer]:
order = desc(func.lower(sort_column)) if self.sort_reverse else asc(func.lower(sort_column))
query = query.order_by(order)

###### come back to add search functioanloty later
self.users = session.exec(query).all()

self.get_current_month_values()
Expand Down Expand Up @@ -111,6 +122,9 @@ def toggle_sort(self):
self.sort_reverse = not self.sort_reverse
self.load_entries()

def filter_values(self, search_value):
self.search_value = search_value
self.load_entries()

def get_user(self, user: Customer):
self.current_user = user
Expand All @@ -133,7 +147,6 @@ def add_customer_to_db(self, form_data: dict):

def update_customer_to_db(self, form_data: dict):
self.current_user.update(form_data)
print(self.current_user)
with rx.session() as session:
customer = session.exec(
select(Customer).where(Customer.id == self.current_user["id"])
Expand Down
5 changes: 5 additions & 0 deletions customer_data_app/customer_data_app/views/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ def main_table():
size="3",
on_change=lambda sort_value: State.sort_values(sort_value),
),
rx.input(
placeholder="Search here...",
size="3",
on_change=lambda value: State.filter_values(value),
),
spacing="3",
align="center",
),
Expand Down

0 comments on commit 5e24f8e

Please sign in to comment.