delphi.market/server/router/pages/user.templ

95 lines
2.8 KiB
Plaintext

package pages
import (
"fmt"
"git.ekzyis.com/ekzyis/delphi.market/server/router/pages/components"
"git.ekzyis.com/ekzyis/delphi.market/types"
"strconv"
"time"
)
templ User(user *types.User, errors *types.UserEditError) {
<html>
@components.Head()
<body class="container">
@components.Nav()
<div id="content" class="flex flex-col">
@components.Figlet("random", "user")
<div
class="grid grid-cols-[1fr_3fr] gap-4 my-3 mx-auto"
hx-target="#content"
hx-swap="outerHTML"
hx-select="#content"
hx-push-url="true"
hx-select-oob="#nav"
>
<div class="font-bold">id</div>
<div>{ strconv.Itoa(user.Id) }</div>
<div class="font-bold">name</div>
<div>
<div class="flex">
<span id="name" class={ maybeShow("", errors == nil || errors.Name == "") }>{ user.Name }</span>
<form
class="hidden flex mb-0"
hx-put="/user"
hx-push-url="false"
hx-target="#name"
hx-select="#name"
hx-select-oob="#error"
>
<input
name="name"
type="text"
value={ user.Name }
class="font-mono w-[160px]"
/>
<button class="ms-1 px-1" type="submit">save</button>
</form>
<button id="cancel" class="hidden flex text-muted hover:text-reset text-xs items-center ms-1">cancel</button>
<button id="edit" class="flex text-muted hover:text-reset text-xs items-center ms-1">edit</button>
</div>
if errors != nil && errors.Name != "" {
<div id="error" class="text-xs text-error">{ errors.Name }</div>
} else {
<div id="error" class="hidden"></div>
}
</div>
<div class="font-bold">joined</div>
<div>{ user.CreatedAt.Format(time.DateOnly) }</div>
<div class="font-bold">sats</div>
<!-- TODO: implement withdrawal of sats -->
<div class="flex">
<span>{ strconv.Itoa(int(user.Msats) / 1000) }</span>
<button class="flex text-muted hover:text-reset text-xs items-center ms-3">withdraw</button>
</div>
<!-- TODO: add WebLN and NWC for send+recv -->
<button hx-post="/logout" class="col-span-2">logout</button>
</div>
<script>
function toggleForm () {
htmx.toggleClass("#name", "hidden")
htmx.toggleClass("#name+form", "hidden")
htmx.toggleClass("#cancel", "hidden")
htmx.toggleClass("#edit", "hidden")
$("#name+form>input").focus()
}
htmx.on("#edit", "click", toggleForm)
htmx.on("#cancel", "click", toggleForm)
htmx.on("form", "htmx:afterRequest", ({ detail }) => {
if (detail.successful) toggleForm()
})
</script>
</div>
@components.Footer()
</body>
</html>
}
func maybeShow(class string, show bool) string {
if show {
return class
}
return fmt.Sprintf("%s %s", class, "hidden")
}