Files
breakpilot-lehrer/studio-v2/app/stundenplan/_components/AssignmentsManager.tsx
T
Benjamin Admin c2c09e1cd9
CI / go-lint (push) Has been skipped
CI / python-lint (push) Has been skipped
CI / nodejs-lint (push) Has been skipped
CI / test-go-school (push) Successful in 31s
CI / test-go-edu-search (push) Successful in 30s
CI / test-python-klausur (push) Failing after 3m31s
CI / test-python-agent-core (push) Successful in 18s
CI / test-nodejs-website (push) Successful in 22s
Stundenplan Phase 3c: complete Stammdaten + RegelnHub with 4 editors
Frontend additions in studio-v2:
  - PeriodsManager renders the weekly grid as a Mo–So table with one
    row per period_index. New entries auto-increment period_index so
    the user can hit Anlegen repeatedly for a full day's slots.
  - CurriculumManager joins classes + subjects; new entries refuse to
    open when either prerequisite list is empty (banner instead).
  - AssignmentsManager joins teacher × class × subject with the same
    prerequisite-banner pattern.
  - regeln/RegelnHub: vertical sidebar grouping all 15 constraint
    types by parent entity (Lehrer/Fach/Klasse/Raum). Implemented
    editors are clickable, the other 11 are visibly disabled with
    a 'soon' tag.
  - Three new editors:
      TeacherUnavailableWindowEditor (time-window pattern),
      SubjectMaxConsecutiveEditor (number-input pattern),
      SubjectPreferredPeriodEditor (number range pattern).
  - page.tsx wires every tab to its manager; the not-implemented
    placeholder is gone (no more empty tabs).

Test coverage:
  - e2e/stundenplan.spec.ts rewritten: 23 tests across 7 suites,
    covering all 8 tabs, the new managers' prerequisite banners,
    sub-tab switching in the RegelnHub, and the disabled state of
    not-yet-implemented constraint rules. Each test mocks the
    backend via page.route() so the suite stays hermetic.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 23:08:15 +02:00

171 lines
7.5 KiB
TypeScript

