-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp2.mojo
86 lines (78 loc) · 2.85 KB
/
app2.mojo
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from ui import *
# Example of creating routing and login system with components,
# (don't use it as a login system, it's not complete)
def main():
appdata = Python.dict()
appdata["users"] = ["user1", "user2"]
exit_if_client_not_in = PythonObject(["127.0.0.1"])
serve_app[MainComponent](appdata, exit_if_client_not_in)
@value
struct MainComponent: #(Component):
var input_login: String
var connected: Optional[String]
var session: Session
def __init__(inout self, session: Session):
self.connected = None
self.input_login = "user name (user1 or user2)"
self.session = session
def Event(inout self, data: PythonObject)->Action:
if data["EventName"] == "change_input_login":
self.input_login = str(data["value"])
if data["EventName"] == "connect":
if self.session.appdata()["users"].__contains__(self.input_login):
self.connected = str(self.input_login)
if data["EventName"] == "disconnect":
self.connected = None
return Action.Render
def Render(inout self, props: PythonObject)->PythonObject:
if self.connected:
return Div(
H1(
Text("Connected! " + self.connected.value()),
`class`= "bg-green"
),
Button(
Text("Disconnect"),
click = Event("disconnect"),
),
self.session.Render[Router]("simple_router"),
)
else: return Div(
Text("login:"),
Input(
type='text', value=self.input_login,
change=Event("change_input_login"),
),
Button(
Text("connect"),
`class`= "btn",
click = Event("connect"),
),
`class` = "bg-blue"
)
@value
struct Router: #(Component):
var current_page: String
var session: Session
def __init__(inout self, session: Session):
self.current_page = "home"
self.session = session
def Event(inout self, data: PythonObject)->Action:
if data["EventName"] == "change_route":
self.current_page = str(data["page_name"])
return Action.Render
def Render(inout self, props: PythonObject)->PythonObject:
page = Div(
Button(Text("home"), click=Event("change_route",page_name="home")),
Button(Text("test"), click=Event("change_route",page_name="test"))
)
if self.current_page == "home":
AppendElement(
Div(
H1(Text("home")),
Text("Welcome to the home page!"),
), to = page
)
else:
AppendElement(Div(H1(Text(self.current_page))), to=page)
return page