sklad: prefill form with last values on error

Since the browser's back button cannot be used because of our
fascist caching policy.
This commit is contained in:
Přemysl Eric Janouch 2019-04-22 12:03:36 +02:00
parent 301d035425
commit 88560a8fbf
Signed by: p
GPG Key ID: A0420B94F92B9493
2 changed files with 34 additions and 19 deletions

View File

@ -36,18 +36,19 @@
</form> </form>
</header> </header>
<form method=post action="container?id={{ .Container.Id }}"> <form method=post action="container?id={{ .Container.Id }}">
<textarea name=description {{- $description := or .NewDescription .Container.Description }}
rows="{{ max 5 (lines .Container.Description) }}" <textarea name=description rows="{{ max 5 (lines $description) }}"
placeholder="Popis obalu nebo jeho obsahu"> placeholder="Popis obalu nebo jeho obsahu">
{{- .Container.Description -}} {{- $description -}}
</textarea> </textarea>
<footer> <footer>
<div> <div>
<label for=series>Řada:</label> <label for=series>Řada:</label>
<select name=series id=series> <select name=series id=series>
{{- $preselect := or .NewSeries .Container.Series }}
{{- range $prefix, $desc := .AllSeries }} {{- range $prefix, $desc := .AllSeries }}
<option value="{{ $prefix }}" <option value="{{ $prefix }}"
{{ if eq $prefix $.Container.Series }}selected{{ end -}} {{ if eq $prefix $preselect }}selected{{ end -}}
>{{ $prefix }} &mdash; {{ $desc }}</option> >{{ $prefix }} &mdash; {{ $desc }}</option>
{{- end }} {{- end }}
</select> </select>
@ -55,7 +56,7 @@
<div> <div>
<label for=parent>Nadobal:</label> <label for=parent>Nadobal:</label>
<input type=text name=parent id=parent <input type=text name=parent id=parent
value="{{ .Container.Parent }}"> value="{{ or .NewParent .Container.Parent }}">
</div> </div>
<input type=submit value="Uložit"> <input type=submit value="Uložit">
</footer> </footer>
@ -69,21 +70,27 @@
<h2>Nový obal</h2> <h2>Nový obal</h2>
</header> </header>
<form method=post action="container"> <form method=post action="container">
<textarea name=description rows=5 {{- $description := or .NewDescription "" }}
placeholder="Popis obalu nebo jeho obsahu"></textarea> <textarea name=description rows="{{ max 5 (lines $description) }}"
placeholder="Popis obalu nebo jeho obsahu">
{{- $description -}}
</textarea>
<footer> <footer>
<div> <div>
<label for=series>Řada:</label> <label for=series>Řada:</label>
<select name=series id=series> <select name=series id=series>
{{- $preselect := or .NewSeries "" }}
{{- range $prefix, $desc := .AllSeries }} {{- range $prefix, $desc := .AllSeries }}
<option value="{{ $prefix }}" <option value="{{ $prefix }}"
{{ if eq $prefix $preselect }}selected{{ end -}}
>{{ $prefix }} &mdash; {{ $desc }}</option> >{{ $prefix }} &mdash; {{ $desc }}</option>
{{- end }} {{- end }}
</select> </select>
</div> </div>
<div> <div>
<label for=parent>Nadobal:</label> <label for=parent>Nadobal:</label>
<input type=text name=parent id=parent value=""> <input type=text name=parent id=parent
value="{{ or .NewParent "" }}">
</div> </div>
<input type=submit value="Uložit"> <input type=submit value="Uložit">
</footer> </footer>

View File

@ -123,7 +123,6 @@ func handleContainer(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, redirect, http.StatusSeeOther) http.Redirect(w, r, redirect, http.StatusSeeOther)
return return
} }
// TODO: Use the last data as a prefill.
} else if r.Method != http.MethodGet { } else if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed) w.WriteHeader(http.StatusMethodNotAllowed)
return return
@ -134,14 +133,6 @@ func handleContainer(w http.ResponseWriter, r *http.Request) {
allSeries[s.Prefix] = s.Description allSeries[s.Prefix] = s.Description
} }
var container *Container
children := indexChildren[""]
if c, ok := indexContainer[ContainerId(shownId)]; ok {
children = c.Children()
container = c
}
params := struct { params := struct {
Error error Error error
ErrorNoSuchSeries bool ErrorNoSuchSeries bool
@ -152,6 +143,9 @@ func handleContainer(w http.ResponseWriter, r *http.Request) {
ErrorWouldContainItself bool ErrorWouldContainItself bool
ErrorContainerInUse bool ErrorContainerInUse bool
Container *Container Container *Container
NewDescription *string
NewSeries string
NewParent *string
Children []*Container Children []*Container
AllSeries map[string]string AllSeries map[string]string
}{ }{
@ -163,10 +157,24 @@ func handleContainer(w http.ResponseWriter, r *http.Request) {
ErrorCannotChangeNumber: err == errCannotChangeNumber, ErrorCannotChangeNumber: err == errCannotChangeNumber,
ErrorWouldContainItself: err == errWouldContainItself, ErrorWouldContainItself: err == errWouldContainItself,
ErrorContainerInUse: err == errContainerInUse, ErrorContainerInUse: err == errContainerInUse,
Container: container, Children: indexChildren[""],
Children: children,
AllSeries: allSeries, AllSeries: allSeries,
} }
if c, ok := indexContainer[ContainerId(shownId)]; ok {
params.Children = c.Children()
params.Container = c
}
if description, ok := r.Form["description"]; ok {
params.NewDescription = &description[0]
}
if series, ok := r.Form["series"]; ok {
// It seems impossible to dereference strings in text/template so that
// `eq` can be used, and we don't actually need a null value here.
params.NewSeries = series[0]
}
if parent, ok := r.Form["parent"]; ok {
params.NewParent = &parent[0]
}
executeTemplate("container.tmpl", w, &params) executeTemplate("container.tmpl", w, &params)
} }