'use client'
import { useState, useEffect, useCallback } from 'react'
import { useTheme } from '@/lib/ThemeContext'
import { assignmentsApi, classesApi, subjectsApi, teachersApi } from '@/lib/stundenplan/api'
import type {
TimetableAssignment, CreateTimetableAssignment,
TimetableClass, TimetableSubject, TimetableTeacher,
} from '@/app/stundenplan/types'
const initialForm: CreateTimetableAssignment = {
teacher_id: '',
class_id: '',
subject_id: '',
}
export function AssignmentsManager() {
const { isDark } = useTheme()
const [items, setItems] = useState<TimetableAssignment[]>([])
const [classes, setClasses] = useState<TimetableClass[]>([])
const [subjects, setSubjects] = useState<TimetableSubject[]>([])
const [teachers, setTeachers] = useState<TimetableTeacher[]>([])
const [loading, setLoading] = useState(true)
const [error, setError] = useState<string | null>(null)
const [showForm, setShowForm] = useState(false)
const [submitting, setSubmitting] = useState(false)
const [form, setForm] = useState<CreateTimetableAssignment>(initialForm)
const load = useCallback(async () => {
setLoading(true); setError(null)
try {
const [asg, cls, sub, t] = await Promise.all([
assignmentsApi.list(),
classesApi.list(),
subjectsApi.list(),
teachersApi.list(),
])
setItems(asg || [])
setClasses(cls || [])
setSubjects(sub || [])
setTeachers(t || [])
} catch (e) {
setError(e instanceof Error ? e.message : 'Laden fehlgeschlagen')
} finally { setLoading(false) }
}, [])
useEffect(() => { load() }, [load])
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault()
setSubmitting(true); setError(null)
try {
await assignmentsApi.create(form)
setForm(initialForm)
setShowForm(false)
await load()
} catch (err) {
setError(err instanceof Error ? err.message : 'Anlegen fehlgeschlagen')
} finally { setSubmitting(false) }
}
const handleDelete = async (id: string) => {
if (!confirm('Lehrauftrag wirklich loeschen?')) return
try {
await assignmentsApi.remove(id)
await load()
} catch (err) {
setError(err instanceof Error ? err.message : 'Loeschen fehlgeschlagen')
}
}
const cardClass = isDark ? 'bg-white/10 border-white/20 text-white' : 'bg-white/80 border-black/10 text-slate-900'
const inputClass = isDark ? 'bg-white/10 border-white/20 text-white' : 'bg-white border-slate-300 text-slate-900'
const prereqMissing = classes.length === 0 || subjects.length === 0 || teachers.length === 0
return (
<div className="space-y-4" data-testid="assignments-manager">
<div className="flex items-center justify-between">
<div>
<h2 className={`text-xl font-semibold ${isDark ? 'text-white' : 'text-slate-900'}`}>
Lehrauftraege ({items.length})
</h2>
<p className={`text-sm ${isDark ? 'text-white/60' : 'text-slate-500'}`}>
Welcher Lehrer unterrichtet welches Fach in welcher Klasse.
</p>
</div>
<button
onClick={() => setShowForm(s => !s)}
disabled={prereqMissing}
className={`px-4 py-2 rounded-xl font-medium transition-colors disabled:opacity-50 ${isDark ? 'bg-indigo-500 hover:bg-indigo-600 text-white' : 'bg-indigo-600 hover:bg-indigo-700 text-white'}`}
>
{showForm ? 'Abbrechen' : '+ Neuer Lehrauftrag'}
</button>
</div>
{prereqMissing && !loading && (
<div className={`p-3 rounded-xl text-sm ${isDark ? 'bg-amber-500/10 border border-amber-500/30 text-amber-200' : 'bg-amber-50 border border-amber-200 text-amber-900'}`}>
Zuerst Klassen, Faecher und Lehrer anlegen.
</div>
)}
{error && <div className="p-3 rounded-xl bg-red-500/20 border border-red-500/40 text-red-300">{error}</div>}
{showForm && (
<form onSubmit={handleSubmit} className={`p-4 rounded-2xl border backdrop-blur-xl ${cardClass}`}>
<div className="grid grid-cols-1 md:grid-cols-4 gap-3">
<div>
<label className="block text-sm mb-1 opacity-70">Lehrer</label>
<select required value={form.teacher_id} onChange={e => setForm({ ...form, teacher_id: e.target.value })} className={`w-full px-3 py-2 rounded-lg border ${inputClass}`}>
<option value=""> bitte waehlen </option>
{teachers.map(t => <option key={t.id} value={t.id}>{t.last_name}, {t.first_name}</option>)}
</select>
</div>
<div>
<label className="block text-sm mb-1 opacity-70">Klasse</label>
<select required value={form.class_id} onChange={e => setForm({ ...form, class_id: e.target.value })} className={`w-full px-3 py-2 rounded-lg border ${inputClass}`}>
<option value=""> bitte waehlen </option>
{classes.map(c => <option key={c.id} value={c.id}>{c.name}</option>)}
</select>
</div>
<div>
<label className="block text-sm mb-1 opacity-70">Fach</label>
<select required value={form.subject_id} onChange={e => setForm({ ...form, subject_id: e.target.value })} className={`w-full px-3 py-2 rounded-lg border ${inputClass}`}>
<option value=""> bitte waehlen </option>
{subjects.map(s => <option key={s.id} value={s.id}>{s.name}</option>)}
</select>
</div>
<div className="flex items-end">
<button type="submit" disabled={submitting} className="w-full px-4 py-2 rounded-lg bg-emerald-500 hover:bg-emerald-600 text-white font-medium disabled:opacity-50">
{submitting ? 'Speichert...' : 'Anlegen'}
</button>
</div>
</div>
</form>
)}
{loading ? (
<div className={`text-center py-8 opacity-60 ${isDark ? 'text-white' : 'text-slate-700'}`}>Laedt</div>
) : items.length === 0 ? (
<div className={`text-center py-8 opacity-60 ${isDark ? 'text-white' : 'text-slate-700'}`}>Noch keine Lehrauftraege.</div>
) : (
<div className={`rounded-2xl border backdrop-blur-xl overflow-hidden ${cardClass}`}>
<table className="w-full">
<thead className={isDark ? 'bg-white/5' : 'bg-slate-100'}>
<tr>
<th className="text-left px-4 py-3 text-sm font-medium opacity-70">Lehrer</th>
<th className="text-left px-4 py-3 text-sm font-medium opacity-70">Klasse</th>
<th className="text-left px-4 py-3 text-sm font-medium opacity-70">Fach</th>
<th className="px-4 py-3"></th>
</tr>
</thead>
<tbody>
{items.map(a => (
<tr key={a.id} className={isDark ? 'border-t border-white/10' : 'border-t border-slate-200'}>
<td className="px-4 py-3 font-medium">{a.teacher_name || a.teacher_id.slice(0, 8) + '…'}</td>
<td className="px-4 py-3">{a.class_name || a.class_id.slice(0, 8) + '…'}</td>
<td className="px-4 py-3">{a.subject_name || a.subject_id.slice(0, 8) + '…'}</td>
<td className="px-4 py-3 text-right">
<button onClick={() => handleDelete(a.id)} className="text-red-400 hover:text-red-300 text-sm">Loeschen</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
)
}