Skip to content

Commit 123e763

Browse files
refactor(web): use block for views
1 parent aead69b commit 123e763

20 files changed

+109
-109
lines changed

web/controllers/controllers.go

+26
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,32 @@ func Index(retentionPolicy models.RetentionPolicy) func(c *fiber.Ctx, cc *core.C
3737
}
3838
}
3939

40+
func IndexPStorageTable(c *fiber.Ctx, cc *core.Context) error {
41+
// Execute
42+
storage, err := procs.StorageGet(cc)
43+
if err != nil {
44+
return helpers.Error(c, err)
45+
}
46+
47+
// Response
48+
return c.Render("index", fiber.Map{
49+
"Storage": storage,
50+
}, "storage-table")
51+
}
52+
53+
func IndexPRecentEnvelopesTable(c *fiber.Ctx, cc *core.Context) error {
54+
// Execute
55+
messages, err := procs.EnvelopeMessageList(cc, pagination.NewPage(1, 5), envelope.MessageFilter{})
56+
if err != nil {
57+
return helpers.Error(c, err)
58+
}
59+
60+
// Response
61+
return c.Render("index", fiber.Map{
62+
"Messages": messages.Messages,
63+
}, "recent-envelopes-table")
64+
}
65+
4066
func Files(app core.App) fiber.Handler {
4167
return filesystem.New(filesystem.Config{
4268
Root: http.FS(app.File.FS),

web/controllers/p.go

-36
This file was deleted.

web/controllers/rules.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ func RuleEnable(c *fiber.Ctx, cc *core.Context, id int64) error {
3131
}
3232

3333
// Response
34-
return partial(c, "p/rule-enable-form", rule)
34+
return c.Render("rules", rule, "rule-enable-form")
3535
}

web/http/routes.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import (
1010

1111
func routes(http *fiber.App, app core.App, retentionPolicy models.RetentionPolicy) {
1212
http.Get("/", middleware.App(app, controllers.Index(retentionPolicy)))
13+
http.Route("/p", func(http fiber.Router) {
14+
http.Get("/storage-table", middleware.App(app, controllers.IndexPStorageTable))
15+
http.Get("/recent-envelopes-table", middleware.App(app, controllers.IndexPRecentEnvelopesTable))
16+
})
1317

1418
http.Route("/envelopes", func(http fiber.Router) {
1519
http.Get("/", middleware.App(app, controllers.Envelopes))
@@ -46,8 +50,4 @@ func routes(http *fiber.App, app core.App, retentionPolicy models.RetentionPolic
4650
http.Post("/trim", middleware.App(app, controllers.Trim))
4751
http.Group("/files", controllers.Files(app))
4852

49-
http.Route("/p", func(http fiber.Router) {
50-
http.Get("/storage-table", middleware.App(app, controllers.PStorageTable))
51-
http.Get("/recent-envelopes-table", middleware.App(app, controllers.PRecentEnvelopesTable))
52-
})
5353
}

web/views/404.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
{{template "p/header" .}}
1+
{{template "partials/header" .}}
22

33
<main class="container">
44
<div>404 Not Found</div>
55
</main>
66

7-
{{template "p/footer" .}}
7+
{{template "partials/footer" .}}

web/views/500.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
{{template "p/header" .}}
1+
{{template "partials/header" .}}
22

33
<main class="container">
44
<div>500 Internal Server Error</div>
55
<p>{{.Error}}</p>
66
</main>
77

8-
{{template "p/footer" .}}
8+
{{template "partials/footer" .}}

web/views/_default.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
{{template "p/header" .}}
1+
{{template "partials/header" .}}
22

33
<main class="container">
44
<section></section>
55
</main>
66

7-
{{template "p/footer" .}}
7+
{{template "partials/footer" .}}

web/views/attachments.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{template "p/header" .}}
1+
{{template "partials/header" .}}
22

33
<main class="container">
44
<section>
@@ -79,7 +79,7 @@ <h2>
7979
</details>
8080

8181
{{range $val := .Attachments}}
82-
{{template "p/attachment-card" $val}}
82+
{{template "partials/attachment-card" $val}}
8383
<hr>
8484
{{end}}
8585

@@ -109,4 +109,4 @@ <h2>
109109
</section>
110110
</main>
111111

112-
{{template "p/footer" .}}
112+
{{template "partials/footer" .}}

web/views/endpoints.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{template "p/header" .}}
1+
{{template "partials/header" .}}
22

33
<main class="container">
44
<hgroup>
@@ -83,4 +83,4 @@ <h4>Config</h4>
8383
{{end}}
8484
</main>
8585

86-
{{template "p/footer" .}}
86+
{{template "partials/footer" .}}

web/views/envelopes-new.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{template "p/header" .}}
1+
{{template "partials/header" .}}
22

33
<main class="container">
44
<nav aria-label="breadcrumb">
@@ -42,4 +42,4 @@ <h2>New Envelope</h2>
4242
</form>
4343
</main>
4444

45-
{{template "p/footer" .}}
45+
{{template "partials/footer" .}}

web/views/envelopes-show.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{template "p/header" .}}
1+
{{template "partials/header" .}}
22

33
<main class="container">
44
<nav aria-label="breadcrumb">
@@ -55,7 +55,7 @@ <h2><a href="#message">Message</a></h2>
5555
<section id="attachments">
5656
<h2><a href="#attachments">Attachments</a></h2>
5757
{{range $val := .Envelope.Attachments}}
58-
{{template "p/attachment-card" $val}}
58+
{{template "partials/attachment-card" $val}}
5959
<hr>
6060
{{end}}
6161
</section>
@@ -93,4 +93,4 @@ <h2><a href="#html">HTML</a></h2>
9393
{{end}}
9494
</main>
9595

96-
{{template "p/footer" .}}
96+
{{template "partials/footer" .}}

web/views/envelopes.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{template "p/header" .}}
1+
{{template "partials/header" .}}
22

33
<main class="container">
44
<section>
@@ -145,4 +145,4 @@ <h2>
145145
</section>
146146
</main>
147147

148-
{{template "p/footer" .}}
148+
{{template "partials/footer" .}}

web/views/index.html

+52-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,58 @@
1-
{{template "p/header" .}}
1+
{{template "partials/header" .}}
22

33
<main class="container">
44
<article>
55
<h2>Recent Envelopes</h2>
66
<figure>
7-
{{template "p/recent-envelopes-table" .}}
7+
{{block "recent-envelopes-table" .}}
8+
<table hx-trigger="every 5s, deletedEnvelopes from:body, trimmed from:body" hx-get="/p/recent-envelopes-table"
9+
hx-swap="outerHTML">
10+
<tbody>
11+
{{range $val := .Messages}}
12+
<tr>
13+
<td class="text-left">
14+
<a href="/envelopes/{{$val.ID}}">{{$val.Subject}}</a>
15+
</td>
16+
<td class="text-right">
17+
<a href="/envelopes/{{$val.ID}}" title="{{timeFormat $val.CreatedAt}}">
18+
{{timeHumanize $val.CreatedAt}}
19+
</a>
20+
</td>
21+
</tr>
22+
{{end}}
23+
</tbody>
24+
</table>
25+
{{end}}
826
</figure>
927
</article>
1028

11-
<article id="storage">
29+
<article>
1230
<h2>Storage</h2>
13-
<figure>{{template "p/storage-table" .}}</figure>
31+
<figure>
32+
{{block "storage-table" .}}
33+
<table hx-trigger="every 5s, vacuumed from:body, trimmed from:body, deletedEnvelopes from:body"
34+
hx-get="/p/storage-table" hx-swap="outerHTML">
35+
<tbody>
36+
<tr>
37+
<th scope="row"><strong>Envelope Count</strong></th>
38+
<td>{{.Storage.EnvelopeCount}}</td>
39+
</tr>
40+
<tr>
41+
<th scope="row"><strong>Attachment Count</strong></th>
42+
<td>{{.Storage.AttachmentCount}}</td>
43+
</tr>
44+
<tr>
45+
<th scope="row"><strong>Attachment Size</strong></th>
46+
<td>{{bytesHumanize .Storage.AttachmentSize}}</td>
47+
</tr>
48+
<tr>
49+
<th scope="row"><strong>Database Size</strong></th>
50+
<td>{{bytesHumanize .Storage.DatabaseSize}}</td>
51+
</tr>
52+
</tbody>
53+
</table>
54+
{{end}}
55+
</figure>
1456
<div class="grid">
1557
<form method="post" action="/vacuum" hx-post="/vacuum" class="pico-form-reset">
1658
<button data-loading-path="/vacuum" data-loading-disable data-loading-aria-busy
@@ -37,7 +79,11 @@ <h2>Retention Policy</h2>
3779
<table>
3880
<tbody>
3981
<tr>
40-
<th scope="row"><strong>Minimum Age</strong></th>
82+
<th scope="row">
83+
<strong>
84+
Minimum Age
85+
</strong>
86+
</th>
4187
<td>{{.RetentionPolicy.MinAge}}</td>
4288
</tr>
4389
{{if not (eq .RetentionPolicy.EnvelopeCount nil)}}
@@ -94,4 +140,4 @@ <h2>Build</h2>
94140
</article>
95141
</main>
96142

97-
{{template "p/footer" .}}
143+
{{template "partials/footer" .}}

web/views/p/recent-envelopes-table.html

-16
This file was deleted.

web/views/p/rule-enable-form.html

-4
This file was deleted.

web/views/p/storage-table.html

-21
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.

web/views/rules.html

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{template "p/header" .}}
1+
{{template "partials/header" .}}
22

33
<main class="container">
44
<hgroup>
@@ -15,7 +15,12 @@ <h2>Showing {{len .AggregateRules}} rules.</h2>
1515
<div>
1616

1717
<div>
18-
{{template "p/rule-enable-form" $val.Rule}}
18+
{{block "rule-enable-form" $val.Rule}}
19+
<form hx-post="/rules/{{.ID}}/enable" hx-trigger="change from:find input" hx-swap="outerHTML swap:100ms"
20+
class="pico-form-reset">
21+
<input name="enable" {{if .Enable}}checked{{end}} type="checkbox" role="switch" />
22+
</form>
23+
{{end}}
1924
</div>
2025
</div>
2126
</div>
@@ -64,4 +69,4 @@ <h2>Showing {{len .AggregateRules}} rules.</h2>
6469
{{end}}
6570
</main>
6671

67-
{{template "p/footer" .}}
72+
{{template "partials/footer" .}}

0 commit comments

Comments
 (0)