feat(router): SSR libraries and users on /admin/library page
Update the /admin/library route handler to fetch and pass data to template for server-side rendering, improving page load performance. Changes: - Fetch all libraries using ListLibrariesData() helper - Fetch all users for visibility management - Convert database rows to template types (LibraryData, User) - Pass data to AdminLibrary template for SSR - Follows existing pattern from dashboard and custom-section pages Benefits: - Faster initial page load (no AJAX fetch) - Better UX (content visible immediately) - Progressive enhancement (works without JS)
This commit is contained in:
@@ -413,8 +413,47 @@ func registerFrontendRoutes(cfg *Config) {
|
||||
if err != nil {
|
||||
return renderErrorPage(c, "Error loading user", "user_load_error")
|
||||
}
|
||||
|
||||
// Fetch all libraries (admin-only)
|
||||
libraries, err := cfg.LibraryHandler.ListLibrariesData(c.Request().Context())
|
||||
if err != nil {
|
||||
log.Printf("ListLibrariesData failed: %v", err)
|
||||
libraries = []database.ListLibrariesRow{}
|
||||
}
|
||||
|
||||
// Convert to template LibraryData
|
||||
libData := make([]templates.LibraryData, len(libraries))
|
||||
for i, lib := range libraries {
|
||||
libUUID, _ := uuid.FromBytes(lib.ID.Bytes[0:16])
|
||||
libData[i] = templates.LibraryData{
|
||||
ID: libUUID.String(),
|
||||
Name: lib.Name,
|
||||
Description: getText(lib.Description),
|
||||
TypeName: lib.TypeName,
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch all users for visibility management
|
||||
users, err := cfg.Queries.ListUsers(c.Request().Context())
|
||||
if err != nil {
|
||||
log.Printf("ListUsers failed: %v", err)
|
||||
users = []database.ListUsersRow{}
|
||||
}
|
||||
|
||||
// Convert to template User types
|
||||
userData := make([]templates.User, len(users))
|
||||
for i, u := range users {
|
||||
userUUID, _ := uuid.FromBytes(u.ID.Bytes[0:16])
|
||||
userData[i] = templates.User{
|
||||
ID: userUUID.String(),
|
||||
Username: u.Username,
|
||||
Email: u.Email,
|
||||
Role: u.Role,
|
||||
}
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
err = templates.AdminLibrary(user).Render(c.Request().Context(), &buf)
|
||||
err = templates.AdminLibrary(user, libData, userData).Render(c.Request().Context(), &buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user