I'll help you improve the AdminPanel.tsx with these requirements. Here are the necessary changes:
First, let's fix the delete functionality by adding delete handlers:
typescript Copy code
const handleDeleteClass = (id: string) => {
setClasses(classes.filter(cls => cls.id !== id));
saveData();
toast.success("Turma removida com sucesso!");
};
const handleDeleteTeacher = (id: string) => {
setTeachers(teachers.filter(teacher => teacher.id !== id));
saveData();
toast.success("Professor removido com sucesso!");
};
const handleDeleteSubject = (id: string) => {
setSubjects(subjects.filter(subject => subject.id !== id));
saveData();
toast.success("Disciplina removida com sucesso!");
};
For the subjects section, we need to add class selection and icon preview. Here's the modified subject addition form:
typescript Copy code
import { Icons } from "@/components/icons"; // You'll need to create this component with available icons
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
// Add these states
const [selectedClass, setSelectedClass] = useState("");
const [selectedIcon, setSelectedIcon] = useState("Book");
// Modify the handleAddSubject function
const handleAddSubject = () => {
if (!newSubjectName || !selectedClass) {
toast.error("Por favor, preencha todos os campos");
return;
}
const newSubject = {
id: crypto.randomUUID(),
name: newSubjectName,
iconName: selectedIcon,
color: "bg-blue-200",
classId: selectedClass // Add this field to link subject to class
};
setSubjects([...subjects, newSubject]);
saveData();
setNewSubjectName("");
setSelectedClass("");
setSelectedIcon("Book");
toast.success("Disciplina adicionada com sucesso!");
};
// Modify the subject form in SheetContent
<SheetContent>
<SheetHeader>
<SheetTitle>Adicionar Nova Disciplina</SheetTitle>
</SheetHeader>
<div className="grid gap-4 py-4">
<div className="space-y-2">
<Label htmlFor="subjectName">Nome da Disciplina</Label>
<Input
id="subjectName"
value={newSubjectName}
onChange={(e) => setNewSubjectName(e.target.value)}
placeholder="Ex: Matemática"
/>
</div>
<div className="space-y-2">
<Label htmlFor="class">Turma</Label>
<Select value={selectedClass} onValueChange={setSelectedClass}>
<SelectTrigger>
<SelectValue placeholder="Selecione uma turma" />
</SelectTrigger>
<SelectContent>
{classes.map((cls) => (
<SelectItem key={cls.id} value={cls.id}>
{cls.name}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="space-y-2">
<Label htmlFor="icon">Ícone</Label>
<Select value={selectedIcon} onValueChange={setSelectedIcon}>
<SelectTrigger>
<SelectValue>
<div className="flex items-center gap-2">
{/* Add dynamic icon preview */}
{Icons[selectedIcon as keyof typeof Icons] &&
<Icons[selectedIcon as keyof typeof Icons] className="w-4 h-4" />}
{selectedIcon}
</div>
</SelectValue>
</SelectTrigger>
<SelectContent>
{Object.keys(Icons).map((iconName) => (
<SelectItem key={iconName} value={iconName}>
<div className="flex items-center gap-2">
{Icons[iconName as keyof typeof Icons] &&
<Icons[iconName as keyof typeof Icons] className="w-4 h-4" />}
{iconName}
</div>
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<Button onClick={handleAddSubject}>Adicionar Disciplina</Button>
</div>
</SheetContent>
Update the delete buttons in each table to use the delete handlers:
typescript Copy code
// In the Classes table
<Button
variant="destructive"
size="icon"
onClick={() => handleDeleteClass(cls.id)}
>
<Trash2 className="h-4 w-4" />
</Button>
// In the Teachers table
<Button
variant="destructive"
size="icon"
onClick={() => handleDeleteTeacher(teacher.id)}
>
<Trash2 className="h-4 w-4" />
</Button>
// In the Subjects table
<Button
variant="destructive"
size="icon"
onClick={() => handleDeleteSubject(subject.id)}
>
<Trash2 className="h-4 w-4" />
</Button>
Update the Subject interface in types/index.ts to include the classId:
typescript Copy code
export interface Subject {
id: string;
name: string;
iconName: string;
color: string;
teacherId?: string;
classId: string; // Add this field
}
You'll also need to create an Icons component that exports all available icons. You can use the icons from lucide-react or any other icon library.
These changes will:
Enable delete functionality for all items
Link subjects to specific classes
Add icon selection with preview
Show which class each subject belongs to
Remember to update any existing subjects in your initial data to include the classId field.