diff --git a/cmd/server/main.go b/cmd/server/main.go index 0155d87..899cc1f 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -331,7 +331,10 @@ func setupRouter(db *gorm.DB, cfg *config.Config, local *localdb.LocalDB, dbUser exportHandler := handlers.NewExportHandler(exportService, configService, componentService) pricingHandler := handlers.NewPricingHandler(db, pricingService, alertService, componentRepo, priceRepo, statsRepo) pricelistHandler := handlers.NewPricelistHandler(pricelistService, local) - syncHandler := handlers.NewSyncHandler(local, syncService, db) + syncHandler, err := handlers.NewSyncHandler(local, syncService, db, "web/templates") + if err != nil { + return nil, nil, fmt.Errorf("creating sync handler: %w", err) + } // Setup handler (for reconfiguration) setupHandler, err := handlers.NewSetupHandler(local, "web/templates") @@ -418,6 +421,7 @@ func setupRouter(db *gorm.DB, cfg *config.Config, local *localdb.LocalDB, dbUser partials := router.Group("/partials") { partials.GET("/components", webHandler.ComponentsPartial) + partials.GET("/sync-status", syncHandler.SyncStatusPartial) } // API routes diff --git a/internal/handlers/sync.go b/internal/handlers/sync.go index f9a844c..bebe4ab 100644 --- a/internal/handlers/sync.go +++ b/internal/handlers/sync.go @@ -1,8 +1,10 @@ package handlers import ( + "html/template" "log/slog" "net/http" + "path/filepath" "time" "github.com/gin-gonic/gin" @@ -16,15 +18,24 @@ type SyncHandler struct { localDB *localdb.LocalDB syncService *sync.Service mariaDB *gorm.DB + tmpl *template.Template } // NewSyncHandler creates a new sync handler -func NewSyncHandler(localDB *localdb.LocalDB, syncService *sync.Service, mariaDB *gorm.DB) *SyncHandler { +func NewSyncHandler(localDB *localdb.LocalDB, syncService *sync.Service, mariaDB *gorm.DB, templatesPath string) (*SyncHandler, error) { + // Load sync_status partial template + partialPath := filepath.Join(templatesPath, "partials", "sync_status.html") + tmpl, err := template.ParseFiles(partialPath) + if err != nil { + return nil, err + } + return &SyncHandler{ localDB: localDB, syncService: syncService, mariaDB: mariaDB, - } + tmpl: tmpl, + }, nil } // SyncStatusResponse represents the sync status @@ -270,3 +281,24 @@ func (h *SyncHandler) GetPendingChanges(c *gin.Context) { "changes": changes, }) } + +// SyncStatusPartial renders the sync status partial for htmx +// GET /partials/sync-status +func (h *SyncHandler) SyncStatusPartial(c *gin.Context) { + // Check online status + isOffline, _ := c.Get("is_offline") + + // Get pending count + pendingCount := h.localDB.GetPendingCount() + + data := gin.H{ + "IsOffline": isOffline.(bool), + "PendingCount": pendingCount, + } + + c.Header("Content-Type", "text/html; charset=utf-8") + if err := h.tmpl.ExecuteTemplate(c.Writer, "sync_status", data); err != nil { + slog.Error("failed to render sync_status template", "error", err) + c.String(http.StatusInternalServerError, "Template error") + } +} diff --git a/web/templates/base.html b/web/templates/base.html index ca7692f..0be305b 100644 --- a/web/templates/base.html +++ b/web/templates/base.html @@ -26,8 +26,11 @@