diff --git a/src/app/dashboard/[entity]/page.tsx b/src/app/dashboard/[entity]/page.tsx index 8b83b82..bcecf13 100644 --- a/src/app/dashboard/[entity]/page.tsx +++ b/src/app/dashboard/[entity]/page.tsx @@ -1,5 +1,5 @@ import { APPWRITE_ALMEIRA_CONFIG, endpoint, projectId } from '@/lib/appwrite'; -import { createAdminClient, createSessionClient, getLoggedInUser } from '@/lib/appwrite.server'; +import { createAdminClient, getLoggedInUser } from '@/lib/appwrite.server'; import { Query } from 'node-appwrite'; import Link from 'next/link'; import { collectionMap, SCHEMA_MAP } from '@/lib/schema'; @@ -8,107 +8,7 @@ import { getUserPreferences } from '@/lib/preferences'; import { ErrorBoundary } from '@/components/publications/ErrorBoundary'; import { saveEntityAction } from '@/app/actions/entities'; import type { Post, AuthUser } from '@/types/publications'; - -// ───────────────────────────────────────────── -// POSTS‑SPECIFIC LAYOUT (card grid + drawer) -// ───────────────────────────────────────────── - -/** - * Renders the publications module with PublicationsList (cards + drawer) - * instead of the generic DataTable, when entity === 'posts'. - */ -async function PostsPage({ - entity, - user, -}: { - entity: string; - user: AuthUser; -}) { - // Lazy import server-side only - const { PublicationsList } = await import('@/components/publications/PublicationsList'); - - const collectionId = collectionMap[entity]; - let posts: Post[] = []; - let relationData: Record> = {}; - - try { - const client = await createAdminClient(); - - // Fetch all published + draft + pending posts - const res = await client.databases.listDocuments( - APPWRITE_ALMEIRA_CONFIG.databaseId, - collectionId, - [Query.orderDesc('$createdAt'), Query.limit(100)] - ); - - // Deep-clone/sanitize to avoid "Only plain objects..." Next.js error - posts = JSON.parse(JSON.stringify(res.documents)) as Post[]; - - // Pre-load relation data for the drawer/form - const fields = SCHEMA_MAP[entity] || []; - await Promise.all( - fields - .filter((f) => f.type === 'relation' && f.relationCollection) - .map(async (field) => { - const relKey = field.relationCollection as string; - const relColId = collectionMap[relKey]; - if (!relColId) return; - try { - const relRes = await client.databases.listDocuments( - APPWRITE_ALMEIRA_CONFIG.databaseId, - relColId, - [Query.limit(100)] - ); - relationData[field.name] = relRes.documents as Array<{ $id: string; [key: string]: unknown }>; - } catch { - // Fail silently for individual relation fetches - } - }) - ); - - // Sanitize relationData too - relationData = JSON.parse(JSON.stringify(relationData)); - } catch (error) { - console.error('PostsPage fetch error:', error); - } - - const boundEditAction = saveEntityAction.bind(null, entity); - - return ( -
- {/* Page Header */} -
-
-

- Publicaciones -

-

- {posts.length} registros en el sistema -

-
-
- - - - -
- ); -} - -// ───────────────────────────────────────────── -// GENERIC ENTITY PAGE (DataTable) -// ───────────────────────────────────────────── +import { PublicationsList } from '@/components/publications/PublicationsList'; export default async function EntityPage({ params, @@ -123,7 +23,7 @@ export default async function EntityPage({ const page = Number(sParams.page) || 1; const sort = sParams.sort || '$createdAt'; const dir = sParams.dir === 'asc' ? 'asc' : 'desc'; - const limit = 15; + const limit = entity === 'posts' ? 12 : 15; const offset = (page - 1) * limit; const collectionId = collectionMap[entity]; @@ -151,31 +51,21 @@ export default async function EntityPage({ const prefs = await getUserPreferences(); const hiddenColumns = prefs.hiddenColumns?.[entity] || []; - // Redirect the 'posts' entity to the specialized card view - if (entity === 'posts') { - return ; - } - - // Generic DataTable for all other entities - let documents: Array> = []; + let documents: any[] = []; let total = 0; + let relationData: Record = {}; try { - const sessionClient = await createSessionClient(); - if (!sessionClient) throw new Error('No sesión activa'); - + const client = await createAdminClient(); const queries = [Query.limit(limit), Query.offset(offset)]; - if (dir === 'asc') { - queries.push(Query.orderAsc(sort)); - } else { - queries.push(Query.orderDesc(sort)); - } + if (dir === 'asc') queries.push(Query.orderAsc(sort)); + else queries.push(Query.orderDesc(sort)); if (q) { const searchableFields = fieldsConfig - .filter((f) => f.type === 'text' || f.type === 'textarea') - .map((f) => f.name) + .filter((f: any) => f.type === 'text' || f.type === 'textarea') + .map((f: any) => f.name) .slice(0, 2); if (searchableFields.length > 0) { @@ -183,18 +73,41 @@ export default async function EntityPage({ } } - const response = await sessionClient.databases.listDocuments( + const response = await client.databases.listDocuments( APPWRITE_ALMEIRA_CONFIG.databaseId, collectionId, queries ); - // Deep-clone/sanitize documents to avoid "Only plain objects..." Next.js error + documents = JSON.parse(JSON.stringify(response.documents)); total = response.total; + + if (entity === 'posts') { + const fields = SCHEMA_MAP[entity] || []; + await Promise.all( + fields + .filter((f) => f.type === 'relation' && f.relationCollection) + .map(async (field) => { + const relKey = field.relationCollection as string; + const relColId = collectionMap[relKey]; + if (!relColId) return; + try { + const relRes = await client.databases.listDocuments( + APPWRITE_ALMEIRA_CONFIG.databaseId, + relColId, + [Query.limit(100)] + ); + relationData[field.name] = JSON.parse(JSON.stringify(relRes.documents)); + } catch { } + }) + ); + } } catch (error) { - console.error(`Error fetching from ${collectionId}:`, error); + console.error(`Error fetching entity ${entity}:`, error); } + const boundEditAction = saveEntityAction.bind(null, entity); + return (
@@ -228,18 +141,34 @@ export default async function EntityPage({
- + {entity === 'posts' ? ( + + + + ) : ( + + )} - {/* Pagination */} {total > limit && (
ANTERIOR - + Página {page} de {Math.ceil(total / limit)}