'use client' import { useState, useEffect } from 'react' import { teacherExcludedSubjectApi, teacherExcludedRoomApi, teachersApi, subjectsApi, roomsApi, } from '@/lib/stundenplan/api' import type { TeacherExcludedSubject, TeacherExcludedRoom, TimetableTeacher, TimetableSubject, TimetableRoom, } from '@/app/stundenplan/types' import { useConstraintCrud, ConstraintShell, useShellStyles } from './_shell' // ---------- Excluded Subject ---------- type SubForm = Omit const initialSub: SubForm = { teacher_id: '', subject_id: '', is_hard: true, weight: 100, active: true, note: '' } export function TeacherExcludedSubjectEditor() { const styles = useShellStyles() const [teachers, setTeachers] = useState([]) const [subjects, setSubjects] = useState([]) const crud = useConstraintCrud(teacherExcludedSubjectApi, initialSub) useEffect(() => { teachersApi.list().then(setTeachers).catch(() => setTeachers([])) subjectsApi.list().then(setSubjects).catch(() => setSubjects([])) }, []) const tLabel = (id: string): string => { const t = teachers.find(x => x.id === id); return t ? `${t.last_name}, ${t.first_name}` : id.slice(0, 8) + '…' } const sLabel = (id: string): string => { const s = subjects.find(x => x.id === id); return s ? s.name : id.slice(0, 8) + '…' } const missing = teachers.length === 0 || subjects.length === 0 return (
crud.setForm({ ...crud.form, is_hard: e.target.checked })} className="w-5 h-5" />
} renderRow={(item) => { const c = item as TeacherExcludedSubject return ( {tLabel(c.teacher_id)} {sLabel(c.subject_id)} {c.is_hard ? '✓' : '—'} {c.weight} ) }} /> ) } // ---------- Excluded Room ---------- type RoomForm = Omit const initialRoom: RoomForm = { teacher_id: '', room_id: '', is_hard: true, weight: 100, active: true, note: '' } export function TeacherExcludedRoomEditor() { const styles = useShellStyles() const [teachers, setTeachers] = useState([]) const [rooms, setRooms] = useState([]) const crud = useConstraintCrud(teacherExcludedRoomApi, initialRoom) useEffect(() => { teachersApi.list().then(setTeachers).catch(() => setTeachers([])) roomsApi.list().then(setRooms).catch(() => setRooms([])) }, []) const tLabel = (id: string): string => { const t = teachers.find(x => x.id === id); return t ? `${t.last_name}, ${t.first_name}` : id.slice(0, 8) + '…' } const rLabel = (id: string): string => { const r = rooms.find(x => x.id === id); return r ? r.name : id.slice(0, 8) + '…' } const missing = teachers.length === 0 || rooms.length === 0 return (
crud.setForm({ ...crud.form, is_hard: e.target.checked })} className="w-5 h-5" />
} renderRow={(item) => { const c = item as TeacherExcludedRoom return ( {tLabel(c.teacher_id)} {rLabel(c.room_id)} {c.is_hard ? '✓' : '—'} {c.weight} ) }} /> ) }