adding cakes 50% working
This commit is contained in:
parent
a09cee09c8
commit
cd7c9a9d0c
8 changed files with 57 additions and 38 deletions
11
README.md
11
README.md
|
|
@ -1,9 +1,12 @@
|
|||
# iroon
|
||||
## todo:
|
||||
- extract order creation form into it's own element
|
||||
- option to add additional cakes
|
||||
- saving orders on server
|
||||
- displaying orders
|
||||
- [ ] extract order creation form into it's own element
|
||||
- [x] option to add additional cakes
|
||||
- [x] saving orders on server
|
||||
- [ ] displaying orders
|
||||
- [ ] move id to cake struct
|
||||
- [ ] refresh the list on submission
|
||||
- [ ] peristent storage
|
||||
## ux design
|
||||
### adding an order:
|
||||
1. user selects the _add order_ button
|
||||
|
|
|
|||
|
|
@ -6,8 +6,14 @@ import (
|
|||
"net/http"
|
||||
)
|
||||
|
||||
type Cake struct {
|
||||
Name string
|
||||
Price int
|
||||
}
|
||||
|
||||
type controller struct {
|
||||
tmpl map[string]*template.Template
|
||||
tmpl map[string]*template.Template
|
||||
cakes map[int]Cake
|
||||
http.Handler
|
||||
}
|
||||
|
||||
|
|
|
|||
54
handlers.go
54
handlers.go
|
|
@ -8,39 +8,37 @@ import (
|
|||
)
|
||||
|
||||
func (c *controller) cakeOptions(w http.ResponseWriter, r *http.Request) {
|
||||
type cake struct {
|
||||
Name string
|
||||
ID int
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
template := strings.Split(r.URL.String(), "/")[1]
|
||||
c.tmpl[template].Execute(w, struct{ Cakes []cake }{Cakes: []cake{{"Hello", 1}, {"World", 2}}})
|
||||
c.tmpl[template].Execute(w, c.cakes)
|
||||
}
|
||||
|
||||
func (c *controller) cakeEditor(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("received get request at ", r.URL.String())
|
||||
type cake struct {
|
||||
Name string
|
||||
ID int
|
||||
Price int
|
||||
type contents struct {
|
||||
ID int
|
||||
Cake Cake
|
||||
}
|
||||
|
||||
url := strings.Split(r.URL.String(), "/")
|
||||
|
||||
if len(url) < 3 {
|
||||
fmt.Println("no arg")
|
||||
c.tmpl["cake_editor"].Execute(w, cake{"", 0, 0})
|
||||
c.tmpl["cake_editor"].Execute(w, contents{-1, Cake{"", 0}})
|
||||
return
|
||||
}
|
||||
|
||||
id, err := strconv.Atoi(url[2])
|
||||
if err != nil {
|
||||
fmt.Println("can't get info")
|
||||
c.tmpl["cake_editor"].Execute(w, cake{"", 0, 0})
|
||||
c.tmpl["cake_editor"].Execute(w, contents{0, Cake{"", 0}})
|
||||
return
|
||||
}
|
||||
|
||||
c.tmpl["cake_editor"].Execute(w, cake{"Hello", id, 120})
|
||||
cake, exists := c.cakes[id]
|
||||
if !exists {
|
||||
c.tmpl["cake_editor"].Execute(w, contents{0, Cake{"", 0}})
|
||||
return
|
||||
}
|
||||
|
||||
out := contents{id, cake}
|
||||
c.tmpl["cake_editor"].Execute(w, out)
|
||||
}
|
||||
|
||||
func (c *controller) newCake(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -50,24 +48,30 @@ func (c *controller) newCake(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
type cake struct {
|
||||
Name string
|
||||
ID int
|
||||
Price int
|
||||
type contents struct {
|
||||
ID int
|
||||
Cake Cake
|
||||
}
|
||||
var out cake
|
||||
out.Name = r.FormValue("name")
|
||||
out.Price, err = strconv.Atoi(r.FormValue("price"))
|
||||
|
||||
var in contents
|
||||
in.Cake.Name = r.FormValue("name")
|
||||
in.Cake.Price, err = strconv.Atoi(r.FormValue("price"))
|
||||
if err != nil {
|
||||
c.tmpl["alert"].Execute(w, err)
|
||||
return
|
||||
}
|
||||
out.ID, err = strconv.Atoi(r.FormValue("id"))
|
||||
in.ID, err = strconv.Atoi(r.FormValue("id"))
|
||||
if err != nil {
|
||||
c.tmpl["alert"].Execute(w, err)
|
||||
return
|
||||
}
|
||||
_ = c.tmpl["cake"].Execute(w, out)
|
||||
|
||||
if in.ID == -1 {
|
||||
in.ID = len(c.cakes)
|
||||
}
|
||||
|
||||
c.cakes[in.ID] = in.Cake
|
||||
_ = c.tmpl["cake"].Execute(w, in)
|
||||
}
|
||||
|
||||
func (c *controller) postNewOrder(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
|||
4
main.go
4
main.go
|
|
@ -19,6 +19,10 @@ func main() {
|
|||
var c controller
|
||||
c.tmpl = LoadTemplates(templates)
|
||||
c.LoadRouter(public)
|
||||
c.cakes = make(map[int]Cake)
|
||||
c.cakes[0] = Cake{"Sernik", 120}
|
||||
c.cakes[1] = Cake{"Malinowa chmórka", 120}
|
||||
c.cakes[2] = Cake{"Beza Pavlova", 8}
|
||||
|
||||
err := http.ListenAndServe(":8080", c)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -2,8 +2,9 @@
|
|||
<h3>Cakes</h3>
|
||||
<button hx-get="edit_cakes" hx-swap="outerHTML" hx-target="closest div">edit</button>
|
||||
<div id="cakes">
|
||||
{{range .Cakes}}
|
||||
<button onclick="addCake('{{.ID}}', '{{.Name}}')">{{.Name}}</button>
|
||||
{{range $key, $value := .}}
|
||||
<button onclick="addCake('{{$key}}', '{{$value.Name}} ({{$value.Price}})')">{{$value.Name}}
|
||||
{{$value.Price}}</button>
|
||||
{{end}}
|
||||
</div>
|
||||
<script>
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
<div id="cake_preview">
|
||||
<ul>
|
||||
<li>ID: {{.ID}}</li>
|
||||
<li>Name: {{.Name}}</li>
|
||||
<li>Price: {{.Price}}</li>
|
||||
<li>Name: {{.Cake.Name}}</li>
|
||||
<li>Price: {{.Cake.Price}}</li>
|
||||
</ul>
|
||||
<button type="button" onClick="document.getElementById('cake_preview').remove()">close</button>
|
||||
</div>
|
||||
|
|
@ -1,11 +1,11 @@
|
|||
<form hx-post="new_cake" hx-swap="outerHTML">
|
||||
<div>
|
||||
<label for="name">Name</label>
|
||||
<input type="text" id="name" name="name" required value="{{.Name}}">
|
||||
<input type="text" id="name" name="name" required value="{{.Cake.Name}}">
|
||||
</div>
|
||||
<div>
|
||||
<label for="price">Price</label>
|
||||
<input type="number" id="price" name="price" step="0.01" min="0" required value="{{.Price}}">
|
||||
<input type="number" id="price" name="price" step="0.01" min="0" required value="{{.Cake.Price}}">
|
||||
</div>
|
||||
<input value="{{.ID}}" name="id" hidden></input>
|
||||
<button type="submit">Submit</button>
|
||||
|
|
|
|||
|
|
@ -3,8 +3,9 @@
|
|||
<button hx-get="available_cakes" hx-swap="outerHTML" hx-target="closest div">back</button>
|
||||
<button hx-get="cake_editor" hx-swap="innerHTML" hx-target="#cake_editor">New</button>
|
||||
<div id="cakes">
|
||||
{{range .Cakes}}
|
||||
<button hx-get="cake_editor/{{.ID}}" hx-swap="innerHTML" hx-target="#cake_editor">{{.Name}}</button>
|
||||
{{range $key, $value := .}}
|
||||
<button hx-get="cake_editor/{{$key}}" hx-swap="innerHTML" hx-target="#cake_editor">{{$value.Name}}
|
||||
{{$value.Price}}</button>
|
||||
{{end}}
|
||||
</div>
|
||||
<div id="cake_editor"></div>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue