refactor(router): one WeekStatsData mapper for the dashboard page and week-widget fragment

This commit is contained in:
John O'Keefe committed 2026-10-17 16:54:21 -04:00
1 parent 77cf0b827b
commit 87fc89a816
1 file changed
+20 -20
+20 -20
View File
@@ -468,16 +468,8 @@ func registerFrontendRoutes(cfg *Config) {
weekSvc, weekErr := cfg.StatisticsService.ThisWeek(c.Request().Context(),
pgtype.UUID{Bytes: userUUID, Valid: true}, user.Timezone, weekScope)
if weekErr == nil {
week = &templates.WeekStatsData{
WeekMinutes: weekSvc.WeekMinutes,
WeekPages: weekSvc.WeekPages,
WeekSessions: weekSvc.WeekSessions,
WeekBooks: weekSvc.WeekBooks,
PrevMinutes: weekSvc.PrevMinutes,
ActiveToday: weekSvc.ActiveToday,
CurrentStreak: weekSvc.CurrentStreak,
LibraryID: libRes.LibraryID,
}
wk := weekStatsData(weekSvc, libRes.LibraryID)
week = &wk
} else {
log.Printf("ThisWeek widget failed: %v", weekErr)
}
@@ -816,16 +808,8 @@ func registerFrontendRoutes(cfg *Config) {
}
}
}
err = templates.DashboardWeekWidget(templates.WeekStatsData{
WeekMinutes: weekSvc.WeekMinutes,
WeekPages: weekSvc.WeekPages,
WeekSessions: weekSvc.WeekSessions,
WeekBooks: weekSvc.WeekBooks,
PrevMinutes: weekSvc.PrevMinutes,
ActiveToday: weekSvc.ActiveToday,
CurrentStreak: weekSvc.CurrentStreak,
LibraryID: libRes.LibraryID,
}, widgetLibType).Render(c.Request().Context(), &buf)
err = templates.DashboardWeekWidget(weekStatsData(weekSvc, libRes.LibraryID),
widgetLibType).Render(c.Request().Context(), &buf)
if err != nil {
return err
}
@@ -1680,3 +1664,19 @@ func (cfg *Config) GetHealth(c *echo.Context) error {
},
})
}
// weekStatsData maps the statistics weekly aggregate to the widget's
// template shape (shared by the dashboard page and the HTMX
// week-widget fragment).
func weekStatsData(weekSvc services.WeekSummaryStats, libraryID string) templates.WeekStatsData {
return templates.WeekStatsData{
WeekMinutes: weekSvc.WeekMinutes,
WeekPages: weekSvc.WeekPages,
WeekSessions: weekSvc.WeekSessions,
WeekBooks: weekSvc.WeekBooks,
PrevMinutes: weekSvc.PrevMinutes,
ActiveToday: weekSvc.ActiveToday,
CurrentStreak: weekSvc.CurrentStreak,
LibraryID: libraryID,
}
}