From 5573274ad41786499d793a3604defd103607821b Mon Sep 17 00:00:00 2001 From: Lucas Santana Date: Fri, 20 Dec 2024 16:00:47 -0300 Subject: [PATCH] =?UTF-8?q?fix:=20corrige=20grava=C3=A7=C3=A3o=20de=20?= =?UTF-8?q?=C3=A1udio=20na=20p=C3=A1gina=20de=20hist=C3=B3ria?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remove campos não utilizados (class_id e school_id) da tabela story_recordings - Simplifica o componente AudioRecorder para usar apenas campos necessários - Atualiza interface StoryRecording no types/database.ts - Corrige erro de constraint na inserção de gravações --- src/components/story/AudioRecorder.tsx | 22 ++++++++++++---------- src/pages/student-dashboard/StoryPage.tsx | 2 -- src/types/database.ts | 8 ++++++++ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/components/story/AudioRecorder.tsx b/src/components/story/AudioRecorder.tsx index 5a33819..f4f8378 100644 --- a/src/components/story/AudioRecorder.tsx +++ b/src/components/story/AudioRecorder.tsx @@ -5,12 +5,10 @@ import { supabase } from '../../lib/supabase'; interface AudioRecorderProps { storyId: string; studentId: string; - classId: string; - schoolId: string; onAudioUploaded: (audioUrl: string) => void; } -export function AudioRecorder({ storyId, studentId, classId, schoolId, onAudioUploaded }: AudioRecorderProps) { +export function AudioRecorder({ storyId, studentId, onAudioUploaded }: AudioRecorderProps) { const [isRecording, setIsRecording] = useState(false); const [audioBlob, setAudioBlob] = useState(null); const [isUploading, setIsUploading] = useState(false); @@ -56,15 +54,21 @@ export function AudioRecorder({ storyId, studentId, classId, schoolId, onAudioUp const uploadAudio = async () => { if (!audioBlob) return; + const { data: { session } } = await supabase.auth.getSession(); + if (!session?.user) { + setError('Usuário não autenticado'); + return; + } + setIsUploading(true); setError(null); try { - // Criar nome único para o arquivo usando uma estrutura de pastas organizada + // Criar nome único para o arquivo const timestamp = new Date().getTime(); - const filePath = `${studentId}/${classId}/${storyId}/${timestamp}.webm`; + const filePath = `${studentId}/${storyId}/${timestamp}.webm`; - // Upload do arquivo para o Supabase Storage + // Upload do arquivo const { data, error: uploadError } = await supabase.storage .from('recordings') .upload(filePath, audioBlob, { @@ -74,19 +78,17 @@ export function AudioRecorder({ storyId, studentId, classId, schoolId, onAudioUp if (uploadError) throw uploadError; - // Obter URL pública do arquivo + // Obter URL pública const { data: { publicUrl } } = supabase.storage .from('recordings') .getPublicUrl(filePath); - // Salvar referência no banco com todas as relações + // Salvar referência no banco apenas com os campos necessários const { error: dbError } = await supabase .from('story_recordings') .insert({ story_id: storyId, student_id: studentId, - class_id: classId, - school_id: schoolId, audio_url: publicUrl, status: 'pending_analysis' }); diff --git a/src/pages/student-dashboard/StoryPage.tsx b/src/pages/student-dashboard/StoryPage.tsx index 0cb165a..2962cfc 100644 --- a/src/pages/student-dashboard/StoryPage.tsx +++ b/src/pages/student-dashboard/StoryPage.tsx @@ -129,8 +129,6 @@ export function StoryPage() { { console.log('Áudio gravado:', audioUrl); }} diff --git a/src/types/database.ts b/src/types/database.ts index 070c42e..a49a226 100644 --- a/src/types/database.ts +++ b/src/types/database.ts @@ -129,4 +129,12 @@ export interface StudentWithStories extends Student { // Atualizando ClassWithStudents para incluir histórias dos alunos export interface ClassWithStudentsAndStories extends Class { students: StudentWithStories[]; +} + +interface StoryRecording { + id: string; + student_id: string; + story_id: string; + audio_url: string; + created_at: string; } \ No newline at end of file