feat(handlers): Add GetDevicesData helper and update devices template for SSR

- Add GetDevicesData() to DeviceHandler (returns raw data, not JSON)
- Update devices template signature to accept pre-rendered data
- Add server-side rendering of devices and pending registrations
- Update JavaScript to use location.reload() after CRUD operations
- Remove getDeviceIcon dependency on JavaScript function
- Use templ if/else instead of ternary operators for device status

Preserves all API endpoints and backward compatibility
This commit is contained in:
2026-01-31 22:42:21 -05:00
parent 86eaee5a25
commit b3c0c0c225
2 changed files with 158 additions and 160 deletions
+40
View File
@@ -272,6 +272,46 @@ func (h *DeviceHandler) ListDevices(c echo.Context) error {
})
}
func (h *DeviceHandler) GetDevicesData(c echo.Context) ([]DeviceInfo, error) {
userID := c.Get("user_id").(string)
userUUID, err := uuid.Parse(userID)
if err != nil {
return nil, err
}
pgUserID := pgtype.UUID{Bytes: [16]byte(userUUID), Valid: true}
devices, err := h.db.ListDevicesByUser(c.Request().Context(), pgUserID)
if err != nil {
return nil, err
}
deviceList := make([]DeviceInfo, 0, len(devices))
for _, device := range devices {
syncEnabled := device.SyncEnabled.Bool && device.SyncEnabled.Valid
autoSync := device.AutoSync.Bool && device.AutoSync.Valid
syncFreq := int32(0)
if device.SyncFrequencyMinutes.Valid {
syncFreq = device.SyncFrequencyMinutes.Int32
}
deviceList = append(deviceList, DeviceInfo{
ID: device.ID.Bytes,
DeviceName: device.DeviceName,
DeviceType: device.DeviceType,
LastSync: (*time.Time)(&device.LastSync.Time),
LastSeen: (*time.Time)(&device.LastSeen.Time),
SyncEnabled: syncEnabled,
AutoSync: autoSync,
SyncFrequency: syncFreq,
CreatedAt: device.CreatedAt.Time,
DeviceMetadata: device.DeviceMetadata,
})
}
return deviceList, nil
}
func (h *DeviceHandler) GetDevice(c echo.Context) error {
userID := c.Get("user_id")
if userID == nil {