"use client"; import { useState, useEffect } from "react"; import { Project } from "@/types/models"; import { Button, buttonVariants } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import * as z from "zod"; const projectSchema = z.object({ title: z.string().min(3, "Title must be at least 3 characters"), description: z.string().min(10, "Description must be at least 10 characters"), github: z.string().url("Invalid URL").optional(), stack: z.string().array(), }); export default function Projects() { const [projects, setProjects] = useState([]); const [stackInput, setStackInput] = useState(""); const [loading, setLoading] = useState(true); const [open, setOpen] = useState(false); const form = useForm>({ resolver: zodResolver(projectSchema), defaultValues: { title: "", description: "", github: "", stack: [], }, }); const { setValue } = form; const fetchProjects = async () => { try { setLoading(true); const res = await fetch("/api/projects"); if (!res.ok) throw new Error("HTTP status " + res.status); const data = await res.json(); setProjects(data); setLoading(false); } catch (err) { console.error("Failed to fetch projects:", err); } }; useEffect(() => { fetchProjects(); }, []); async function handleSubmit(values: z.infer) { console.log(values); try { console.log(); const res = await fetch("/api/projects", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(values), }); if (!res.ok) throw new Error("HTTP status " + res.status); const newProject = await res.json(); setProjects([...projects, newProject]); setOpen(false); } catch (err) { console.error("Failed to create project:", err); } } const keyHandler = (e: React.KeyboardEvent) => { if (e.key === "Enter" || e.key === "Tab") { e.preventDefault(); setValue("stack", [...form.getValues("stack"), stackInput]); setStackInput(""); } }; return (

Projects

Open Add Project
( Project Title )} /> ( Project Description )} /> ( Github Repository URL )} /> ( Tech Stack setStackInput(e.target.value)} onKeyDown={keyHandler} /> )} />
{loading ? (

Loading...

) : (
{projects.map((project) => (

{project.title}

{project.description}

))}
)}
); }