Pre-select current $running when modifying it

Implements: https://todo.sr.ht/~amolith/willow/25
This commit is contained in:
Amolith 2023-11-28 20:01:26 -05:00
parent 61c551ade4
commit 8ca0376243
Signed by: Amolith
GPG Key ID: 8AE30347CE28D101
3 changed files with 41 additions and 24 deletions

View File

@ -196,12 +196,14 @@ func RefreshLoop(dbConn *sql.DB, interval int, manualRefresh, req *chan struct{}
// GetProject returns a project from the database // GetProject returns a project from the database
func GetProject(dbConn *sql.DB, url string) (Project, error) { func GetProject(dbConn *sql.DB, url string) (Project, error) {
var p Project
projectDB, err := db.GetProject(dbConn, url) projectDB, err := db.GetProject(dbConn, url)
if err != nil { if err != nil {
return p, err return Project{}, err
} }
p = Project{ if len(projectDB) == 0 {
return Project{}, nil
}
p := Project{
URL: projectDB["url"], URL: projectDB["url"],
Name: projectDB["name"], Name: projectDB["name"],
Forge: projectDB["forge"], Forge: projectDB["forge"],

View File

@ -24,8 +24,9 @@
<p>Which release of {{ .Name }} are you currently running?</p> <p>Which release of {{ .Name }} are you currently running?</p>
{{- $url := .URL -}} {{- $url := .URL -}}
{{- $forge := .Forge -}} {{- $forge := .Forge -}}
{{- $running := .Running -}}
{{- range .Releases -}} {{- range .Releases -}}
<input type="radio" id="{{ .Tag }}" name="release" value="{{ .Tag }}"> <input type="radio" id="{{ .Tag }}" name="release" value="{{ .Tag }}" {{- if eq $running .Tag }} checked {{- end -}}>
{{- if ne .URL "" -}} {{- if ne .URL "" -}}
<label for="{{ .Tag }}"><a href="{{ .URL }}">{{ .Tag }}</a></label><br> <label for="{{ .Tag }}"><a href="{{ .URL }}">{{ .Tag }}</a></label><br>
{{- else -}} {{- else -}}
@ -44,7 +45,7 @@
<input type="hidden" name="forge" value="{{ .Forge }}"> <input type="hidden" name="forge" value="{{ .Forge }}">
<input class="button" type="submit" formaction="/new" value="Track future releases"> <input class="button" type="submit" formaction="/new" value="Track future releases">
</form> </form>
<!-- Append these if they ever start limited RSS entries: `(eq $forge "gitea") (eq $forge "forgejo")` --> <!-- Append these if they ever start limiting RSS entries: `(eq $forge "gitea") (eq $forge "forgejo")` -->
{{- if or (eq $forge "github") -}} {{- if or (eq $forge "github") -}}
<small>Some RSS feeds (notably GitHub's) include a limited number of releases. If you don't see your version, please change the forge type to "Other".</small> <small>Some RSS feeds (notably GitHub's) include a limited number of releases. If you don't see your version, please change the forge type to "Other".</small>
{{- end -}} {{- end -}}

View File

@ -73,35 +73,48 @@ func (h Handler) NewHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
forge := bmStrict.Sanitize(params.Get("forge")) proj, err := project.GetProject(h.DbConn, submittedURL)
if forge == "" { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte("No forge provided")) _, err := w.Write([]byte(fmt.Sprintf("Error getting project: %s", err)))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
return return
} }
name := bmStrict.Sanitize(params.Get("name")) if proj.Running == "" {
if name == "" { forge := bmStrict.Sanitize(params.Get("forge"))
w.WriteHeader(http.StatusBadRequest) if forge == "" {
_, err := w.Write([]byte("No name provided")) w.WriteHeader(http.StatusBadRequest)
if err != nil { _, err := w.Write([]byte("No forge provided"))
fmt.Println(err) if err != nil {
fmt.Println(err)
}
return
} }
return
name := bmStrict.Sanitize(params.Get("name"))
if name == "" {
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte("No name provided"))
if err != nil {
fmt.Println(err)
}
return
}
proj = project.Project{
URL: submittedURL,
Name: name,
Forge: forge,
}
proj.URL = strings.TrimSuffix(proj.URL, ".git")
} }
proj := project.Project{ proj, err = project.GetReleases(h.DbConn, proj)
URL: submittedURL,
Name: name,
Forge: forge,
}
proj.URL = strings.TrimSuffix(proj.URL, ".git")
proj, err := project.GetReleases(h.DbConn, proj)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte(fmt.Sprintf("Error getting releases: %s", err))) _, err := w.Write([]byte(fmt.Sprintf("Error getting releases: %s", err)))
@ -110,6 +123,7 @@ func (h Handler) NewHandler(w http.ResponseWriter, r *http.Request) {
} }
return return
} }
tmpl := template.Must(template.ParseFS(fs, "static/select-release.html")) tmpl := template.Must(template.ParseFS(fs, "static/select-release.html"))
if err := tmpl.Execute(w, proj); err != nil { if err := tmpl.Execute(w, proj); err != nil {
fmt.Println(err) fmt.Println(err)