import { Check, X } from 'lucide-react';
interface AttemptAnswerWithQuestion extends ExamAttemptAnswer {
exam_question: ExamQuestion;
}
const QuestionAnswerResult = ({ question, answer }: { question: ExamQuestion; answer: AttemptAnswerWithQuestion }) => {
const answerData = answer.answer_data;
if (!answerData) {
return
No answer provided
;
}
switch (question.question_type) {
case 'multiple_choice': {
const selectedId = typeof answerData === 'object' ? (answerData as any).selected_option_id : answerData;
const options = question.question_options || [];
return (
Select the option that best matches:
{options.map((option: any, idx: number) => {
const isSelected = option.id === selectedId;
const isCorrect = option.is_correct;
return (
{isSelected ? (
isCorrect ? (
) : (
)
) : isCorrect ? (
) : (
)}
{option.option_text}
{isSelected &&
Selected Answer }
{isCorrect && !isSelected &&
(Correct Answer)}
);
})}
);
}
case 'multiple_select': {
const selectedIds = Array.isArray(answerData) ? answerData : (answerData as any).selected_option_ids || [];
const options = question.question_options || [];
return (
Select all statements that apply:
{options.map((option: any, idx: number) => {
const isSelected = selectedIds.includes(option.id);
const isCorrect = option.is_correct;
return (
{isSelected ? (
isCorrect ? (
) : (
)
) : isCorrect ? (
) : (
)}
{option.option_text}
{isSelected && isCorrect && (
(Selected Answer - Correct)
)}
{isSelected && !isCorrect &&
(Selected Answer - Wrong)}
{isCorrect && !isSelected && (
(Correct Answer - Not Selected)
)}
);
})}
);
}
case 'fill_blank': {
const userAnswers = Array.isArray(answerData) ? answerData : (answerData as any).answers || [];
const correctAnswers = question.options?.answers || [];
return (
Fill in the blanks:
{userAnswers.map((userAns: string, idx: number) => {
const correctOptions = correctAnswers;
const isCorrect = correctOptions.some((ans: string) => ans?.toLowerCase().trim() === userAns?.toLowerCase().trim());
return (
Student Answer:{' '}
{userAns || '(empty)'}
{correctAnswers.length > 0 && (
Correct Answer{correctAnswers.length > 1 ? 's' : ''}:{' '}
{correctAnswers.join(', ')}
)}
);
})}
);
}
case 'ordering': {
const userOrder = Array.isArray(answerData) ? answerData : (answerData as any).order || [];
const items = question.options?.items || [];
const correctOrder = question.options?.correct_order || [];
return (
Arrange in the correct order:
Your Order:
{userOrder.map((itemIndex: number, idx: number) => (
{idx + 1}
{items[itemIndex] || `Item ${itemIndex}`}
))}
Correct Order:
{correctOrder.map((itemIndex: number, idx: number) => (
{idx + 1}
{items[itemIndex] || `Item ${itemIndex}`}
))}
);
}
case 'matching': {
const userMatches = (answerData as any).matches || [];
const correctMatches = question.options?.matches || [];
return (
Match each item with its pair:
{userMatches.map((match: any, idx: number) => {
// Find the correct match definition
const correctMatch = correctMatches.find((cm: any) => cm.id === match.id);
const isCorrect = correctMatch && correctMatch.answer === match.answer;
return (
{isCorrect ? (
) : (
)}
{correctMatch?.question || 'Unknown'}
→ {match.answer || 'Unknown'}
{!isCorrect && correctMatch && (
Correct: {correctMatch.question} → {correctMatch.answer}
)}
);
})}
);
}
case 'short_answer':
case 'listening': {
const userAnswer = typeof answerData === 'string' ? answerData : (answerData as any).answer_text || JSON.stringify(answerData);
return (
);
}
default:
return (
Answer data:
{JSON.stringify(answerData, null, 2)}
);
}
};
export default QuestionAnswerResult;