mirror of
https://github.com/lucasrcsantana/story-generator.git
synced 2025-12-17 22:07:52 +00:00
Some checks are pending
Docker Build and Push / build (push) Waiting to run
- Corrige tipo de retorno em useExerciseWords - Ajusta usePhonicsExercises para filtrar por categoria - Atualiza queries para usar inner join e ordenação - Adiciona interfaces para melhor tipagem - Corrige convenção de nomes para snake_case
74 lines
1.8 KiB
TypeScript
74 lines
1.8 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
import { supabase } from '@/lib/supabase';
|
|
import type { StudentPhonicsAttempt, PhonicsWord } from '@/types/phonics';
|
|
|
|
interface AttemptParams {
|
|
student_id: string;
|
|
exercise_id: string;
|
|
score: number;
|
|
time_spent_seconds: number;
|
|
}
|
|
|
|
interface ExerciseWord {
|
|
word: PhonicsWord;
|
|
options: PhonicsWord[];
|
|
}
|
|
|
|
export function useExerciseWords(exerciseId: string) {
|
|
return useQuery({
|
|
queryKey: ['exercise-words', exerciseId],
|
|
queryFn: async () => {
|
|
const { data, error } = await supabase
|
|
.from('phonics_exercise_words')
|
|
.select(`
|
|
word:phonics_words!inner (
|
|
id,
|
|
word,
|
|
audio_url,
|
|
phonetic_transcription,
|
|
syllables_count,
|
|
created_at
|
|
),
|
|
options:phonics_words!inner (
|
|
id,
|
|
word,
|
|
audio_url,
|
|
phonetic_transcription,
|
|
syllables_count,
|
|
created_at
|
|
)
|
|
`)
|
|
.eq('exercise_id', exerciseId)
|
|
.order('order_index', { ascending: true });
|
|
|
|
if (error) throw error;
|
|
|
|
// Transformar os dados para o formato correto
|
|
return (data || []).map(item => ({
|
|
word: item.word[0],
|
|
options: item.options
|
|
})) as ExerciseWord[];
|
|
},
|
|
enabled: !!exerciseId
|
|
});
|
|
}
|
|
|
|
export function useExerciseAttempt() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: async (params: AttemptParams) => {
|
|
const { data, error } = await supabase
|
|
.from('student_phonics_attempts')
|
|
.insert(params)
|
|
.select()
|
|
.single();
|
|
|
|
if (error) throw error;
|
|
return data;
|
|
},
|
|
onSuccess: (_, { student_id }) => {
|
|
queryClient.invalidateQueries({ queryKey: ['phonics-progress', student_id] });
|
|
}
|
|
});
|
|
}
|