aboutsummaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
authorAlexander Neonxp Kiryukhin <i@neonxp.ru>2024-10-07 04:18:59 +0300
committerAlexander Neonxp Kiryukhin <i@neonxp.ru>2024-10-07 04:18:59 +0300
commit895c7905f9e39bbb046c130a3a94e4044bf6c989 (patch)
treefb9b458237f2f3553e0f9b93fa0d70f12f786185 /pkg
parentffcc9eeb1746971d7d009822ae65a1201c54e289 (diff)
Подгрузка рандома аяксом
Diffstat (limited to 'pkg')
-rw-r--r--pkg/handler/random.go15
-rw-r--r--pkg/tpl/layout.templ1
-rw-r--r--pkg/tpl/layout_templ.go2
-rw-r--r--pkg/tpl/random.templ18
-rw-r--r--pkg/tpl/random_templ.go45
5 files changed, 29 insertions, 52 deletions
diff --git a/pkg/handler/random.go b/pkg/handler/random.go
index 091058f..29c5f6f 100644
--- a/pkg/handler/random.go
+++ b/pkg/handler/random.go
@@ -1,6 +1,7 @@
package handler
import (
+ "github.com/a-h/templ"
"github.com/labstack/echo/v4"
"sh.org.ru/pkg/model"
"sh.org.ru/pkg/tpl"
@@ -14,5 +15,17 @@ func (h *Handler) Random(c echo.Context) error {
return err
}
- return tpl.Random(quotes).Render(c.Request().Context(), c.Response())
+ comp := tpl.Random(quotes)
+
+ if c.Request().Header.Get("Hx-Request") == "true" {
+ return comp.Render(c.Request().Context(), c.Response())
+ }
+
+ ctx := templ.WithChildren(c.Request().Context(), comp)
+
+ return tpl.Layout(tpl.HeaderParams{
+ Title: "Цитатник Рунета",
+ Description: "Новый цитатник Рунета",
+ URL: "https://sh.org.ru/",
+ }).Render(ctx, c.Response())
}
diff --git a/pkg/tpl/layout.templ b/pkg/tpl/layout.templ
index 2c41a0d..c777457 100644
--- a/pkg/tpl/layout.templ
+++ b/pkg/tpl/layout.templ
@@ -42,6 +42,7 @@ templ Layout(params HeaderParams) {
</nav>
</main>
</footer>
+ <script src="/js/htmx.min.js"></script>
</html>
}
diff --git a/pkg/tpl/layout_templ.go b/pkg/tpl/layout_templ.go
index eb340ee..a86bb30 100644
--- a/pkg/tpl/layout_templ.go
+++ b/pkg/tpl/layout_templ.go
@@ -76,7 +76,7 @@ func Layout(params HeaderParams) templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
- _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</main></body><footer><main class=\"container\"><nav><ul><li>Сделал <a href=\"https://neonxp.ru/\">NeonXP</a> в 2024 году.</li></ul><ul><a href=\"https://gitrepo.ru/NeonXP/ShOrgRu\">Исходный код</a></ul></nav></main></footer></html>")
+ _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</main></body><footer><main class=\"container\"><nav><ul><li>Сделал <a href=\"https://neonxp.ru/\">NeonXP</a> в 2024 году.</li></ul><ul><a href=\"https://gitrepo.ru/NeonXP/ShOrgRu\">Исходный код</a></ul></nav></main></footer><script src=\"/js/htmx.min.js\"></script></html>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
diff --git a/pkg/tpl/random.templ b/pkg/tpl/random.templ
index 63e7c03..39ce3ff 100644
--- a/pkg/tpl/random.templ
+++ b/pkg/tpl/random.templ
@@ -1,18 +1,12 @@
package tpl
-import "sh.org.ru/pkg/model"
-import "math/rand"
-import "fmt"
+import (
+ "sh.org.ru/pkg/model"
+)
templ Random(quotes []model.Quote) {
- @Layout(HeaderParams{
- Title: "Цитатник Рунета -- случайные",
- Description: "Новый цитатник Рунета",
- URL: "https://sh.org.ru/random",
- }) {
- for _, q := range quotes {
- @Quote(&q)
- }
- <a role="button" href={templ.URL(fmt.Sprintf("/random?%d", rand.Int()))}>Обновить</a>
+ for _, q := range quotes {
+ @Quote(&q)
}
+ <a role="button" hx-get="/random" hx-swap="outerHTML">Загрузить ещё...</a>
}
diff --git a/pkg/tpl/random_templ.go b/pkg/tpl/random_templ.go
index 19a7035..79f5685 100644
--- a/pkg/tpl/random_templ.go
+++ b/pkg/tpl/random_templ.go
@@ -8,9 +8,9 @@ package tpl
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
-import "sh.org.ru/pkg/model"
-import "math/rand"
-import "fmt"
+import (
+ "sh.org.ru/pkg/model"
+)
func Random(quotes []model.Quote) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
@@ -33,44 +33,13 @@ func Random(quotes []model.Quote) templ.Component {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
- templ_7745c5c3_Var2 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
- templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
- templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
- if !templ_7745c5c3_IsBuffer {
- defer func() {
- templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
- if templ_7745c5c3_Err == nil {
- templ_7745c5c3_Err = templ_7745c5c3_BufErr
- }
- }()
- }
- ctx = templ.InitializeContext(ctx)
- for _, q := range quotes {
- templ_7745c5c3_Err = Quote(&q).Render(ctx, templ_7745c5c3_Buffer)
- if templ_7745c5c3_Err != nil {
- return templ_7745c5c3_Err
- }
- }
- _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" <a role=\"button\" href=\"")
- if templ_7745c5c3_Err != nil {
- return templ_7745c5c3_Err
- }
- var templ_7745c5c3_Var3 templ.SafeURL = templ.URL(fmt.Sprintf("/random?%d", rand.Int()))
- _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var3)))
+ for _, q := range quotes {
+ templ_7745c5c3_Err = Quote(&q).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
- _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\">Обновить</a>")
- if templ_7745c5c3_Err != nil {
- return templ_7745c5c3_Err
- }
- return templ_7745c5c3_Err
- })
- templ_7745c5c3_Err = Layout(HeaderParams{
- Title: "Цитатник Рунета -- случайные",
- Description: "Новый цитатник Рунета",
- URL: "https://sh.org.ru/random",
- }).Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer)
+ }
+ _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<a role=\"button\" hx-get=\"/random\" hx-swap=\"outerHTML\">Загрузить ещё...</a>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}