diff --git a/src/pages/student-dashboard/StudentDashboardPage.tsx b/src/pages/student-dashboard/StudentDashboardPage.tsx index a7a5a0b..0a09d0b 100644 --- a/src/pages/student-dashboard/StudentDashboardPage.tsx +++ b/src/pages/student-dashboard/StudentDashboardPage.tsx @@ -14,7 +14,6 @@ interface DashboardMetrics { export function StudentDashboardPage() { const navigate = useNavigate(); const [student, setStudent] = React.useState(null); - const [stories, setStories] = React.useState([]); const [metrics, setMetrics] = React.useState({ totalStories: 0, averageReadingFluency: 0, @@ -54,18 +53,16 @@ export function StudentDashboardPage() { .limit(6); if (storiesError) throw storiesError; - setStories(storiesData || []); - // Calcular métricas - // Em produção: Implementar cálculos reais baseados nos dados + // Calcular métricas (exemplo simplificado) setMetrics({ totalStories: storiesData?.length || 0, - averageReadingFluency: 85, // Exemplo - totalReadingTime: 120, // Exemplo: 120 minutos - currentLevel: 3 // Exemplo + averageReadingFluency: 85, + totalReadingTime: 120, + currentLevel: 3 }); - // Buscar histórias recentes com a primeira página como capa + // Buscar histórias recentes com a capa definida const { data, error } = await supabase .from('stories') .select(` @@ -80,7 +77,26 @@ export function StudentDashboardPage() { .limit(6); if (error) throw error; - setRecentStories(data || []); + + // Transformar os dados para definir a propriedade 'cover' de forma robusta + const mappedData = (data || []).map(story => { + let coverObj = null; + if (story.cover) { + coverObj = Array.isArray(story.cover) ? story.cover[0] : story.cover; + } else if (story.pages && story.pages.length > 0) { + coverObj = story.pages[0]; + } + + if (coverObj && typeof coverObj === 'object' && coverObj.image_url) { + return { ...story, cover: coverObj }; + } else if (coverObj && typeof coverObj === 'string') { + return { ...story, cover: { image_url: coverObj } }; + } + + return { ...story, cover: null }; + }); + + setRecentStories(mappedData); } catch (err) { console.error('Erro ao carregar dashboard:', err); diff --git a/src/pages/student-dashboard/StudentStoriesPage.tsx b/src/pages/student-dashboard/StudentStoriesPage.tsx index 66e463e..2e05687 100644 --- a/src/pages/student-dashboard/StudentStoriesPage.tsx +++ b/src/pages/student-dashboard/StudentStoriesPage.tsx @@ -42,7 +42,7 @@ export function StudentStoriesPage() { if (error) throw error; // Aplicar ordenação - const sortedData = (data || []).sort((a, b) => { + let sortedData = (data || []).sort((a, b) => { switch (sortBy) { case 'oldest': return new Date(a.created_at).getTime() - new Date(b.created_at).getTime(); @@ -55,6 +55,12 @@ export function StudentStoriesPage() { } }); + // Adicionar a propriedade 'cover', usando a imagem da primeira página + sortedData = sortedData.map(story => ({ + ...story, + cover: story.pages && story.pages.length > 0 ? story.pages[0] : null + })); + setStories(sortedData); } catch (err) { console.error('Erro ao buscar histórias:', err);