catbase/plugins/counter/counter.templ

66 lines
1.9 KiB
Plaintext
Raw Normal View History

2024-02-27 20:36:50 +00:00
package counter
import "fmt"
func urlFor(who, what, dir string) string {
return fmt.Sprintf("/counter/users/%s/items/%s/%s", who, what, dir)
}
func (p *CounterPlugin) allItems() map[string][]Item {
items, err := GetAllItemsByUser(p.db)
if err != nil {
return map[string][]Item{"error": []Item{}}
}
return items
}
templ (p *CounterPlugin) index() {
2024-02-28 14:52:49 +00:00
<div class="grid-container">
<div class="grid-x">
<h2>Counter</h2>
2024-02-27 20:36:50 +00:00
</div>
2024-02-28 14:52:49 +00:00
<div class="grid-x">
<div class="input-group">
<span class="input-group-label">Password</span>
<input class="input-group-field" type="text" name="password" />
</div>
</div>
<table>
for user, items := range p.allItems() {
<tr><th class="text-left" colspan="3">{ user }</th></tr>
2024-02-27 20:36:50 +00:00
for _, thing := range items {
@p.renderItem(user, thing)
}
2024-02-28 14:52:49 +00:00
}
</table>
2024-02-27 20:36:50 +00:00
</div>
}
templ (p *CounterPlugin) renderItem(user string, item Item) {
2024-02-28 14:52:49 +00:00
<tr id={ fmt.Sprintf("item%d", item.ID) }>
<td>
2024-02-27 20:36:50 +00:00
{ item.Item }
2024-02-28 14:52:49 +00:00
</td>
<td>
2024-02-27 20:36:50 +00:00
{ fmt.Sprintf("%d", item.Count) }
2024-02-28 14:52:49 +00:00
</td>
<td>
2024-02-27 20:36:50 +00:00
<button
2024-02-28 14:52:49 +00:00
class="button tiny alert"
style="vertical-align: baseline"
2024-02-27 20:36:50 +00:00
hx-target={ "#"+fmt.Sprintf("item%d", item.ID) }
hx-include="[name='password']"
hx-swap="outerHTML"
hx-post={ urlFor(user, item.Item, "decrement") }
>-</button>
<button
2024-02-28 14:52:49 +00:00
class="button tiny success"
style="vertical-align: baseline"
2024-02-27 20:36:50 +00:00
hx-target={ "#"+fmt.Sprintf("item%d", item.ID) }
hx-include="[name='password']"
hx-swap="outerHTML"
hx-post={ urlFor(user, item.Item, "increment") }
>+</button>
2024-02-28 14:52:49 +00:00
</td>
</tr>
2024-02-27 20:36:50 +00:00
}