export function useQuiz(slug: string) {
const [answers, setAnswers] = useState<Record<string, any>>({});
const [currentQuestion, setCurrentQuestion] = useState(0);
const handleAnswer = (questionId: string, answer: string | string[]) => {
setAnswers(prev => ({ ...prev, [questionId]: answer }));
// Passer à la question suivante
if (currentQuestion < quiz.questions.length - 1) {
setCurrentQuestion(prev => prev + 1);
} else {
// Dernière question : soumettre le quiz
submitQuiz();
}
};
const submitQuiz = async () => {
const response = await quizApi.submitResponse({
quizId: quiz.id,
answers,
email: emailCapture
});
// Récupérer les recommandations
const recommendations = await quizApi.getRecommendations(
quiz.id,
answers
);
setResults(recommendations);
};
return { quiz, currentQuestion, progress, handleAnswer };
}