Validaciones mandatorias — Sección 8 — IDs concretos
This commit is contained in:
50
app/page.tsx
50
app/page.tsx
@@ -1,21 +1,27 @@
|
||||
import { createClient } from '@/lib/supabase'
|
||||
import type { Question, Survey } from '@/lib/types'
|
||||
import type { BrandConfig, Question, Survey } from '@/lib/types'
|
||||
import { SurveyForm } from '@/components/survey-form'
|
||||
|
||||
// Force dynamic rendering — page fetches from Supabase at request time.
|
||||
// Never statically generated: credentials live in .env.local (runtime only).
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
// Server Component — fetches survey with anon key; RLS ensures only status='live' is returned.
|
||||
export default async function Page() {
|
||||
// In Next.js 15, searchParams is a Promise and must be awaited.
|
||||
interface PageProps {
|
||||
searchParams: Promise<{ s?: string }>
|
||||
}
|
||||
|
||||
export default async function Page({ searchParams }: PageProps) {
|
||||
const { s: surveyId } = await searchParams
|
||||
const supabase = createClient()
|
||||
|
||||
// 1. Fetch the live survey
|
||||
const { data: surveyRow, error: surveyError } = await supabase
|
||||
// 1. Fetch survey — by ?s=UUID if provided, otherwise first live survey
|
||||
const surveyQuery = supabase
|
||||
.from('surveys')
|
||||
.select('id, title, status, is_anonymous, k_threshold')
|
||||
.eq('status', 'live')
|
||||
.maybeSingle()
|
||||
.select('id, org_id, title, status, is_anonymous, k_threshold, consent_version_id, brand_config')
|
||||
|
||||
const { data: surveyRow, error: surveyError } = surveyId
|
||||
? await surveyQuery.eq('id', surveyId).maybeSingle()
|
||||
: await surveyQuery.eq('status', 'live').maybeSingle()
|
||||
|
||||
if (surveyError) {
|
||||
return (
|
||||
@@ -32,10 +38,11 @@ export default async function Page() {
|
||||
<div className="no-survey" role="main">
|
||||
<h1 className="no-survey-title">No hay encuesta activa</h1>
|
||||
<p>
|
||||
Para ver el formulario, la encuesta debe estar en estado <code>live</code>.
|
||||
Para ver el formulario, la encuesta debe estar en estado <code>live</code>,
|
||||
o pasá el UUID por parámetro: <code>?s=UUID</code>.
|
||||
</p>
|
||||
<pre className="no-survey-hint">
|
||||
{`-- Activar la encuesta en Supabase Cloud:\nUPDATE surveys\nSET status = 'live'\nWHERE id = '30000000-0000-0000-0000-000000000001';`}
|
||||
{`-- Activar en Supabase Cloud:\nUPDATE surveys SET status = 'live'\nWHERE id = '30000000-0000-0000-0000-000000000001';`}
|
||||
</pre>
|
||||
</div>
|
||||
)
|
||||
@@ -61,20 +68,19 @@ export default async function Page() {
|
||||
|
||||
const questions = questionRows as Question[]
|
||||
|
||||
// 3. Log question count for validation (visible in server logs and browser Network tab)
|
||||
console.log(
|
||||
`[2A] Survey loaded: id=${surveyRow.id} | questions=${questions.length}`
|
||||
)
|
||||
console.log(`[2B] Survey loaded: id=${surveyRow.id} | questions=${questions.length}`)
|
||||
|
||||
const survey: Survey = {
|
||||
...(surveyRow as Omit<Survey, 'questions'>),
|
||||
id: surveyRow.id,
|
||||
org_id: surveyRow.org_id,
|
||||
title: surveyRow.title,
|
||||
status: surveyRow.status,
|
||||
is_anonymous: surveyRow.is_anonymous,
|
||||
k_threshold: surveyRow.k_threshold,
|
||||
consent_version_id: surveyRow.consent_version_id,
|
||||
brand_config: (surveyRow.brand_config as BrandConfig) ?? null,
|
||||
questions,
|
||||
}
|
||||
|
||||
return (
|
||||
<SurveyForm
|
||||
survey={survey}
|
||||
questions={questions}
|
||||
/>
|
||||
)
|
||||
return <SurveyForm survey={survey} questions={questions} />
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user