// Modal para configurar cada pregunta individual // Funcionalidades: // - Configuración específica según el tipo de pregunta // - Para títulos: solo contenido // - Para preguntas simples: texto de la pregunta // - Para preguntas con opciones: texto y lista de opciones // - Switch para hacer la pregunta obligatoria/opcional 'use client'; import { Button } from '@repo/shadcn/button'; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from '@repo/shadcn/dialog'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@repo/shadcn/form'; import { Input } from '@repo/shadcn/input'; import { Switch } from '@repo/shadcn/switch'; import { useEffect } from 'react'; import { useFieldArray, useForm } from 'react-hook-form'; import { QuestionType } from '../../schemas/survey'; import { Plus, Trash2 } from 'lucide-react'; interface QuestionConfigModalProps { isOpen: boolean; onClose: () => void; question: any; onSave: (config: any) => void; } export function QuestionConfigModal({ isOpen, onClose, question, onSave, }: QuestionConfigModalProps) { const form = useForm({ defaultValues: { content: '', question: '', required: false, options: [{ id: '1', text: '' }], }, }); const { fields, append, remove } = useFieldArray({ control: form.control, name: 'options', }); useEffect(() => { if (question) { form.reset({ content: question.content || '', question: question.question || '', required: question.required || false, options: question.options || [{ id: '1', text: '' }], }); } }, [question, form]); const handleSubmit = (data: any) => { const config = { ...question, ...data, }; // Remove options if not needed if (![ QuestionType.MULTIPLE_CHOICE, QuestionType.SINGLE_CHOICE, QuestionType.SELECT ].includes(question.type)) { delete config.options; } // Remove content if not a title if (question.type !== QuestionType.TITLE) { delete config.content; } onSave(config); }; const renderFields = () => { switch (question?.type) { case QuestionType.TITLE: return ( ( Contenido del Título )} /> ); case QuestionType.SIMPLE: return ( ( Pregunta )} /> ); case QuestionType.MULTIPLE_CHOICE: case QuestionType.SINGLE_CHOICE: case QuestionType.SELECT: return ( <> ( Pregunta )} />
Opciones
{fields.map((field, index) => (
( )} /> {fields.length > 1 && ( )}
))}
); } }; return (
Configuración de la pregunta de la encuesta
Configurar Pregunta
{renderFields()} {question?.type !== QuestionType.TITLE && ( (
Respuesta Obligatoria
)} /> )}
); }