catbase/plugins/counter/counter.templ

61 lines
1.7 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": {}}
2024-02-27 20:36:50 +00:00
}
return items
}
templ (p *CounterPlugin) index() {
<h2>Counter</h2>
<label>Password
<input class="input-group-field" type="text" name="password" />
</label>
<table class="striped">
for user, items := range p.allItems() {
<thead>
2024-02-28 14:52:49 +00:00
<tr><th class="text-left" colspan="3">{ user }</th></tr>
</thead>
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
}
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
}