'use client' import { useState, useEffect } from 'react' import { subjectMaxConsecutiveApi, subjectsApi } from '@/lib/stundenplan/api' import type { SubjectMaxConsecutive, TimetableSubject } from '@/app/stundenplan/types' import { useConstraintCrud, ConstraintShell, useShellStyles } from './_shell' type FormState = Omit const initialForm: FormState = { subject_id: '', max_consecutive: 2, is_hard: true, weight: 100, active: true, note: '', } export function SubjectMaxConsecutiveEditor() { const styles = useShellStyles() const crud = useConstraintCrud(subjectMaxConsecutiveApi, initialForm) const [subjects, setSubjects] = useState([]) useEffect(() => { subjectsApi.list().then(setSubjects).catch(() => setSubjects([])) }, []) const sLabel = (id: string): string => { const s = subjects.find(x => x.id === id) return s ? `${s.name} (${s.short_code})` : id.slice(0, 8) + '…' } return (
crud.setForm({ ...crud.form, max_consecutive: parseInt(e.target.value) || 1 })} className={`w-full px-3 py-2 rounded-lg border ${styles.inputClass}`} />
crud.setForm({ ...crud.form, is_hard: e.target.checked })} className="w-5 h-5" />
crud.setForm({ ...crud.form, weight: parseInt(e.target.value) || 0 })} className={`w-full px-3 py-2 rounded-lg border ${styles.inputClass}`} />
} renderRow={(item) => { const c = item as SubjectMaxConsecutive return ( {sLabel(c.subject_id)} {c.max_consecutive} {c.is_hard ? '✓' : '—'} {c.weight} ) }} /> ) }