Skip to content

Commit 82bc3f2

Browse files
35 - Publish Date Toggle
1 parent 5a83527 commit 82bc3f2

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

full_stack_python/blog/forms.py

+29
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def blog_post_add_form() -> rx.Component:
3939
def blog_post_edit_form() -> rx.Component:
4040
post = BlogEditFormState.post
4141
title = post.title
42+
publish_active = post.publish_active
4243
post_content = BlogEditFormState.post_content
4344
return rx.form(
4445
rx.box(
@@ -70,6 +71,34 @@ def blog_post_edit_form() -> rx.Component:
7071
height='50vh',
7172
width='100%',
7273
),
74+
rx.flex(
75+
rx.switch(
76+
default_checked=BlogEditFormState.post_publish_active,
77+
on_change=BlogEditFormState.set_post_publish_active,
78+
name='publish_active',
79+
),
80+
rx.text("Publish Active"),
81+
spacing="2",
82+
),
83+
rx.cond(
84+
BlogEditFormState.post_publish_active,
85+
rx.box(
86+
rx.hstack(
87+
rx.input(
88+
type='date',
89+
name='publish_date',
90+
width='100%'
91+
),
92+
rx.input(
93+
type='time',
94+
name='publish_time',
95+
width='100%'
96+
),
97+
width='100%'
98+
),
99+
width='100%'
100+
)
101+
),
73102
rx.button("Submit", type="submit"),
74103
),
75104
on_submit=BlogEditFormState.handle_submit,

full_stack_python/blog/model.py

+1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ class BlogPostModel(rx.Model, table=True):
2828
},
2929
nullable=False
3030
)
31+
publish_active: bool = False
3132
# publish_date
3233
# publish_time

full_stack_python/blog/state.py

+13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class BlogPostState(rx.State):
1414
posts: List['BlogPostModel'] = []
1515
post: Optional['BlogPostModel'] = None
1616
post_content: str = ""
17+
post_publish_active: bool = False
1718

1819
@rx.var
1920
def blog_post_id(self):
@@ -46,6 +47,7 @@ def get_post_detail(self):
4647
self.post_content = ""
4748
return
4849
self.post_content = self.post.content
50+
self.post_publish_active = self.post.publish_active
4951
# return
5052

5153

@@ -108,6 +110,17 @@ class BlogEditFormState(BlogPostState):
108110
def handle_submit(self, form_data):
109111
self.form_data = form_data
110112
post_id = form_data.pop('post_id')
113+
publish_date = None
114+
if 'publish_date' in form_data:
115+
publish_date = form_data.pop('publish_date')
116+
publish_time = None
117+
if 'publish_time' in form_data:
118+
publish_time = form_data.pop('publish_time')
119+
print(publish_date, publish_time)
120+
publish_active = False
121+
if 'publish_active' in form_data:
122+
publish_active = form_data.pop('publish_active') == "on"
111123
updated_data = {**form_data}
124+
updated_data['publish_active'] = publish_active
112125
self.save_post_edits(post_id, updated_data)
113126
return self.to_blog_post()

0 commit comments

Comments
 (0)