Подгрузка рандома аяксом

This commit is contained in:
Александр Кирюхин 2024-10-07 04:18:59 +03:00
parent ffcc9eeb17
commit 895c7905f9
Signed by: neonxp
SSH key fingerprint: SHA256:SVt7TjxbVc87m1QYaQziOJ0N3OCFURv2g76gD/UTTXI
7 changed files with 30 additions and 55 deletions

View file

@ -1,6 +1,7 @@
package handler package handler
import ( import (
"github.com/a-h/templ"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"sh.org.ru/pkg/model" "sh.org.ru/pkg/model"
"sh.org.ru/pkg/tpl" "sh.org.ru/pkg/tpl"
@ -14,5 +15,17 @@ func (h *Handler) Random(c echo.Context) error {
return err 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())
} }

View file

@ -42,6 +42,7 @@ templ Layout(params HeaderParams) {
</nav> </nav>
</main> </main>
</footer> </footer>
<script src="/js/htmx.min.js"></script>
</html> </html>
} }

View file

@ -76,7 +76,7 @@ func Layout(params HeaderParams) templ.Component {
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err 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 { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }

View file

@ -1,18 +1,12 @@
package tpl package tpl
import "sh.org.ru/pkg/model" import (
import "math/rand" "sh.org.ru/pkg/model"
import "fmt" )
templ Random(quotes []model.Quote) { templ Random(quotes []model.Quote) {
@Layout(HeaderParams{
Title: "Цитатник Рунета -- случайные",
Description: "Новый цитатник Рунета",
URL: "https://sh.org.ru/random",
}) {
for _, q := range quotes { for _, q := range quotes {
@Quote(&q) @Quote(&q)
} }
<a role="button" href={templ.URL(fmt.Sprintf("/random?%d", rand.Int()))}>Обновить</a> <a role="button" hx-get="/random" hx-swap="outerHTML">Загрузить ещё...</a>
}
} }

View file

@ -8,9 +8,9 @@ package tpl
import "github.com/a-h/templ" import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime" import templruntime "github.com/a-h/templ/runtime"
import "sh.org.ru/pkg/model" import (
import "math/rand" "sh.org.ru/pkg/model"
import "fmt" )
func Random(quotes []model.Quote) templ.Component { func Random(quotes []model.Quote) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { 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 templ_7745c5c3_Var1 = templ.NopComponent
} }
ctx = templ.ClearChildren(ctx) 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 { for _, q := range quotes {
templ_7745c5c3_Err = Quote(&q).Render(ctx, templ_7745c5c3_Buffer) templ_7745c5c3_Err = Quote(&q).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
} }
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" <a role=\"button\" href=\"") _, 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
}
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)))
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)
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }

1
static/js/htmx.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View file

@ -1,3 +0,0 @@
async function shareQuote(title, text, url) {
await navigator.share({title, text, url});
}