loading all templates automatically
This commit is contained in:
parent
9f858d50ae
commit
f7498f4ef1
13 changed files with 128 additions and 105 deletions
|
|
@ -1,4 +1,9 @@
|
|||
# iroon
|
||||
## todo:
|
||||
- extract order creation form into it's own element
|
||||
- option to add additional cakes
|
||||
- saving orders on server
|
||||
- displaying orders
|
||||
## ux design
|
||||
### adding an order:
|
||||
1. user selects the _add order_ button
|
||||
|
|
|
|||
22
controller.go
Normal file
22
controller.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type controller struct {
|
||||
tmpl map[string]*template.Template
|
||||
http.Handler
|
||||
}
|
||||
|
||||
func (c *controller) LoadRouter(pub fs.FS) {
|
||||
serveMux := http.NewServeMux()
|
||||
|
||||
serveMux.Handle("GET /", http.FileServerFS(unwrap(fs.Sub(pub, "public"))))
|
||||
serveMux.HandleFunc("POST /order/new", c.postNewOrder)
|
||||
serveMux.HandleFunc("GET /cake/options", c.cakeOptions)
|
||||
|
||||
c.Handler = serveMux
|
||||
}
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
package controller
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (c *Controller) cakeOptions(w http.ResponseWriter, _ *http.Request) {
|
||||
type cake struct {
|
||||
Name string
|
||||
ID int
|
||||
}
|
||||
//w.Header().Set("Content-Type", "text/html")
|
||||
c.tmpl["cake/options"].Execute(w, struct{ Cakes []cake }{Cakes: []cake{{"Hello", 1}, {"World", 2}}})
|
||||
}
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
package controller
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
tmpl map[string]*template.Template
|
||||
http.Handler
|
||||
}
|
||||
|
||||
// #todo: load all automatically
|
||||
func (c *Controller) LoadTemplates(fs fs.FS) {
|
||||
c.tmpl = make(map[string]*template.Template)
|
||||
c.tmpl["order/confirmation"], _ = template.ParseFS(fs, "templates/order/confirmation.html")
|
||||
c.tmpl["cake/options"], _ = template.ParseFS(fs, "templates/cake/options.html")
|
||||
}
|
||||
|
||||
func (c *Controller) LoadRouter(pub fs.FS) {
|
||||
serveMux := http.NewServeMux()
|
||||
|
||||
serveMux.Handle("GET /", http.FileServerFS(unwrap(fs.Sub(pub, "public"))))
|
||||
serveMux.HandleFunc("POST /order/new", c.postNewOrder)
|
||||
serveMux.HandleFunc("GET /cake/options", c.cakeOptions)
|
||||
|
||||
c.Handler = serveMux
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (c *Controller) postNewOrder(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("received Post request: ", r)
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
_ = c.tmpl["order/confirmation"].Execute(w, struct{ Status any }{Status: err})
|
||||
}
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
fmt.Println(r.Form)
|
||||
_ = c.tmpl["order/confirmation"].Execute(w, struct{ Status any }{Status: r.Form})
|
||||
}
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
package controller
|
||||
|
||||
func unwrap[T any](output T, err error) T {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return output
|
||||
}
|
||||
24
handlers.go
Normal file
24
handlers.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (c *controller) cakeOptions(w http.ResponseWriter, _ *http.Request) {
|
||||
type cake struct {
|
||||
Name string
|
||||
ID int
|
||||
}
|
||||
c.tmpl["available_cakes"].Execute(w, struct{ Cakes []cake }{Cakes: []cake{{"Hello", 1}, {"World", 2}}})
|
||||
}
|
||||
|
||||
func (c *controller) postNewOrder(w http.ResponseWriter, r *http.Request) {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
_ = c.tmpl["confirm_order"].Execute(w, struct{ Status any }{Status: err})
|
||||
}
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
fmt.Println(r.Form)
|
||||
_ = c.tmpl["confirm_order"].Execute(w, struct{ Status any }{Status: r.Form})
|
||||
}
|
||||
5
main.go
5
main.go
|
|
@ -6,7 +6,6 @@ import (
|
|||
|
||||
"net/http"
|
||||
|
||||
"github.com/Bronku/iroon/controller"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
|
|
@ -17,8 +16,8 @@ var public embed.FS
|
|||
var templates embed.FS
|
||||
|
||||
func main() {
|
||||
var c controller.Controller
|
||||
c.LoadTemplates(templates)
|
||||
var c controller
|
||||
c.tmpl = LoadTemplates(templates)
|
||||
c.LoadRouter(public)
|
||||
|
||||
err := http.ListenAndServe(":8080", c)
|
||||
|
|
|
|||
|
|
@ -14,39 +14,44 @@
|
|||
<h1>I ran out of names</h1>
|
||||
</header>
|
||||
<main>
|
||||
<form x-show="open" hx-post="order/new" hx-swap="innerHTML" hx-target="#response" id="new_cake_form">
|
||||
<div>
|
||||
<label for="firstname">First Name</label>
|
||||
<input type="text" id="firstname" name="firstname" required value="">
|
||||
</div>
|
||||
<div>
|
||||
<label for="lastname">Last Name</label>
|
||||
<input type="text" id="lastname" name="lastname" value="">
|
||||
</div>
|
||||
<div>
|
||||
<label for="phone">Phone</label>
|
||||
<input type="tel" id="phone" name="phone" value="">
|
||||
</div>
|
||||
<div>
|
||||
<label for="location">Location</label>
|
||||
<input type="text" id="location" name="location" value="">
|
||||
</div>
|
||||
<div>
|
||||
<label for="paid">Paid</label>
|
||||
<input type="number" id="paid" name="paid" step="0.01" min="0" value="">
|
||||
</div>
|
||||
<div id="selected_cakes"></div>
|
||||
<button id="submit_button" type="submit">Submit</button>
|
||||
<button id="reset_button" type="reset">Reset</button>
|
||||
<script>
|
||||
document.getElementById("reset_button").addEventListener("click", function () {
|
||||
document.getElementById("selected_cakes").innerHTML = "";
|
||||
document.querySelectorAll("#dropdown option").forEach(option => option.disabled = false);
|
||||
})
|
||||
</script>
|
||||
</form>
|
||||
<div hx-get="cake/options" hx-swap="outerHTML" hx-trigger="load" hx-target="closest div"></div>
|
||||
<div id="response"></div>
|
||||
<div id="new_order_form">
|
||||
<form x-show="open" hx-post="order/new" hx-swap="innerHTML" hx-target="#new_order_response"
|
||||
id="new_cake_form">
|
||||
<div>
|
||||
<label for="firstname">First Name</label>
|
||||
<input type="text" id="firstname" name="firstname" required value="">
|
||||
</div>
|
||||
<div>
|
||||
<label for="lastname">Last Name</label>
|
||||
<input type="text" id="lastname" name="lastname" value="">
|
||||
</div>
|
||||
<div>
|
||||
<label for="phone">Phone</label>
|
||||
<input type="tel" id="phone" name="phone" value="">
|
||||
</div>
|
||||
<div>
|
||||
<label for="location">Location</label>
|
||||
<input type="text" id="location" name="location" value="">
|
||||
</div>
|
||||
<div>
|
||||
<label for="paid">Paid</label>
|
||||
<input type="number" id="paid" name="paid" step="0.01" min="0" value="">
|
||||
</div>
|
||||
<div id="selected_cakes"></div>
|
||||
<button id="submit_button" type="submit">Submit</button>
|
||||
<button id="reset_button" type="reset">Reset</button>
|
||||
<script>
|
||||
document.getElementById("reset_button").addEventListener("click", function () {
|
||||
document.getElementById("selected_cakes").innerHTML = "";
|
||||
document.querySelectorAll("#dropdown option").forEach(option => option.disabled = false);
|
||||
})
|
||||
</script>
|
||||
</form>
|
||||
<!--A dropdown with all available cakes loaded dynamically-->
|
||||
<div hx-get="cake/options" hx-swap="outerHTML" hx-trigger="load" hx-target="closest div"></div>
|
||||
<!--A space to fit the response after submitting the form-->
|
||||
<div id="new_order_response"></div>
|
||||
</div>
|
||||
</main>
|
||||
</body>
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,6 @@
|
|||
{{.Status}}
|
||||
<script>
|
||||
document.getElementById("close_button").addEventListener("click", function () {
|
||||
document.getElementById("response").innerHTML = ""
|
||||
document.getElementById("new_order_response").innerHTML = ""
|
||||
})
|
||||
</script>
|
||||
36
util.go
Normal file
36
util.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func unwrap[T any](output T, err error) T {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
func LoadTemplates(fs fs.ReadDirFS) map[string]*template.Template {
|
||||
files, err := fs.ReadDir("templates")
|
||||
if err != nil {
|
||||
fmt.Println("can't read the templates directory")
|
||||
return nil
|
||||
}
|
||||
tmpl := make(map[string]*template.Template)
|
||||
for _, e := range files {
|
||||
if e.IsDir() {
|
||||
continue
|
||||
}
|
||||
name := e.Name()
|
||||
name = strings.Split(name, ".")[0]
|
||||
tmpl[name], err = template.ParseFS(fs, "templates/"+name+".html")
|
||||
if err != nil {
|
||||
fmt.Println("can't open template: " + name)
|
||||
}
|
||||
}
|
||||
return tmpl
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue