-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.templ
150 lines (142 loc) · 4.67 KB
/
list.templ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package listsweb
import "fmt"
import "github.com/joerdav/shopping-list/app/common"
import "path"
func recipeInList(recipeID string, list List) bool {
for _, r := range list.Recipes {
if r.ID == recipeID {
return true
}
}
return false
}
templ ListRecipes(
list List,
availableRecipes []Recipe,
) {
for _, recipe := range list.Recipes {
<div class="grid grid-cols-2">
<p class="col-span-1 py-1">{ recipe.Name }</p>
@common.SetQuantity(fmt.Sprintf("/lists/%v/recipe", list.ID), "#list", "recipeID", recipe.ID, recipe.Quantity)
</div>
}
<div>
<select
name="recipeID"
hx-trigger="change"
hx-put={ fmt.Sprintf("/lists/%v/recipe", list.ID) }
hx-target="#list"
hx-select="#list"
hx-swap="outerHTML"
hx-vals='{"quantity":"1"}'
class="bg-white border border-gray-300 text-black rounded-full shadow-sm focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5"
>
<option value="">Select a recipe</option>
for _, v := range availableRecipes {
if !recipeInList(v.ID, list) {
<option value={ v.ID }>{ v.Name }</option>
}
}
</select>
</div>
}
func itemInList(itemID string, list List) bool {
for _, i := range list.Items {
if i.ID == itemID {
return true
}
}
return false
}
templ ListItems(list List, availableItems []Item) {
for _, item := range list.Items {
<div class="grid grid-cols-2">
<p class="col-span-1 py-1">{ item.Name }</p>
@common.SetQuantity(fmt.Sprintf("/lists/%v/item", list.ID), "#list", "itemID", item.ID, item.Quantity)
</div>
}
<div>
<select
name="itemID"
hx-trigger="change"
hx-put={ fmt.Sprintf("/lists/%v/item", list.ID) }
hx-target="#list"
hx-select="#list"
hx-swap="outerHTML"
hx-vals='{"quantity":"1"}'
class="bg-white border border-gray-300 text-black rounded-full shadow-sm focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5"
>
<option value="">Select an item</option>
for _, v := range availableItems {
if !itemInList(v.ID, list) {
<option value={ v.ID }>{ v.Name }</option>
}
}
</select>
</div>
}
templ ItemButton(listID string, i ListItem) {
<button
class={ "border border-gray-300 rounded-full shadow px-4 py-1", templ.KV("line-through bg-gray-200 text-gray-500", i.Bought) }
hx-post={ path.Join("/lists", listID, "item", i.ID, "toggle") }
hx-swap="outerHTML"
hx-target="#list"
hx-select="#list"
>{ i.Name } x { fmt.Sprint(i.Quantity) }</button>
}
templ ListPage(urlpath string, list List, availableRecipes []Recipe, availableItems []Item) {
@common.Layout(list.CreatedDate.Format("List - 02 Jan 06 15:04"), urlpath) {
<section id="list" class="grid grid-cols-1 gap-4 px-4 pt-4 pb-28 sm:grid-cols-3">
<h2 class="text-xl border-b border-gray-300 col-span-full font-bold px-4">List</h2>
<div class="rounded-xl border border-gray-100 shadow-lg py-4 px-2">
<h2 class="font-semibold text-lg text-black p-2 mb-2 border-b border-gray-300">Recipes</h2>
<div class="px-2 py-4">
@ListRecipes(list, availableRecipes)
</div>
</div>
<div class="rounded-xl border border-gray-100 shadow-lg py-4 px-2">
<h2 class="font-semibold text-lg text-black p-2 mb-2 border-b border-gray-300">Items</h2>
<div class="px-2 py-4">
@ListItems(list, availableItems)
</div>
</div>
<h2 class="text-xl border-b border-gray-300 col-span-full font-bold px-4">Shops</h2>
for _, shop := range list.Shops {
<div class="rounded-xl border border-gray-100 shadow-lg py-4 px-2">
<h2 class="font-semibold text-lg text-black p-2 mb-2 border-b border-gray-300">{ shop.Name }</h2>
<div class="py-4 px-2">
<div class="flex flex-wrap gap-1">
for _, i := range shop.Items {
@ItemButton(list.ID, i)
}
</div>
</div>
</div>
}
<div class="flex p-8 col-span-full">
<button type="submit" class="bg-red-600 py-2 px-4 text-white text-lg rounded-full shadow-lg mx-auto" hx-delete={ "/lists/" + list.ID } hx-target="body" hx-swap="outerHTML" hx-push-url="true">
Delete list
</button>
</div>
</section>
}
}
templ ListsPage(path string, lists []ListSummary) {
@common.Layout("Lists", path) {
<section class="grid grid-cols-1 gap-4 px-4 pt-4 pb-28 sm:grid-cols-3">
for _, s := range lists {
<a href={ templ.SafeURL("/lists/" + s.ID) } class="rounded-xl border border-gray-100 shadow-lg py-4 px-2">
<h2 class="font-semibold text-lg text-black p-2">{ s.CreatedDate.Format("02 Jan 06 15:04") }</h2>
</a>
}
</section>
<button
value="+ New list"
hx-post="/lists"
hx-target="body"
hx-push-url="true"
hx-swap="outerHTML"
class="absolute bg-white bottom-24 right-4 border border-gray-300 rounded-full shadow-lg py-2 px-3 text-gray-500"
>+ New list</button>
}
}