2024-09-27 07:23:37 +02:00

102 lines
3.3 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_1fr] 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 col-start-1">name</div>
<div>
<div class="flex">
<span id="name" class={ maybeShow("w-40", errors == nil || errors.Name == "") }>{ user.Name }</span>
<form
id="name-form"
class="hidden flex mb-0"
hx-put="/user"
hx-push-url="false"
hx-target="#name"
hx-select="#name"
hx-select-oob="#name-error"
>
<input
name="name"
type="text"
value={ user.Name }
class="w-40 font-mono"
/>
</form>
</div>
if errors != nil && errors.Name != "" {
<div id="name-error" class="w-40 text-xs text-error py-1">{ errors.Name }</div>
} else {
<div id="name-error" class="hidden"></div>
}
</div>
<div id="name-btns" class="grid grid-cols-2 w-[90px] h-min self-center">
<button id="name-save" form="name-form" class="h-min hidden px-3 text-xs self-center" type="submit">save</button>
<button id="name-cancel" class="h-min hidden text-muted hover:text-reset self-center text-xs ms-2">cancel</button>
<button id="name-edit" class="col-span-2 text-muted hover:text-reset text-xs">edit</button>
</div>
<div class="font-bold">joined</div>
<div>{ user.CreatedAt.Format(time.DateOnly) }</div>
<div class="font-bold col-start-1">sats</div>
<!-- TODO: implement withdrawal of sats -->
<div>{ strconv.Itoa(int(user.Msats) / 1000) }</div>
<button class="text-muted hover:text-reset text-xs self-center h-min">withdraw</button>
<!-- TODO: add WebLN and NWC for send+recv -->
<button hx-post="/logout" class="col-start-2">logout</button>
</div>
<script>
function toggleForm () {
htmx.toggleClass("#name", "hidden")
htmx.toggleClass("#name-form", "hidden")
htmx.toggleClass("#name-save", "hidden")
htmx.toggleClass("#name-cancel", "hidden")
htmx.toggleClass("#name-edit", "hidden")
// make sure buttons don't shift on error
htmx.toggleClass("#name-btns", "self-center")
// reset error message on toggle
htmx.addClass("#name-error", "hidden")
$("#name-form>input").focus()
}
htmx.on("#name-edit", "click", toggleForm)
htmx.on("#name-cancel", "click", toggleForm)
htmx.on("#name-form", "htmx:afterRequest", ({ detail }) => {
// only toggle form if request was successful
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")
}