forked from reactive-python/reactpy-django
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse_mutation.py
More file actions
30 lines (22 loc) · 766 Bytes
/
use_mutation.py
File metadata and controls
30 lines (22 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from reactpy import component, html
from example.models import TodoItem
from reactpy_django.hooks import use_mutation
async def add_item(text: str):
await TodoItem(text=text).asave()
@component
def todo_list():
item_mutation = use_mutation(add_item)
def submit_event(event):
if event["key"] == "Enter":
item_mutation(text=event["target"]["value"])
if item_mutation.loading:
mutation_status = html.h2("Adding...")
elif item_mutation.error:
mutation_status = html.h2("Error when adding!")
else:
mutation_status = html.h2("Mutation done.")
return html.div(
html.label("Add an item:"),
html.input({"type": "text", "onKeyDown": submit_event}),
mutation_status,
)