package progress import ( "cpone-dashboard/menu/auth" "cpone-dashboard/menu/projects" "html/template" "net/http" ) var tmpl *template.Template var basePath string func SetTemplates(t *template.Template) { tmpl = t } func SetBasePath(p string) { basePath = p } type pageData struct { Username string CurrentProject projects.ProjectItem Search string Status string Rows []ProgressRow FilteredRows []ProgressRow Summary ProgressSummary ValidatedPct int PublishedPct int } func Index(w http.ResponseWriter, r *http.Request) { username := auth.Username(r) mcuID := auth.SelectedProjectID(r) if mcuID == 0 { http.Redirect(w, r, basePath+"/projects", http.StatusSeeOther) return } project, ok, err := projects.GetUserProject(username, mcuID) if err != nil { http.Error(w, "query error", http.StatusInternalServerError) return } if !ok { http.Redirect(w, r, basePath+"/projects", http.StatusSeeOther) return } rows, err := GetProgressRows(mcuID) if err != nil { http.Error(w, "query error", http.StatusInternalServerError) return } summary := BuildProgressSummary(rows) search := r.URL.Query().Get("search") status := r.URL.Query().Get("status") filteredRows := FilterProgressRows(rows, search, status) t := tmpl if t == nil { http.Error(w, "template not ready", http.StatusInternalServerError) return } if err := t.ExecuteTemplate(w, "base", pageData{ Username: username, CurrentProject: project, Search: search, Status: status, Rows: rows, FilteredRows: filteredRows, Summary: summary, ValidatedPct: Pct(summary.Validated, summary.Total), PublishedPct: Pct(summary.Published, summary.Total), }); err != nil { http.Error(w, "template error", http.StatusInternalServerError) } }