add htmx-alpine with optimistic ui experience

This commit is contained in:
2025-12-09 18:46:50 +01:00
parent ddc3d77394
commit cfa0734f08
6 changed files with 282 additions and 0 deletions
@@ -0,0 +1,44 @@
<li
id="todo-{{.ID}}"
x-data="{ editing: false, name: '{{.Name}}', backup: '{{.Name}}' }"
@htmx:response-error="name = backup; $dispatch('show-toast', 'Error al actualizar. Inténtelo de nuevo.');"
>
<div x-show="!editing" @dblclick="editing = true" x-text="name">
{{ .Name }} {{ .Completed }}
</div>
<div x-show="editing" style="display: none">
<form
hx-put="/todo/{{.ID}}/update"
hx-target="closest li"
hx-swap="outerHTML"
@submit="editing = false"
>
<input
type="text"
name="name"
value="{{ .Name }}"
@keydown.escape="name = backup; editing = false"
x-model="name"
x-init="$el.focus()"
/>
<button type="button" @click="name =backup; editing = false">
Cancelar
</button>
<button type="submit">Confirmar</button>
</form>
</div>
<button x-show="!editing" @click="editing = true">Editar</button>
<button
x-show="!editing"
hx-patch="/todo/{{.ID}}/completed"
hx-target="closest li"
hx-swap="outerHTML"
>
Completado
</button>
</li>
@@ -0,0 +1,9 @@
<h2>Listado de tareas</h2>
<button hx-get="/todo/new" hx-target="#todo-list-body" hx-swap="afterbegin">
Nueva tarea
</button>
<ol id="todo-list-body">
{{ range .TodoList }} {{ template "fragments/todo-row" . }} {{ end }}
</ol>
+24
View File
@@ -0,0 +1,24 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Lista de tareas</title>
<script src="https://cdn.jsdelivr.net/npm/htmx.org@2/dist/htmx.min.js"></script>
<script
defer
src="https://cdn.jsdelivr.net/npm/alpinejs@3/dist/cdn.min.js"
></script>
</head>
<body>
<div hx-get="/todo" hx-trigger="load" hx-swap="outerHTML">Cargando...</div>
<div
x-data="{ show: false, message: '' }"
@show-toast.window="show = true; message = $event.detail; setTimeout(() => show = false, 3000)"
x-show="show"
>
<span x-text="message"></span>
</div>
</body>
</html>