From e24e5eb4b2b264abf4bf57a64a1740a70c91810f Mon Sep 17 00:00:00 2001 From: Erik Date: Thu, 27 Nov 2025 16:59:02 -0300 Subject: [PATCH] feat: exibir ultimos projetos dinamicamente na home --- frontend/src/app/(public)/page.tsx | 106 ++++++++++++++++++++++++++--- 1 file changed, 98 insertions(+), 8 deletions(-) diff --git a/frontend/src/app/(public)/page.tsx b/frontend/src/app/(public)/page.tsx index 5fe594b..6582e53 100644 --- a/frontend/src/app/(public)/page.tsx +++ b/frontend/src/app/(public)/page.tsx @@ -1,11 +1,51 @@ "use client"; +import { useEffect, useState } from "react"; import Link from "next/link"; import { usePageContent } from "@/hooks/usePageContent"; +type PortfolioProject = { + id: string; + title: string; + category: string; + coverImage: string | null; + galleryImages: string[]; + status: string; + createdAt?: string; +}; + +type FallbackProject = { + id: string; + title: string; + category: string; + image: string; +}; + +const FALLBACK_PROJECTS: FallbackProject[] = [ + { + id: "fallback-1", + title: "Projeto de Adequação - Coca-Cola", + category: "Engenharia Veicular", + image: "https://images.unsplash.com/photo-1616401784845-180882ba9ba8?q=80&w=2070&auto=format&fit=crop", + }, + { + id: "fallback-2", + title: "Laudo de Guindaste Articulado", + category: "Inspeção Técnica", + image: "https://images.unsplash.com/photo-1581092335397-9583eb92d232?q=80&w=2070&auto=format&fit=crop", + }, + { + id: "fallback-3", + title: "Dispositivo de Içamento Especial", + category: "Projeto Mecânico", + image: "https://images.unsplash.com/photo-1504917595217-d4dc5ebe6122?q=80&w=2070&auto=format&fit=crop", + }, +]; + export default function Home() { // Português é o idioma padrão - busca diretamente sem tradução const { content, loading } = usePageContent('home', 'pt'); + const [latestProjects, setLatestProjects] = useState([]); // Usar conteúdo do banco ou fallback const hero = content?.hero || { @@ -68,6 +108,52 @@ export default function Home() { button: 'Fale Conosco' }; + useEffect(() => { + let isMounted = true; + const controller = new AbortController(); + + const fetchProjects = async () => { + try { + const response = await fetch('/api/projects', { + method: 'GET', + cache: 'no-store', + credentials: 'same-origin', + signal: controller.signal, + }); + + if (!response.ok) { + throw new Error(`Falha ao buscar projetos recentes (status ${response.status})`); + } + + const data = await response.json(); + + if (isMounted && Array.isArray(data)) { + const published = data + .filter((project: PortfolioProject) => project.status !== 'Rascunho') + .sort((a, b) => { + const dateA = a.createdAt ? new Date(a.createdAt).getTime() : 0; + const dateB = b.createdAt ? new Date(b.createdAt).getTime() : 0; + return dateB - dateA; + }) + .slice(0, 3); + + setLatestProjects(published); + } + } catch (err) { + if ((err as Error).name !== 'AbortError') { + console.error('Erro ao buscar projetos recentes:', err); + } + } + }; + + fetchProjects(); + + return () => { + isMounted = false; + controller.abort(); + }; + }, []); + return (
{/* Hero Section */} @@ -192,16 +278,20 @@ export default function Home() {
- {[ - { img: "https://images.unsplash.com/photo-1616401784845-180882ba9ba8?q=80&w=2070&auto=format&fit=crop", title: "Projeto de Adequação - Coca-Cola", cat: "Engenharia Veicular" }, - { img: "https://images.unsplash.com/photo-1581092335397-9583eb92d232?q=80&w=2070&auto=format&fit=crop", title: "Laudo de Guindaste Articulado", cat: "Inspeção Técnica" }, - { img: "https://images.unsplash.com/photo-1504917595217-d4dc5ebe6122?q=80&w=2070&auto=format&fit=crop", title: "Dispositivo de Içamento Especial", cat: "Projeto Mecânico" } - ].map((project, index) => ( -
-
+ {(latestProjects.length > 0 + ? latestProjects.map((project) => ({ + id: project.id, + title: project.title, + category: project.category, + image: project.coverImage || project.galleryImages?.[0] || FALLBACK_PROJECTS[0].image, + })) + : FALLBACK_PROJECTS + ).map((project) => ( +
+
- {project.cat} + {project.category}

{project.title}