1+ // /create/page.tsx
12"use client" ;
23
34import { useState } from "react" ;
@@ -6,7 +7,10 @@ import Questions from "./questions/questions";
67import { useTheme } from "@/hook/useTheme" ;
78import { useSubTopic } from "@/hook/useSubTopic" ;
89import { useQuestion } from "@/hook/useQuestion" ;
10+ import { useQuiz } from "@/hook/useQuiz" ;
911import SetQuiz from "./theme/set" ;
12+ import { AutomaticModeData } from "./questions/automatico/automatico" ;
13+ import { QuizPergunta } from "@/util/types/quiz" ;
1014
1115interface ThemeData {
1216 id ?: string ;
@@ -38,11 +42,14 @@ export default function CreateQuiz() {
3842 const [ themeData , setThemeData ] = useState < ThemeData | null > ( null ) ;
3943 const [ subtopicData , setSubtopicData ] = useState < SubtopicData | null > ( null ) ;
4044 const [ questionsData , setQuestionsData ] = useState < QuestionData [ ] > ( [ ] ) ;
45+ const [ isAutomatic , setIsAutomatic ] = useState ( false ) ;
46+ const [ automaticData , setAutomaticData ] = useState < AutomaticModeData | null > ( null ) ;
4147 const [ saving , setSaving ] = useState ( false ) ;
4248
4349 const { createTheme } = useTheme ( ) ;
4450 const { createSubTopic } = useSubTopic ( ) ;
4551 const { createQuestion } = useQuestion ( ) ;
52+ const { createQuizFromText, createQuizFromAudio, createQuizFromDocument } = useQuiz ( ) ;
4653
4754 const handleNext = ( ) => {
4855 if ( ! themeData ?. title ) {
@@ -62,10 +69,37 @@ export default function CreateQuiz() {
6269 setCurrentStep ( "setup" ) ;
6370 } ;
6471
72+ const convertQuizToQuestions = ( quizPerguntas : QuizPergunta [ ] ) : QuestionData [ ] => {
73+ return quizPerguntas . map ( ( pergunta ) => ( {
74+ text : pergunta . pergunta ,
75+ alternatives : pergunta . alternativas . map ( ( alt ) => ( {
76+ text : alt . texto ,
77+ correct : alt . correta ,
78+ explanation : alt . explicacao ,
79+ } ) ) ,
80+ } ) ) ;
81+ } ;
82+
6583 const handleFinish = async ( ) => {
66- if ( questionsData . length === 0 ) {
67- alert ( "Adicione pelo menos uma questão!" ) ;
68- return ;
84+ // Validação
85+ if ( isAutomatic ) {
86+ if ( ! automaticData ) {
87+ alert ( "Preencha os dados para geração automática!" ) ;
88+ return ;
89+ }
90+ if ( automaticData . mode === "text" && ! automaticData . text ) {
91+ alert ( "Preencha o texto para geração!" ) ;
92+ return ;
93+ }
94+ if ( ( automaticData . mode === "audio" || automaticData . mode === "document" ) && ! automaticData . file ) {
95+ alert ( "Selecione um arquivo para geração!" ) ;
96+ return ;
97+ }
98+ } else {
99+ if ( questionsData . length === 0 ) {
100+ alert ( "Adicione pelo menos uma questão!" ) ;
101+ return ;
102+ }
69103 }
70104
71105 setSaving ( true ) ;
@@ -91,13 +125,58 @@ export default function CreateQuiz() {
91125 subTopicId = newSubTopic . id ;
92126 }
93127
94- // 3. Cria todas as questões
95- for ( const question of questionsData ) {
96- await createQuestion ( {
97- text : question . text ,
98- sub_topic_id : subTopicId ! ,
99- alternatives : question . alternatives ,
100- } ) ;
128+ // 3. Gera ou salva as questões
129+ if ( isAutomatic && automaticData ) {
130+ let generatedQuestions : QuestionData [ ] = [ ] ;
131+ let response ;
132+
133+ if ( automaticData . mode === "text" && automaticData . text ) {
134+ response = await createQuizFromText ( {
135+ text : automaticData . text ,
136+ num_questions : automaticData . num_questions ,
137+ num_alternatives : automaticData . num_alternatives ,
138+ theme_id : themeId ,
139+ sub_topic_id : subTopicId
140+ } ) ;
141+ } else if ( automaticData . mode === "audio" && automaticData . file ) {
142+ response = await createQuizFromAudio ( {
143+ file : automaticData . file ,
144+ num_questions : automaticData . num_questions ,
145+ num_alternatives : automaticData . num_alternatives ,
146+ theme_id : themeId ,
147+ sub_topic_id : subTopicId
148+ } ) ;
149+ } else if ( automaticData . mode === "document" && automaticData . file ) {
150+ response = await createQuizFromDocument ( {
151+ file : automaticData . file ,
152+ num_questions : automaticData . num_questions ,
153+ num_alternatives : automaticData . num_alternatives ,
154+ theme_id : themeId ,
155+ sub_topic_id : subTopicId
156+ } ) ;
157+ }
158+
159+ if ( response && response . perguntas ) {
160+ generatedQuestions = convertQuizToQuestions ( response . perguntas ) ;
161+
162+ // Salva as questões geradas no banco
163+ for ( const question of generatedQuestions ) {
164+ await createQuestion ( {
165+ text : question . text ,
166+ sub_topic_id : subTopicId ! ,
167+ alternatives : question . alternatives ,
168+ } ) ;
169+ }
170+ }
171+ } else {
172+ // Salva as questões manuais
173+ for ( const question of questionsData ) {
174+ await createQuestion ( {
175+ text : question . text ,
176+ sub_topic_id : subTopicId ! ,
177+ alternatives : question . alternatives ,
178+ } ) ;
179+ }
101180 }
102181
103182 alert ( "Quiz criado com sucesso!" ) ;
@@ -123,6 +202,8 @@ export default function CreateQuiz() {
123202 { currentStep === "questions" && (
124203 < Questions
125204 onQuestionsChange = { setQuestionsData }
205+ onAutomaticDataChange = { setAutomaticData }
206+ onModeChange = { ( mode ) => setIsAutomatic ( mode === "automatic" ) }
126207 questions = { questionsData }
127208 themeId = { themeData ?. id || "" }
128209 subTopicId = { subtopicData ?. id || "" }
@@ -144,7 +225,7 @@ export default function CreateQuiz() {
144225
145226 { currentStep === "questions" && (
146227 < Button onClick = { handleFinish } disabled = { saving } className = "ml-auto" >
147- { saving ? "Salvando ..." : "Finalizar" }
228+ { saving ? "Processando ..." : "Finalizar" }
148229 </ Button >
149230 ) }
150231 </ nav >
0 commit comments