import DataSortModal from '@/components/data-sort-modal'; import DeleteByInertia from '@/components/inertia/delete-modal'; import { Button } from '@/components/ui/button'; import { Card } from '@/components/ui/card'; import { Dialog, DialogClose, DialogContent, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog'; import { ScrollArea } from '@/components/ui/scroll-area'; import { TabsList, TabsTrigger } from '@/components/ui/tabs'; import { cn } from '@/lib/utils'; import { SharedData } from '@/types/global'; import { router, useForm, usePage } from '@inertiajs/react'; import { Pencil, Trash2 } from 'lucide-react'; import { useEffect, useState } from 'react'; import { Renderer } from 'richtor'; import 'richtor/styles'; import QuestionForm from './question-form'; const getQuestionTypes = (translate: any) => [ { value: 'single', label: translate.dashboard.single_choice, flag: false }, { value: 'multiple', label: translate.dashboard.multiple_choice, flag: false }, { value: 'boolean', label: translate.dashboard.true_false, flag: false }, ]; interface Props { title: string; handler: React.ReactNode; quiz: SectionQuiz; question?: QuizQuestion; } const QuestionQuestions = ({ title, handler, quiz, question }: Props) => { const [open, setOpen] = useState(false); const [questionType, setQuestionType] = useState(question ? 'add-question' : 'questions'); const { props } = usePage(); const { translate } = props; const { input, button, dashboard, frontend } = translate; const questionTypes = getQuestionTypes(translate); // Get the maximum sort value from quiz_questions const maxSort = quiz.quiz_questions.length > 0 ? Math.max(...quiz.quiz_questions.map((question) => question.sort)) : 0; // Parse the options and answers if they're strings const initialOptions = question?.options ? (typeof question.options === 'string' ? JSON.parse(question.options) : question.options) : []; const initialAnswer = question?.answer ? (typeof question.answer === 'string' ? JSON.parse(question.answer) : question.answer) : []; const { data, setData, post, put, reset, processing, errors } = useForm({ title: question?.title || '', type: question?.type || 'single', options: initialOptions, answer: initialAnswer, sort: question?.sort || maxSort + 1, section_quiz_id: quiz.id, }); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (question) { put(route('quiz.question.update', { id: question.id }), { onSuccess: () => setOpen(false), }); } else { post(route('quiz.question.store'), { onSuccess: () => { reset(); setOpen(false); }, }); } }; useEffect(() => { if (!open) { reset(); setQuestionType(question ? 'add-question' : 'questions'); } }, [open]); return ( {handler} {title}
{button.add_question} } /> {button.sort} } onOrderChange={(newOrder) => { router.post( route('quiz.question.sort'), { sortedData: newOrder, }, { preserveScroll: true }, ); }} renderContent={(item) => (
)} />
{quiz.quiz_questions.length > 0 ? ( quiz.quiz_questions.map((question: QuizQuestion) => (
} /> } />
)) ) : (

{frontend.no_results}

)}
); }; export default QuestionQuestions;