~repos /gromer
git clone https://pyrossh.dev/repos/gromer.git
gromer is a framework and cli to build multipage web apps in golang using htmx and alpinejs.
4647e3a1
—
Peter John 3 years ago
add Error handling
- _example/containers/Error.go +24 -0
- _example/containers/TodoCount.go +14 -14
- _example/containers/TodoList.go +17 -18
_example/containers/Error.go
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
package containers
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
. "github.com/pyros2097/gromer/gsx"
|
|
5
|
+
)
|
|
6
|
+
|
|
7
|
+
func Error(c Context, err error) *Node {
|
|
8
|
+
c.Set("err", err.Error())
|
|
9
|
+
return c.Render(`
|
|
10
|
+
<span class="error">
|
|
11
|
+
<strong>Failed to load: {err}</strong>
|
|
12
|
+
</span>
|
|
13
|
+
`)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
var _ = Css(`
|
|
17
|
+
.error {
|
|
18
|
+
color: red;
|
|
19
|
+
font-size: 18px;
|
|
20
|
+
font-weight: 500;
|
|
21
|
+
padding: 24px;
|
|
22
|
+
border: red 1px solid;
|
|
23
|
+
}
|
|
24
|
+
`)
|
_example/containers/TodoCount.go
CHANGED
|
@@ -5,29 +5,29 @@ import (
|
|
|
5
5
|
. "github.com/pyros2097/gromer/gsx"
|
|
6
6
|
)
|
|
7
7
|
|
|
8
|
-
var _ = Css(`
|
|
9
|
-
.todo-count {
|
|
10
|
-
float: left;
|
|
11
|
-
text-align: left;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
.todo-count strong {
|
|
15
|
-
font-weight: 300;
|
|
16
|
-
}
|
|
17
|
-
`)
|
|
18
|
-
|
|
19
|
-
func TodoCount(c Context, filter string)
|
|
8
|
+
func TodoCount(c Context, filter string) *Node {
|
|
20
9
|
todos, err := todos.GetAllTodo(c, todos.GetAllTodoParams{
|
|
21
10
|
Filter: filter,
|
|
22
11
|
Limit: 1000,
|
|
23
12
|
})
|
|
24
13
|
if err != nil {
|
|
25
|
-
return
|
|
14
|
+
return Error(c, err)
|
|
26
15
|
}
|
|
27
16
|
c.Set("count", len(todos))
|
|
28
17
|
return c.Render(`
|
|
29
18
|
<span id="todo-count" class="todo-count" hx-swap-oob="true">
|
|
30
19
|
<strong>{count}</strong> item left
|
|
31
20
|
</span>
|
|
32
|
-
`)
|
|
21
|
+
`)
|
|
33
22
|
}
|
|
23
|
+
|
|
24
|
+
var _ = Css(`
|
|
25
|
+
.todo-count {
|
|
26
|
+
float: left;
|
|
27
|
+
text-align: left;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
.todo-count strong {
|
|
31
|
+
font-weight: 300;
|
|
32
|
+
}
|
|
33
|
+
`)
|
_example/containers/TodoList.go
CHANGED
|
@@ -6,6 +6,23 @@ import (
|
|
|
6
6
|
. "github.com/pyros2097/gromer/gsx"
|
|
7
7
|
)
|
|
8
8
|
|
|
9
|
+
func TodoList(c Context, page int, filter string) *Node {
|
|
10
|
+
index := Default(page, 1)
|
|
11
|
+
todos, err := todos.GetAllTodo(c, todos.GetAllTodoParams{
|
|
12
|
+
Filter: filter,
|
|
13
|
+
Limit: index,
|
|
14
|
+
})
|
|
15
|
+
if err != nil {
|
|
16
|
+
return Error(c, err)
|
|
17
|
+
}
|
|
18
|
+
c.Set("todos", todos)
|
|
19
|
+
return c.Render(`
|
|
20
|
+
<ul id="todo-list" class="relative" x-for="todo in todos">
|
|
21
|
+
<Todo />
|
|
22
|
+
</ul>
|
|
23
|
+
`)
|
|
24
|
+
}
|
|
25
|
+
|
|
9
26
|
var _ = Css(`
|
|
10
27
|
.todo-list {
|
|
11
28
|
margin: 0;
|
|
@@ -96,22 +113,4 @@ var _ = Css(`
|
|
|
96
113
|
height: 40px;
|
|
97
114
|
}
|
|
98
115
|
}
|
|
99
|
-
|
|
100
116
|
`)
|
|
101
|
-
|
|
102
|
-
func TodoList(c Context, page int, filter string) (*Node, error) {
|
|
103
|
-
index := Default(page, 1)
|
|
104
|
-
todos, err := todos.GetAllTodo(c, todos.GetAllTodoParams{
|
|
105
|
-
Filter: filter,
|
|
106
|
-
Limit: index,
|
|
107
|
-
})
|
|
108
|
-
if err != nil {
|
|
109
|
-
return nil, err
|
|
110
|
-
}
|
|
111
|
-
c.Set("todos", todos)
|
|
112
|
-
return c.Render(`
|
|
113
|
-
<ul id="todo-list" class="relative" x-for="todo in todos">
|
|
114
|
-
<Todo />
|
|
115
|
-
</ul>
|
|
116
|
-
`), nil
|
|
117
|
-
}
|