Skip to content

Commit 076af31

Browse files
56 - Limited Data Results on Dashboard and Landing Page
1 parent 05838cb commit 076af31

File tree

8 files changed

+67
-51
lines changed

8 files changed

+67
-51
lines changed

full_stack_python/articles/list.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from ..models import BlogPostModel
66
from . import state
77

8-
def article_link(post: BlogPostModel):
8+
def article_card_link(post: BlogPostModel):
99
post_id = post.id
1010
if post_id is None:
1111
return rx.fragment("Not found")
@@ -24,20 +24,19 @@ def article_link(post: BlogPostModel):
2424
as_child=True,
2525
)
2626

27-
def article_list_item(post: BlogPostModel):
28-
return rx.box(
29-
article_link(post),
30-
padding='1em'
27+
def article_public_list_component(columns:int=3, spacing:int=5, limit:int=100) -> rx.Component:
28+
return rx.grid(
29+
rx.foreach(state.ArticlePublicState.posts, article_card_link),
30+
columns=f'{columns}',
31+
spacing=f'{spacing}',
32+
on_mount=lambda: state.ArticlePublicState.set_limit_and_reload(limit)
3133
)
3234

33-
3435
def article_public_list_page() ->rx.Component:
3536
return base_page(
36-
rx.vstack(
37+
rx.box(
3738
rx.heading("Published Articles", size="5"),
38-
rx.foreach(state.ArticlePublicState.posts, article_list_item),
39-
spacing="5",
40-
align="center",
39+
article_public_list_component(),
4140
min_height="85vh",
4241
)
4342
)

full_stack_python/articles/state.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ArticlePublicState(SessionState):
1818
post: Optional['BlogPostModel'] = None
1919
post_content: str = ""
2020
post_publish_active: bool = False
21+
limit: int = 20
2122

2223
@rx.var
2324
def post_id(self):
@@ -53,6 +54,11 @@ def get_post_detail(self):
5354
self.post_publish_active = self.post.publish_active
5455
# return
5556

57+
def set_limit_and_reload(self, new_limit: int=5):
58+
self.limit = new_limit
59+
self.load_posts()
60+
yield
61+
5662
def load_posts(self, *args, **kwargs):
5763
lookup_args = (
5864
(BlogPostModel.publish_active == True) &
@@ -62,7 +68,7 @@ def load_posts(self, *args, **kwargs):
6268
result = session.exec(
6369
select(BlogPostModel).options(
6470
sqlalchemy.orm.joinedload(BlogPostModel.userinfo)
65-
).where(lookup_args)
71+
).where(lookup_args).limit(self.limit)
6672
).all()
6773
self.posts = result
6874

full_stack_python/blog/list.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import reflex as rx
2-
2+
import reflex_local_auth
33
from .. import navigation
44
from ..ui.base import base_page
55
from ..models import BlogPostModel
@@ -32,6 +32,7 @@ def blog_post_list_item(post: BlogPostModel):
3232
# def foreach_callback(text):
3333
# return rx.box(rx.text(text))
3434

35+
@reflex_local_auth.require_login
3536
def blog_post_list_page() ->rx.Component:
3637
return base_page(
3738
rx.vstack(

full_stack_python/full_stack_python.py

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,53 +15,26 @@
1515

1616

1717
from .articles.detail import article_detail_page
18-
from .articles.list import article_public_list_page
18+
from .articles.list import article_public_list_page, article_public_list_component
1919
from .articles.state import ArticlePublicState
2020

2121
from . import blog, contact, navigation, pages
2222

23-
class State(rx.State):
24-
"""The app state."""
25-
label = "Welcome to Reflex!"
26-
27-
def handle_title_input_change(self, val):
28-
self.label = val
29-
30-
def did_click(self):
31-
print("Hello world did click")
32-
return rx.redirect(navigation.routes.ABOUT_US_ROUTE)
33-
3423
def index() -> rx.Component:
35-
# Welcome Page (Index)
36-
my_user_obj = SessionState.authenticated_user_info
37-
my_child = rx.vstack(
38-
rx.heading(State.label, size="9"),
39-
rx.text(my_user_obj.to_string()),
40-
rx.text(my_user_obj.user.username),
41-
rx.text(
42-
"Get started by editing ",
43-
rx.code(f"{config.app_name}/{config.app_name}.py"),
44-
size="5",
45-
),
46-
# rx.button("About us", on_click=State.did_click),
47-
rx.link(
48-
rx.button("About us"),
49-
href=navigation.routes.ABOUT_US_ROUTE
50-
),
51-
spacing="5",
52-
justify="center",
53-
align="center",
54-
# text_align="center",
55-
min_height="85vh",
56-
id='my-child'
24+
return base_page(
25+
rx.cond(SessionState.is_authenticated,
26+
pages.dashboard_component(),
27+
pages.landing_component(),
5728
)
58-
return base_page(my_child)
29+
)
5930

6031

6132

6233

6334
app = rx.App()
64-
app.add_page(index)
35+
app.add_page(index,
36+
on_load=ArticlePublicState.load_posts
37+
)
6538
# reflex_local_auth pages
6639
app.add_page(
6740
my_login_page,

full_stack_python/pages/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
from .about import about_page
2+
from .dashboard import dashboard_component
3+
from .landing import landing_component
24
from .pricing import pricing_page
35
from .protected import protected_page
46
# from .contact import contact_page
57

68
__all__ = [
79
'about_page',
810
# 'contact_page',
11+
'dashboard_component',
12+
'landing_component',
913
'pricing_page',
1014
'protected_page'
1115
]

full_stack_python/pages/dashboard.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import reflex as rx
2+
3+
from ..articles.list import article_public_list_component
4+
5+
def dashboard_component() -> rx.Component:
6+
return rx.box(
7+
rx.heading("Welcome back", size='2'),
8+
rx.divider(margin_top='1em', margin_bottom='1em'),
9+
article_public_list_component(columns=3, limit=20),
10+
min_height="85vh",
11+
)

full_stack_python/pages/landing.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import reflex as rx
2+
3+
from .. import navigation
4+
from ..articles.list import article_public_list_component
5+
6+
def landing_component() -> rx.Component:
7+
return rx.vstack(
8+
rx.heading("Welcome to SaaS", size="9"),
9+
rx.link(
10+
rx.button("About us"),
11+
href=navigation.routes.ABOUT_US_ROUTE
12+
),
13+
rx.divider(),
14+
rx.heading("Recent Articles", size="5"),
15+
article_public_list_component(columns=1, limit=1),
16+
spacing="5",
17+
justify="center",
18+
align="center",
19+
# text_align="center",
20+
min_height="85vh",
21+
id='my-child'
22+
)

full_stack_python/ui/nav.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def navbar() -> rx.Component:
3535
rx.hstack(
3636
navbar_link("Home", navigation.routes.HOME_ROUTE),
3737
navbar_link("About", navigation.routes.ABOUT_US_ROUTE),
38-
navbar_link("Blog", navigation.routes.BLOG_POSTS_ROUTE),
38+
navbar_link("Articles", navigation.routes.ARTICLE_LIST_ROUTE),
3939
navbar_link("Pricing", navigation.routes.PRICING_ROUTE),
4040
navbar_link("Contact", navigation.routes.CONTACT_US_ROUTE),
4141
spacing="5",
@@ -88,8 +88,8 @@ def navbar() -> rx.Component:
8888
on_click=navigation.NavState.to_home),
8989
rx.menu.item("About",
9090
on_click=navigation.NavState.to_about_us),
91-
rx.menu.item("Blog",
92-
on_click=navigation.NavState.to_blog),
91+
rx.menu.item("Articles",
92+
on_click=navigation.NavState.to_articles),
9393
rx.menu.item("Pricing",
9494
on_click=navigation.NavState.to_pricing),
9595
rx.menu.item("Contact",

0 commit comments

Comments
 (0)