Potion/internal/app/server/state_handler.go
Hayden Hargreaves aad4640527 (REFACTOR): Working on dependency injection.
However, everything seems really slow now... 950ms for a page request?
Something is wrong, just not sure what yet.
2025-09-04 21:51:16 -07:00

77 lines
1.7 KiB
Go

package server
import (
"fmt"
"net/http"
"strings"
"github.com/gin-gonic/gin"
domain "github.com/haydenhargreaves/Potion/internal/domain/server"
)
const TAG_HTML = `
<li
hx-post="%s"
hx-trigger="click"
hx-target="#tag-list"
hx-swap="innerHTML"
hx-include="#tag-list"
hx-vals='{"target": "%s"}'
class="flex text-xs items-center bg-blue-100 w-fit px-2 py-1 rounded-full gap-x-1 select-none cursor-pointer hover:bg-blue-200 duration-300">
&times; %s
</li>
`
const TAG_LIST_HTML = `
<input
hx-swap-oob="outerHTML"
type="hidden"
name="tags"
id="tags"
value="%s"
/>
`
func (s *Server) NewTagHandler(ctx *gin.Context) {
tag := strings.ToLower(ctx.PostForm("tag"))
tags := strings.Split(ctx.PostForm("tags"), ",")
tags = append([]string{tag}, tags...)
var html string
var cleaned_tags []string
for _, tag := range tags {
if tag != "" {
html += fmt.Sprintf(TAG_HTML, domain.STATE_TAGS_DELETE, tag, tag)
// Ensure that the list provided does not contain blank spaces.
// This is another measure to ensure this state is bulletproof.
cleaned_tags = append(cleaned_tags, tag)
}
}
// Execute OOB swap for the tags
html += fmt.Sprintf(TAG_LIST_HTML, strings.Join(cleaned_tags, ","))
ctx.String(http.StatusOK, html)
}
func (s *Server) DeleteTagHandler(ctx *gin.Context) {
tags := strings.Split(ctx.PostForm("tags"), ",")
target := ctx.PostForm("target")
var html string
var new_tags []string
for _, tag := range tags {
if tag != target && tag != "" {
html += fmt.Sprintf(TAG_HTML, domain.STATE_TAGS_DELETE, tag, tag)
new_tags = append(new_tags, tag)
}
}
// Execute OOB swap for the tags
html += fmt.Sprintf(TAG_LIST_HTML, strings.Join(new_tags, ","))
ctx.String(http.StatusOK, html)
}