-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (102 loc) · 2.65 KB
/
main.go
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/minivera/go-lander"
"github.com/minivera/go-lander/context"
"github.com/minivera/go-lander/experimental/hooks"
"github.com/minivera/go-lander/nodes"
)
type todo struct {
Id int `json:"id"`
Todo string `json:"todo"`
Completed bool `json:"completed"`
UserId int `json:"userId"`
}
func fetchApp(ctx context.Context, _ nodes.Props, _ nodes.Children) nodes.Child {
loading, setLoading, _ := hooks.UseState[bool](ctx, true)
currentTodo, setTodo, _ := hooks.UseState[*todo](ctx, nil)
hooks.UseEffect(ctx, func() (func() error, error) {
// Simulate some loading
time.Sleep(2 * time.Second)
resp, err := http.Get("https://dummyjson.com/todos/1")
if err != nil {
return nil, err
}
loadedTodo := &todo{}
err = json.NewDecoder(resp.Body).Decode(loadedTodo)
if err != nil {
return nil, err
}
err = setTodo(func(_ *todo) *todo {
return loadedTodo
})
if err != nil {
return nil, err
}
return nil, setLoading(func(_ bool) bool {
return false
})
}, []interface{}{})
content := lander.Html("marquee", nodes.Attributes{}, nodes.Children{
lander.Text("Loading..."),
}).Style("width: 150px;")
if !loading {
content = lander.Html("div", nodes.Attributes{}, nodes.Children{
lander.Html("label", nodes.Attributes{
"for": "id",
}, nodes.Children{
lander.Text("ID"),
}),
lander.Html("input", nodes.Attributes{
"name": "id",
"value": currentTodo.Id,
"readonly": true,
}, nodes.Children{}),
lander.Html("label", nodes.Attributes{
"for": "todo",
}, nodes.Children{
lander.Text("Todo"),
}),
lander.Html("input", nodes.Attributes{
"name": "todo",
"value": currentTodo.Todo,
"readonly": true,
}, nodes.Children{}),
lander.Html("label", nodes.Attributes{
"for": "completed",
}, nodes.Children{
lander.Text("Completed?"),
}),
lander.Html("input", nodes.Attributes{
"name": "completed",
"type": "checkbox",
"checked": currentTodo.Completed,
"readonly": true,
}, nodes.Children{}),
}).Style("width: 150px;")
}
return lander.Html("div", nodes.Attributes{}, nodes.Children{
lander.Html("h1", nodes.Attributes{}, nodes.Children{
lander.Text("Sample loading app"),
}),
lander.Html("div", nodes.Attributes{}, nodes.Children{
content,
}),
}).Style("padding: 1rem;")
}
func main() {
c := make(chan bool)
_, err := lander.RenderInto(
lander.Component(hooks.Provider, nodes.Props{}, nodes.Children{
lander.Component(fetchApp, nodes.Props{}, nodes.Children{}),
}),
"#app",
)
if err != nil {
fmt.Println(err)
}
<-c
}