From 7eb84cd937ad65b0941164c5efe6b35a1210f8c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Sun, 14 Apr 2019 01:05:05 +0200 Subject: [PATCH] sklad: styling, add a basic view for containers --- sklad/base.tmpl | 25 ++++++++++++++++---- sklad/container.tmpl | 23 ++++++++++++++++++- sklad/login.tmpl | 2 ++ sklad/main.go | 54 ++++++++++++++++++++++++++++++++------------ 4 files changed, 84 insertions(+), 20 deletions(-) diff --git a/sklad/base.tmpl b/sklad/base.tmpl index 2070d6e..b8487ee 100644 --- a/sklad/base.tmpl +++ b/sklad/base.tmpl @@ -7,18 +7,33 @@ html, body { min-height: 100vh; } body { padding: 1em; box-sizing: border-box; margin: 0 auto; max-width: 50em; - border-left: 1px solid gray; border-right: 1px solid gray; + border-left: 1px solid #ccc; border-right: 1px solid #ccc; font-family: sans-serif; } + header { display: flex; justify-content: space-between; align-items: center; + flex-wrap: wrap; margin: -1em -1em 0 -1em; padding: 0 1em; + background: linear-gradient(0deg, #fff, #eee); } + header * { display: inline-block; } + a { color: inherit; } -

sklad

+ +
+

sklad

{{ if .LoggedIn }} -
- -
+ Obaly + Řady + +
+ +
+ +
+ +
{{ end }} +
{{ template "Content" . }} diff --git a/sklad/container.tmpl b/sklad/container.tmpl index 341d19b..b261496 100644 --- a/sklad/container.tmpl +++ b/sklad/container.tmpl @@ -1,6 +1,27 @@ {{ define "Title" }}Přehled{{ end }} {{ define "Content" }} -

TODO +{{ if .Id }} +

{{ .Id }}

+{{ else }} +

Obaly nejvyšší úrovně

+{{ end }} + +{{ if .Description }} +

{{ .Description }} +{{ end }} + +{{ if .Children }} +{{ range .Children }} +

+

{{ .Id }}

+{{ if .Description }} +

{{ .Description }} +{{ end }} +

+{{ end }} +{{ else }} +

Obal je prázdný. +{{ end }} {{ end }} diff --git a/sklad/login.tmpl b/sklad/login.tmpl index 4df6c3a..8dbca84 100644 --- a/sklad/login.tmpl +++ b/sklad/login.tmpl @@ -1,6 +1,8 @@ {{ define "Title" }}Přihlášení{{ end }} {{ define "Content" }} +

Přihlášení

+
diff --git a/sklad/main.go b/sklad/main.go index 3ceea9f..dee8723 100644 --- a/sklad/main.go +++ b/sklad/main.go @@ -11,22 +11,31 @@ import ( "time" ) -var ( - templates = map[string]*template.Template{} -) +var templates = map[string]*template.Template{} +// TODO: Consider wrapping the data object in something that always contains +// a LoggedIn member, so that we don't need to duplicate it. func executeTemplate(name string, w io.Writer, data interface{}) { if err := templates[name].Execute(w, data); err != nil { panic(err) } } -func handleLogin(w http.ResponseWriter, r *http.Request) { - if err := r.ParseForm(); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return +func wrap(inner func(http.ResponseWriter, *http.Request)) func( + http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + if err := r.ParseForm(); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + if r.Method == http.MethodGet { + w.Header().Set("Cache-Control", "no-store") + } + inner(w, r) } +} +func handleLogin(w http.ResponseWriter, r *http.Request) { redirect := r.FormValue("redirect") if redirect == "" { redirect = "/" @@ -45,7 +54,7 @@ func handleLogin(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: - w.Header().Set("Cache-Control", "no-store") + // We're just going to render the template. case http.MethodPost: if r.FormValue("password") == db.Password { session.LoggedIn = true @@ -78,10 +87,27 @@ func handleContainer(w http.ResponseWriter, r *http.Request) { return } + children := []*Container{} + id := ContainerId(r.FormValue("id")) + description := "" + + if id == "" { + children = db.Containers + } else if container, ok := indexContainer[id]; ok { + children = indexChildren[id] + description = container.Description + } + params := struct { - LoggedIn bool + LoggedIn bool + Id ContainerId + Description string + Children []*Container }{ - LoggedIn: true, + LoggedIn: true, + Id: id, + Description: description, + Children: children, } executeTemplate("container.tmpl", w, ¶ms) @@ -127,11 +153,11 @@ func main() { // - https://stackoverflow.com/a/33880971/76313 // - POST /label?id=UA1 - http.HandleFunc("/", sessionWrap(handleContainer)) - http.HandleFunc("/container", sessionWrap(handleContainer)) + http.HandleFunc("/", sessionWrap(wrap(handleContainer))) + http.HandleFunc("/container", sessionWrap(wrap(handleContainer))) - http.HandleFunc("/login", handleLogin) - http.HandleFunc("/logout", sessionWrap(handleLogout)) + http.HandleFunc("/login", wrap(handleLogin)) + http.HandleFunc("/logout", sessionWrap(wrap(handleLogout))) log.Fatalln(http.ListenAndServe(address, nil)) }