Redirect to correct summary page if interval cookie is set

This adds an additional 302 redirect when the user doesn't specify an
`interval` as a query param, but has the `wakapi_summary_interval`
cookie set.
This commit is contained in:
Daste 2022-12-03 12:46:06 +01:00
parent ebe1836ac6
commit 97fab3e109
1 changed files with 6 additions and 3 deletions

View File

@ -45,13 +45,16 @@ func (h *SummaryHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
rawQuery := r.URL.RawQuery
q := r.URL.Query()
if q.Get("interval") == "" && q.Get("from") == "" {
// If the PersistentIntervalKey cookie is set, redirect to the correct summary page
if intervalCookie, _ := r.Cookie(models.PersistentIntervalKey); intervalCookie != nil {
q.Set("interval", intervalCookie.Value)
} else {
q.Set("interval", "today")
redirectAddress := fmt.Sprintf("%s/summary?interval=%s", h.config.Server.BasePath, intervalCookie.Value)
http.Redirect(w, r, redirectAddress, http.StatusFound)
}
q.Set("interval", "today")
r.URL.RawQuery = q.Encode()
} else if q.Get("interval") != "" {
// Send a Set-Cookie header to persist the interval
headerValue := fmt.Sprintf("%s=%s", models.PersistentIntervalKey, q.Get("interval"))
w.Header().Add("Set-Cookie", headerValue)
}