mirror of https://github.com/velour/catbase.git
66 lines
1.9 KiB
Plaintext
66 lines
1.9 KiB
Plaintext
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() {
|
|
<div class="grid-container">
|
|
<div class="grid-x">
|
|
<h2>Counter</h2>
|
|
</div>
|
|
<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>
|
|
for _, thing := range items {
|
|
@p.renderItem(user, thing)
|
|
}
|
|
}
|
|
</table>
|
|
</div>
|
|
}
|
|
|
|
templ (p *CounterPlugin) renderItem(user string, item Item) {
|
|
<tr id={ fmt.Sprintf("item%d", item.ID) }>
|
|
<td>
|
|
{ item.Item }
|
|
</td>
|
|
<td>
|
|
{ fmt.Sprintf("%d", item.Count) }
|
|
</td>
|
|
<td>
|
|
<button
|
|
class="button tiny alert"
|
|
style="vertical-align: baseline"
|
|
hx-target={ "#"+fmt.Sprintf("item%d", item.ID) }
|
|
hx-include="[name='password']"
|
|
hx-swap="outerHTML"
|
|
hx-post={ urlFor(user, item.Item, "decrement") }
|
|
>-</button>
|
|
<button
|
|
class="button tiny success"
|
|
style="vertical-align: baseline"
|
|
hx-target={ "#"+fmt.Sprintf("item%d", item.ID) }
|
|
hx-include="[name='password']"
|
|
hx-swap="outerHTML"
|
|
hx-post={ urlFor(user, item.Item, "increment") }
|
|
>+</button>
|
|
</td>
|
|
</tr>
|
|
} |