story-generator/src/hooks/phonics/usePhonicsExercises.ts
Lucas Santana a0cfccc14d
Some checks failed
Docker Build and Push / build (push) Has been cancelled
fix: Phonic types
2025-01-20 07:08:43 -03:00

58 lines
1.5 KiB
TypeScript

import { useQuery } from '@tanstack/react-query';
import { supabase } from '@/lib/supabase';
import type { PhonicsExercise, PhonicsExerciseCategory } from '@/types/phonics';
export function usePhonicsExercises(categoryId?: string) {
return useQuery({
queryKey: ['phonics-exercises', categoryId],
queryFn: async () => {
const query = supabase
.from('phonics_exercises')
.select(`
*,
category:phonics_categories(
id,
name,
description,
level
),
type:phonics_exercise_types(
id,
name
),
words:phonics_exercise_words(
id,
is_correct_answer,
order_index,
word:phonics_words(word)
)
`)
.eq('is_active', true)
.order('difficulty_level', { ascending: true });
if (categoryId) {
query.eq('category_id', categoryId);
}
const { data, error } = await query;
if (error) throw error;
return data as PhonicsExercise[];
}
});
}
export function usePhonicsCategories() {
return useQuery({
queryKey: ['phonics-categories'],
queryFn: async () => {
const { data, error } = await supabase
.from('phonics_categories')
.select('*')
.order('order_index', { ascending: true });
if (error) throw error;
return data as PhonicsExerciseCategory[];
}
});
}