Подгрузка рандома аяксом
This commit is contained in:
parent
ffcc9eeb17
commit
895c7905f9
7 changed files with 30 additions and 55 deletions
|
@ -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())
|
||||
}
|
||||
|
|
|
@ -42,6 +42,7 @@ templ Layout(params HeaderParams) {
|
|||
</nav>
|
||||
</main>
|
||||
</footer>
|
||||
<script src="/js/htmx.min.js"></script>
|
||||
</html>
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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>
|
||||
}
|
||||
|
|
|
@ -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=\"")
|
||||
for _, q := range quotes {
|
||||
templ_7745c5c3_Err = Quote(&q).Render(ctx, templ_7745c5c3_Buffer)
|
||||
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)
|
||||
}
|
||||
_, 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
|
||||
}
|
||||
|
|
1
static/js/htmx.min.js
vendored
Normal file
1
static/js/htmx.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
|
@ -1,3 +0,0 @@
|
|||
async function shareQuote(title, text, url) {
|
||||
await navigator.share({title, text, url});
|
||||
}
|
Loading…
Reference in a new issue