"use client"; import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; interface SubmissionItem { id: string; createdAt: string; projectSlug: string; projectTitle: string | null; payload: Record; } type PageStatus = "idle" | "loading" | "error"; export default function AllProjectSubmissionsPage() { const router = useRouter(); const [submissions, setSubmissions] = useState([]); const [status, setStatus] = useState("idle"); const [errorMessage, setErrorMessage] = useState(null); useEffect(() => { let cancelled = false; const load = async () => { try { setStatus("loading"); setErrorMessage(null); const res = await fetch("/api/projects/submissions"); if (!res.ok) { if (cancelled) return; if (res.status === 401) { setStatus("error"); setErrorMessage("폼 제출 내역을 조회하려면 로그인이 필요합니다. 다시 로그인해 주세요."); router.push("/login"); return; } setStatus("error"); setErrorMessage("폼 제출 내역을 불러오는 중 오류가 발생했습니다. 잠시 후 다시 시도해 주세요."); return; } const data = (await res.json()) as SubmissionItem[]; if (!cancelled) { setSubmissions(Array.isArray(data) ? data : []); setStatus("idle"); setErrorMessage(null); } } catch { if (!cancelled) { setStatus("error"); setErrorMessage("폼 제출 내역을 불러오는 중 오류가 발생했습니다. 잠시 후 다시 시도해 주세요."); } } }; void load(); return () => { cancelled = true; }; }, []); const renderOtherFields = (payload: Record): string => { const knownKeys = new Set(["name", "email", "phone", "birthdate"]); const entries = Object.entries(payload).filter(([key]) => !knownKeys.has(key)); if (entries.length === 0) { return "-"; } return entries .map(([key, value]) => { const stringValue = typeof value === "string" ? value : JSON.stringify(value); return `${key}: ${stringValue}`; }) .join("\n"); }; return (

전체 폼 제출 내역

로그인한 계정의 모든 프로젝트에 대한 폼 제출 데이터를 확인할 수 있습니다.

{status === "error" && errorMessage && (

{errorMessage}

)} {status === "loading" && (

폼 제출 내역을 불러오는 중입니다...

)} {submissions.length === 0 && status === "idle" && !errorMessage && (

아직 수집된 폼 제출 내역이 없습니다.

)} {submissions.length > 0 && ( {submissions.map((item) => { const payload = (item.payload ?? {}) as Record; const nameValue = payload["name"]; const emailValue = payload["email"]; const phoneValue = payload["phone"]; const birthdateValue = payload["birthdate"]; const name = typeof nameValue === "string" ? nameValue : ""; const email = typeof emailValue === "string" ? emailValue : ""; const phone = typeof phoneValue === "string" ? phoneValue : ""; const birthdate = typeof birthdateValue === "string" ? birthdateValue : ""; return ( ); })}
제출 시각 프로젝트 Slug 이름 이메일 전화번호 생년월일 기타 필드
{new Date(item.createdAt).toLocaleString()} {item.projectTitle ?? "-"} {item.projectSlug} {name} {email} {phone} {birthdate} {renderOtherFields(payload)}
)}
); }