Improve mobile UI and add infinite scroll
This commit is contained in:
@@ -5,12 +5,15 @@ import (
|
||||
"cpone-dashboard/menu/projects"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var tmpl *template.Template
|
||||
var pdfBaseURL string
|
||||
var basePath string
|
||||
|
||||
const defaultPageSize = 30
|
||||
|
||||
func SetTemplates(t *template.Template) { tmpl = t }
|
||||
func SetPDFBaseURL(u string) { pdfBaseURL = u }
|
||||
func SetBasePath(p string) { basePath = p }
|
||||
@@ -24,51 +27,108 @@ type pageData struct {
|
||||
FilteredRows []ResultRow
|
||||
Summary ResultSummary
|
||||
PDFBaseURL string
|
||||
Page int
|
||||
PageSize int
|
||||
HasMore bool
|
||||
VisibleCount int
|
||||
TotalFiltered int
|
||||
NextPage int
|
||||
}
|
||||
|
||||
func Index(w http.ResponseWriter, r *http.Request) {
|
||||
func parsePage(q string) int {
|
||||
p, err := strconv.Atoi(q)
|
||||
if err != nil || p < 1 {
|
||||
return 1
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func buildResultData(r *http.Request) (pageData, int, error) {
|
||||
username := auth.Username(r)
|
||||
mcuID := auth.SelectedProjectID(r)
|
||||
if mcuID == 0 {
|
||||
http.Redirect(w, r, basePath+"/projects", http.StatusSeeOther)
|
||||
return
|
||||
return pageData{}, http.StatusSeeOther, nil
|
||||
}
|
||||
project, ok, err := projects.GetUserProject(username, mcuID)
|
||||
if err != nil {
|
||||
http.Error(w, "query error", http.StatusInternalServerError)
|
||||
return
|
||||
return pageData{}, http.StatusInternalServerError, err
|
||||
}
|
||||
if !ok {
|
||||
http.Redirect(w, r, basePath+"/projects", http.StatusSeeOther)
|
||||
return
|
||||
return pageData{}, http.StatusSeeOther, nil
|
||||
}
|
||||
|
||||
rows, err := GetResultRows(mcuID)
|
||||
if err != nil {
|
||||
http.Error(w, "query error", http.StatusInternalServerError)
|
||||
return
|
||||
return pageData{}, http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
summary := BuildResultSummary(rows)
|
||||
search := r.URL.Query().Get("search")
|
||||
filter := r.URL.Query().Get("filter")
|
||||
filteredRows := FilterResultRows(rows, search, filter)
|
||||
|
||||
t := tmpl
|
||||
if t == nil {
|
||||
http.Error(w, "template not ready", http.StatusInternalServerError)
|
||||
return
|
||||
page := parsePage(r.URL.Query().Get("page"))
|
||||
pagedRows, hasMore := PaginateResultRows(filteredRows, page, defaultPageSize)
|
||||
visibleCount := page * defaultPageSize
|
||||
if visibleCount > len(filteredRows) {
|
||||
visibleCount = len(filteredRows)
|
||||
}
|
||||
if err := t.ExecuteTemplate(w, "base", pageData{
|
||||
|
||||
return pageData{
|
||||
Username: username,
|
||||
CurrentProject: project,
|
||||
Search: search,
|
||||
Filter: filter,
|
||||
Rows: rows,
|
||||
FilteredRows: filteredRows,
|
||||
FilteredRows: pagedRows,
|
||||
Summary: summary,
|
||||
PDFBaseURL: pdfBaseURL,
|
||||
}); err != nil {
|
||||
Page: page,
|
||||
PageSize: defaultPageSize,
|
||||
HasMore: hasMore,
|
||||
VisibleCount: visibleCount,
|
||||
TotalFiltered: len(filteredRows),
|
||||
NextPage: page + 1,
|
||||
}, http.StatusOK, nil
|
||||
}
|
||||
|
||||
func Index(w http.ResponseWriter, r *http.Request) {
|
||||
t := tmpl
|
||||
if t == nil {
|
||||
http.Error(w, "template not ready", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
data, status, err := buildResultData(r)
|
||||
if err != nil {
|
||||
http.Error(w, "query error", status)
|
||||
return
|
||||
}
|
||||
if status == http.StatusSeeOther {
|
||||
http.Redirect(w, r, basePath+"/projects", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
if err := t.ExecuteTemplate(w, "base", data); err != nil {
|
||||
http.Error(w, "template error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func List(w http.ResponseWriter, r *http.Request) {
|
||||
t := tmpl
|
||||
if t == nil {
|
||||
http.Error(w, "template not ready", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
data, status, err := buildResultData(r)
|
||||
if err != nil {
|
||||
http.Error(w, "query error", status)
|
||||
return
|
||||
}
|
||||
if status == http.StatusSeeOther {
|
||||
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
if err := t.ExecuteTemplate(w, "result-list-chunk", data); err != nil {
|
||||
http.Error(w, "template error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user