feat(dashboard): #82 add chart for sum of account and savings
Some checks failed
Build Docker Image / Build-Docker-Image (push) Has been cancelled

This commit is contained in:
2025-06-19 17:49:07 +02:00
parent 65640aec3b
commit 96e9b4b036
12 changed files with 306 additions and 95 deletions

View File

@@ -1,16 +1,11 @@
package handler
import (
"fmt"
"log/slog"
"net/http"
"spend-sparrow/internal/handler/middleware"
"spend-sparrow/internal/service"
"spend-sparrow/internal/template"
"spend-sparrow/internal/template/dashboard"
"spend-sparrow/internal/types"
"spend-sparrow/internal/utils"
"time"
"github.com/a-h/templ"
)
@@ -21,14 +16,12 @@ type Index interface {
type IndexImpl struct {
r *Render
d *service.Dashboard
c service.Clock
}
func NewIndex(r *Render, d *service.Dashboard, c service.Clock) Index {
func NewIndex(r *Render, c service.Clock) Index {
return IndexImpl{
r: r,
d: d,
c: c,
}
}
@@ -54,11 +47,8 @@ func (handler IndexImpl) handleRootAnd404() http.HandlerFunc {
status = http.StatusNotFound
} else {
if user != nil {
var err error
comp, err = handler.dashboard(user, htmx, r)
if err != nil {
slog.ErrorContext(r.Context(), "Failed to get dashboard summary", "err", err)
}
utils.DoRedirect(w, r, "/dashboard")
return
} else {
comp = template.Index()
}
@@ -73,35 +63,8 @@ func (handler IndexImpl) handleRootAnd404() http.HandlerFunc {
}
}
func (handler IndexImpl) dashboard(user *types.User, htmx bool, r *http.Request) (templ.Component, error) {
var month time.Time
var err error
monthStr := r.URL.Query().Get("month")
if monthStr != "" {
month, err = time.Parse("2006-01-02", monthStr)
if err != nil {
return nil, fmt.Errorf("could not parse timestamp: %w", service.ErrBadRequest)
}
} else {
month = handler.c.Now()
}
summary, err := handler.d.Summary(r.Context(), user, month)
if err != nil {
return nil, err
}
if htmx {
return dashboard.DashboardData(summary), nil
} else {
return dashboard.Dashboard(summary), nil
}
}
func (handler IndexImpl) handleEmpty() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
updateSpan(r)
// Return nothing
// do nothing
}
}