Skip to content

Commit 8dca1c9

Browse files
26 - Listing Stored Database Entries
1 parent 7d21558 commit 8dca1c9

File tree

5 files changed

+43
-11
lines changed

5 files changed

+43
-11
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from .form import contact_form
22
from .state import ContactState
3-
from .page import contact_page
3+
from .page import contact_page, contact_entries_list_page
44

55
__all__ = [
66
'contact_form',
77
'ContactState',
8-
'contact_page'
8+
'contact_page',
9+
'contact_entries_list_page'
910
]

full_stack_python/contact/page.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,29 @@
33
from .. import navigation
44
from ..ui.base import base_page
55

6-
from . import form, state
6+
from . import form, state, model
7+
8+
def contact_entry_list_item(contact: model.ContactEntryModel):
9+
return rx.box(
10+
rx.heading(contact.first_name),
11+
rx.text(contact.message),
12+
padding='1em'
13+
)
14+
15+
# def foreach_callback(text):
16+
# return rx.box(rx.text(text))
17+
18+
def contact_entries_list_page() ->rx.Component:
19+
return base_page(
20+
rx.vstack(
21+
rx.heading("Contact Entries", size="5"),
22+
# rx.foreach(["abc", "abc", "cde"], foreach_callback),
23+
rx.foreach(state.ContactState.entries, contact_entry_list_item),
24+
spacing="5",
25+
align="center",
26+
min_height="85vh",
27+
)
28+
)
729

830
def contact_page() -> rx.Component:
931
my_child = rx.vstack(

full_stack_python/contact/state.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1+
from typing import List
12
import asyncio
23
import reflex as rx
34

5+
from sqlmodel import select
46
from .model import ContactEntryModel
57

68
class ContactState(rx.State):
79
form_data: dict = {}
10+
entries: List['ContactEntryModel'] = []
811
did_submit: bool = False
9-
timeleft: int = 5
10-
11-
@rx.var
12-
def timeleft_label(self):
13-
if self.timeleft < 1:
14-
return "No time left"
15-
return f"{self.timeleft} seconds"
1612

1713
@rx.var
1814
def thank_you(self):
@@ -38,4 +34,11 @@ async def handle_submit(self, form_data: dict):
3834
yield
3935
await asyncio.sleep(2)
4036
self.did_submit = False
41-
yield
37+
yield
38+
39+
def list_entries(self):
40+
with rx.session() as session:
41+
entries = session.exec(
42+
select(ContactEntryModel)
43+
).all()
44+
self.entries = entries

full_stack_python/full_stack_python.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,10 @@ def index() -> rx.Component:
5050
route=navigation.routes.ABOUT_US_ROUTE)
5151
app.add_page(contact.contact_page,
5252
route=navigation.routes.CONTACT_US_ROUTE)
53+
app.add_page(
54+
contact.contact_entries_list_page,
55+
route=navigation.routes.CONTACT_ENTRIES_ROUTE,
56+
on_load=contact.ContactState.list_entries
57+
)
5358
app.add_page(pages.pricing_page,
5459
route=navigation.routes.PRICING_ROUTE)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
HOME_ROUTE="/"
22
ABOUT_US_ROUTE="/about"
33
CONTACT_US_ROUTE="/contact"
4+
CONTACT_ENTRIES_ROUTE="/contact/entries"
45
PRICING_ROUTE="/pricing"

0 commit comments

Comments
 (0)