mirror of
https://github.com/lucasrcsantana/story-generator.git
synced 2025-12-17 22:07:52 +00:00
148 lines
5.0 KiB
TypeScript
148 lines
5.0 KiB
TypeScript
import React from 'react';
|
|
import { useNavigate } from 'react-router-dom';
|
|
import { ArrowLeft, Send } from 'lucide-react';
|
|
import { useDatabase } from '../../../hooks/useDatabase';
|
|
import { supabase } from '../../../lib/supabase';
|
|
|
|
export function InviteTeacherPage() {
|
|
const navigate = useNavigate();
|
|
const { inviteTeacher } = useDatabase();
|
|
const [isLoading, setIsLoading] = React.useState(false);
|
|
const [formData, setFormData] = React.useState({
|
|
name: '',
|
|
email: '',
|
|
subject: '',
|
|
message: ''
|
|
});
|
|
const [formError, setFormError] = React.useState<string | null>(null);
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setIsLoading(true);
|
|
setFormError(null);
|
|
|
|
try {
|
|
const { data: authData } = await supabase.auth.getSession();
|
|
if (!authData.session?.user) {
|
|
throw new Error('Usuário não autenticado');
|
|
}
|
|
|
|
const { data: schoolData, error: schoolError } = await supabase
|
|
.from('schools')
|
|
.select('id')
|
|
.eq('id', authData.session.user.id)
|
|
.single();
|
|
|
|
if (schoolError) throw schoolError;
|
|
|
|
const result = await inviteTeacher(schoolData.id, {
|
|
name: formData.name,
|
|
email: formData.email,
|
|
subject: formData.subject,
|
|
message: formData.message
|
|
});
|
|
|
|
if (!result) {
|
|
throw new Error('Erro ao enviar convite');
|
|
}
|
|
|
|
navigate('/dashboard/professores', {
|
|
state: { message: 'Convite enviado com sucesso!' }
|
|
});
|
|
} catch (err) {
|
|
console.error('Erro ao enviar convite:', err);
|
|
setFormError(err instanceof Error ? err.message : 'Erro ao enviar convite');
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div>
|
|
<button
|
|
onClick={() => navigate('/dashboard/professores')}
|
|
className="flex items-center gap-2 text-gray-600 hover:text-gray-900 mb-6"
|
|
>
|
|
<ArrowLeft className="h-5 w-5" />
|
|
Voltar para professores
|
|
</button>
|
|
|
|
<div className="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
|
|
<h1 className="text-2xl font-bold text-gray-900 mb-6">Convidar Professor</h1>
|
|
|
|
{formError && (
|
|
<div className="mb-4 p-4 bg-red-50 text-red-600 rounded-lg">
|
|
{formError}
|
|
</div>
|
|
)}
|
|
|
|
<form onSubmit={handleSubmit} className="space-y-6 max-w-2xl">
|
|
<div>
|
|
<label htmlFor="name" className="block text-sm font-medium text-gray-700">
|
|
Nome do Professor
|
|
</label>
|
|
<input
|
|
type="text"
|
|
id="name"
|
|
value={formData.name}
|
|
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
|
|
className="mt-1 block w-full rounded-lg border-gray-300 shadow-sm focus:border-purple-500 focus:ring-purple-500"
|
|
required
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label htmlFor="email" className="block text-sm font-medium text-gray-700">
|
|
Email
|
|
</label>
|
|
<input
|
|
type="email"
|
|
id="email"
|
|
value={formData.email}
|
|
onChange={(e) => setFormData({ ...formData, email: e.target.value })}
|
|
className="mt-1 block w-full rounded-lg border-gray-300 shadow-sm focus:border-purple-500 focus:ring-purple-500"
|
|
required
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label htmlFor="subject" className="block text-sm font-medium text-gray-700">
|
|
Disciplina
|
|
</label>
|
|
<input
|
|
type="text"
|
|
id="subject"
|
|
value={formData.subject}
|
|
onChange={(e) => setFormData({ ...formData, subject: e.target.value })}
|
|
className="mt-1 block w-full rounded-lg border-gray-300 shadow-sm focus:border-purple-500 focus:ring-purple-500"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label htmlFor="message" className="block text-sm font-medium text-gray-700">
|
|
Mensagem Personalizada (opcional)
|
|
</label>
|
|
<textarea
|
|
id="message"
|
|
rows={4}
|
|
value={formData.message}
|
|
onChange={(e) => setFormData({ ...formData, message: e.target.value })}
|
|
className="mt-1 block w-full rounded-lg border-gray-300 shadow-sm focus:border-purple-500 focus:ring-purple-500"
|
|
/>
|
|
</div>
|
|
|
|
<div className="pt-4">
|
|
<button
|
|
type="submit"
|
|
disabled={isLoading}
|
|
className="w-full flex justify-center items-center gap-2 py-2 px-4 border border-transparent rounded-lg shadow-sm text-sm font-medium text-white bg-purple-600 hover:bg-purple-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-purple-500 disabled:opacity-50"
|
|
>
|
|
<Send className="h-4 w-4" />
|
|
{isLoading ? 'Enviando...' : 'Enviar Convite'}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|