diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index d41d547..63ae741 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -14,12 +14,36 @@ import ( "vkane.cz/tinyquiz/pkg/rtcomm" ) -func (app *application) home(w http.ResponseWriter, r *http.Request, params httprouter.Params) { +func (app *application) homeSuccess(w http.ResponseWriter, r *http.Request, params httprouter.Params) { + app.home(w, r, homeForm{}, http.StatusOK) +} + +type homeForm struct { + Join struct { + Code string + Name string + Errors []string + } + NewSession struct { + Code string + Name string + Errors []string + } + NewGame struct { + Title string + Name string + Errors []string + } +} + +func (app *application) home(w http.ResponseWriter, r *http.Request, formData homeForm, status int) { type homeData struct { Stats model.Stats + Form homeForm templateData } td := &homeData{} + td.Form = formData setDefaultTemplateData(&td.templateData) if stats, err := app.model.GetStats(r.Context()); err == nil { @@ -28,7 +52,7 @@ func (app *application) home(w http.ResponseWriter, r *http.Request, params http app.serverError(w, err) return } - + w.WriteHeader(status) app.render(w, r, "home.page.tmpl.html", td) } @@ -40,9 +64,13 @@ func (app *application) play(w http.ResponseWriter, r *http.Request, params http var code = strings.TrimSpace(params.ByName("code")) var player = strings.ToLower(strings.TrimSpace(r.PostForm.Get("player"))) + var form homeForm + form.Join.Code = code + form.Join.Name = player if len(player) < 1 { - app.clientError(w, http.StatusBadRequest) + form.Join.Errors = []string{"Zadejte jméno hráče"} + app.home(w, r, form, http.StatusBadRequest) return } @@ -61,10 +89,12 @@ func (app *application) play(w http.ResponseWriter, r *http.Request, params http http.Redirect(w, r, "/game/"+url.PathEscape(player.ID.String()), http.StatusSeeOther) return } else if errors.Is(err, model.NoSuchEntity) { - app.clientError(w, http.StatusNotFound) + form.Join.Errors = []string{"Hra s tímto kódem nebyla nalezena"} + app.home(w, r, form, http.StatusNotFound) return } else if errors.Is(err, model.ConstraintViolation) { - app.clientError(w, http.StatusForbidden) + form.Join.Errors = []string{"Hráč s tímto jménem již existuje"} + app.home(w, r, form, http.StatusForbidden) return } else { app.serverError(w, err) @@ -81,9 +111,13 @@ func (app *application) createSession(w http.ResponseWriter, r *http.Request, pa var code = strings.TrimSpace(r.PostForm.Get("code")) var player = strings.ToLower(strings.TrimSpace(r.PostForm.Get("organiser"))) + var form homeForm + form.NewSession.Code = code + form.NewSession.Name = player if len(player) < 1 { - app.clientError(w, http.StatusBadRequest) + form.NewSession.Errors = []string{"Zadejte jméno organizátora"} + app.home(w, r, form, http.StatusBadRequest) return } @@ -97,7 +131,8 @@ func (app *application) createSession(w http.ResponseWriter, r *http.Request, pa return } } else if errors.Is(err, model.NoSuchEntity) { - app.clientError(w, http.StatusNotFound) + form.NewSession.Errors = []string{"Hra s tímto kódem nebyla nalezena"} + app.home(w, r, form, http.StatusNotFound) return } else { app.serverError(w, err) @@ -253,9 +288,13 @@ func (app *application) createGame(w http.ResponseWriter, r *http.Request, param } var name = r.PostFormValue("name") var author = r.PostFormValue("author") + var form homeForm + form.NewGame.Title = name + form.NewGame.Name = author file, _, err := r.FormFile("game") if err != nil { - app.clientError(w, http.StatusBadRequest) + form.NewGame.Errors = []string{"Nahrajte soubor s otázkami"} + app.home(w, r, form, http.StatusBadRequest) return } @@ -267,11 +306,9 @@ func (app *application) createGame(w http.ResponseWriter, r *http.Request, param app.serverError(w, err) return } - } else if errors.Is(err, gameCreator.ErrInvalidSyntax) || errors.Is(err, gameCreator.ErrTooManyQuestions) || errors.Is(err, gameCreator.ErrTooManyChoices) { - app.clientError(w, http.StatusBadRequest) - return } else { - app.clientError(w, http.StatusBadRequest) + form.NewGame.Errors = []string{"Soubor s otázkami není v pořádku"} + app.home(w, r, form, http.StatusBadRequest) return } } diff --git a/cmd/web/main.go b/cmd/web/main.go index 06789d6..78538ff 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -101,7 +101,7 @@ func main() { }() mux := httprouter.New() - mux.GET("/", app.home) + mux.GET("/", app.homeSuccess) mux.POST("/play/:code", app.play) mux.POST("/session", app.createSession) mux.GET("/game/:playerUid", app.game) diff --git a/ui/html/home.page.tmpl.html b/ui/html/home.page.tmpl.html index 99696e7..a3dd500 100644 --- a/ui/html/home.page.tmpl.html +++ b/ui/html/home.page.tmpl.html @@ -22,28 +22,49 @@

Připojit se ke hře

-
- - - -
+ {{- with .Form.Join }} + {{- with .Errors }} + + {{- end }} +
+ + + +
+ {{- end }}

Zorganizovat novou hru

-
- - - -
+ {{- with .Form.NewSession }} + {{- with .Errors }} + + {{- end }} +
+ + + +
+ {{- end }}
-

Vytvořit nový kvíz

-

Stáhnout šablonu nového kvízu

-
- - - - -
+ {{- with .Form.NewGame }} + {{- with .Errors }} + + {{- end }} +

Vytvořit nový kvíz

+

Stáhnout šablonu nového kvízu

+
+ + + + +
+ {{- end }}
{{ end -}} diff --git a/ui/static/home.css b/ui/static/home.css index ac0187e..2abafa8 100644 --- a/ui/static/home.css +++ b/ui/static/home.css @@ -16,3 +16,8 @@ section { display: flex; flex-direction: column; } + +.error { + color: red; + font-weight: bold; +}