feat(training-planning): enhance training module integration and UI
Deploy Development / deploy (push) Successful in 40s
Test Suite / pytest-backend (push) Successful in 39s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 11s
Test Suite / playwright-tests (push) Successful in 56s

- Introduced a new function to handle optional source training module IDs, ensuring proper validation and integration.
- Updated the backend to include source training module ID and title in section items, allowing for better tracking of module origins.
- Enhanced the frontend to display module bands in the Training Unit Sections Editor, improving user experience by indicating the source of exercises and notes.
- Added functionality to insert training modules at specified positions within sections, providing users with more control over their training plans.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 4.6 committed 2026-05-12 21:53:41 +02:00
1 parent c1243651bb
commit e41908af73
6 files changed
+385 -83

No files matched your search

+93 -13
View File
@@ -1,8 +1,10 @@
import React, { useCallback, useEffect, useState } from 'react'
import React, { useCallback, useEffect, useMemo, useState } from 'react'
import { Link, useNavigate, useParams } from 'react-router-dom'
import api from '../utils/api'
import ExercisePickerModal from '../components/ExercisePickerModal'
import { hydrateExercisePlanningRow } from '../utils/trainingUnitSectionsForm'
import { useAuth } from '../context/AuthContext'
import { activeClubMemberships, getResolvedActiveClubIdForUi } from '../utils/activeClub'
function nextLocalKey() {
return `m-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`
@@ -38,6 +40,19 @@ export default function TrainingModuleEditPage() {
const [primaryMethodId, setPrimaryMethodId] = useState('')
const [items, setItems] = useState([])
const { user } = useAuth()
const clubChoices = useMemo(() => activeClubMemberships(user?.clubs ?? []), [user?.clubs])
useEffect(() => {
if (!isNew || visibility !== 'club') return
if ((clubIdField || '').trim() !== '') return
if (clubChoices.length === 1) setClubIdField(String(clubChoices[0].id))
else {
const r = getResolvedActiveClubIdForUi(user)
if (r) setClubIdField(String(r))
}
}, [isNew, visibility, clubIdField, clubChoices, user])
const itemsPayload = items.map((it, i) => {
if (it.item_type === 'note') {
return { item_type: 'note', order_index: i, note_body: it.note_body ?? '' }
@@ -133,8 +148,16 @@ export default function TrainingModuleEditPage() {
}, [isNew, moduleId])
const buildBody = () => {
const cid =
visibility === 'club' && clubIdField !== '' ? parseInt(clubIdField, 10) : null
let cid = null
if (visibility === 'club') {
const raw = (clubIdField || '').trim()
if (raw !== '') {
const p = parseInt(raw, 10)
if (Number.isFinite(p) && p >= 1) cid = p
} else if (clubChoices.length === 1) {
cid = clubChoices[0].id
}
}
const pm =
primaryMethodId !== '' && primaryMethodId != null ? parseInt(primaryMethodId, 10) : null
return {
@@ -270,22 +293,79 @@ export default function TrainingModuleEditPage() {
<div className="form-row" style={{ display: 'grid', gap: '1rem', gridTemplateColumns: '1fr 1fr' }}>
<div>
<label className="form-label">Sichtbarkeit</label>
<select className="form-input" value={visibility} onChange={(e) => setVisibility(e.target.value)}>
<select
className="form-input"
value={visibility}
onChange={(e) => {
const v = e.target.value
setVisibility(v)
if (v !== 'club') {
setClubIdField('')
return
}
const xs = clubChoices
if (xs.length === 1) setClubIdField(String(xs[0].id))
else if (xs.length === 0) setClubIdField('')
else {
const resolved = getResolvedActiveClubIdForUi(user)
setClubIdField(resolved != null ? String(resolved) : '')
}
}}
>
<option value="private">Privat</option>
<option value="club">Vereinsintern</option>
<option value="official">Offiziell</option>
</select>
</div>
<div>
<label className="form-label">Vereins‑ID (optional, bei Vereins‑Sichtbarkeit)</label>
<input
className="form-input"
type="number"
min={1}
value={clubIdField}
onChange={(e) => setClubIdField(e.target.value)}
placeholder="Leer = aktiver Verein (Server)"
/>
<label className="form-label">Verein (bei „Vereinsintern“)</label>
{visibility !== 'club' ? (
<p style={{ margin: '0.25rem 0 0', fontSize: '0.85rem', color: 'var(--text3)', lineHeight: 1.45 }}>
Bei privaten oder offiziellen Modulen ist keine Vereinszuordnung nötig (Server legt keine
Vereinsbindung fest).
</p>
) : clubChoices.length === 0 ? (
<p style={{ margin: '0.25rem 0 0', fontSize: '0.85rem', color: 'var(--danger)', lineHeight: 1.45 }}>
Kein aktiver Verein im Profil — bitte zuerst einem Verein beitreten.
</p>
) : clubChoices.length === 1 ? (
<>
<input
className="form-input"
disabled
readOnly
value={
(clubChoices[0].short_name || clubChoices[0].name || '').trim() ||
`Verein #${clubChoices[0].id}`
}
/>
<p style={{ margin: '0.35rem 0 0', fontSize: '0.82rem', color: 'var(--text3)', lineHeight: 1.45 }}>
Fixiert durch deine Mitgliedschaft. Verein-ID {clubChoices[0].id} wird beim Speichern verwendet.
</p>
</>
) : (
<>
<select
className="form-input"
value={clubIdField}
onChange={(e) => setClubIdField(e.target.value)}
>
<option value="">Automatisch (aktueller Verein im Profil)</option>
{clubChoices.map((c) => {
const ln = `${((c.short_name || c.name || '').trim() || '') || `Verein #${c.id}`}`
return (
<option key={c.id} value={String(c.id)}>
{ln}
</option>
)
})}
</select>
<p style={{ margin: '0.35rem 0 0', fontSize: '0.82rem', color: 'var(--text3)', lineHeight: 1.45 }}>
Bei „Automatisch“ entscheidet der aktiv gewählte Verein beim Speichern (wie bei anderen
Bibliotheksinhalten).
</p>
</>
)}
</div>
</div>
+82 -31
View File
@@ -14,6 +14,7 @@ import {
enrichSectionsWithVariants,
buildSectionsPayload,
hydrateExercisePlanningRow,
insertTrainingModuleIntoPlanningSections,
} from '../utils/trainingUnitSectionsForm'
function addDaysIsoDate(isoDay, daysDelta) {
@@ -149,6 +150,7 @@ function TrainingPlanningPage() {
const [moduleApplyList, setModuleApplyList] = useState([])
const [moduleApplyModuleId, setModuleApplyModuleId] = useState('')
const [moduleApplySectionIx, setModuleApplySectionIx] = useState(0)
const [moduleApplyInsertSlot, setModuleApplyInsertSlot] = useState('__end__')
const [moduleApplyErr, setModuleApplyErr] = useState('')
const [startDate, setStartDate] = useState(today)
@@ -196,6 +198,19 @@ function TrainingPlanningPage() {
return Number.isFinite(c) ? c : null
}, [groups, formData.group_id])
const moduleApplyTargetItems = useMemo(() => {
const secs = formData.sections || []
if (!secs.length) return []
let ix =
typeof moduleApplySectionIx === 'number'
? moduleApplySectionIx
: parseInt(String(moduleApplySectionIx), 10)
if (!Number.isFinite(ix)) ix = 0
if (ix < 0 || ix >= secs.length) return []
const sec = secs[ix]
return Array.isArray(sec?.items) ? sec.items : []
}, [formData.sections, moduleApplySectionIx])
const refreshPlanningSectionMeta = useCallback(async () => {
const next = await enrichSectionsWithVariants(planningFormRef.current.sections)
setFormData((prev) => ({ ...prev, sections: next }))
@@ -672,6 +687,7 @@ function TrainingPlanningPage() {
const openModuleApplyModal = useCallback(async () => {
setModuleApplyErr('')
setModuleApplySectionIx(0)
setModuleApplyInsertSlot('__end__')
setModuleApplyOpen(true)
try {
const list = await api.listTrainingModules()
@@ -685,41 +701,48 @@ function TrainingPlanningPage() {
}, [])
const handleApplyTrainingModuleConfirm = useCallback(async () => {
if (!editingUnit?.id) return
const mid = parseInt(moduleApplyModuleId, 10)
if (!Number.isFinite(mid)) {
alert('Bitte ein Trainingsmodul wählen.')
return
}
let secIx = parseInt(moduleApplySectionIx, 10)
let secIx = parseInt(String(moduleApplySectionIx), 10)
if (!Number.isFinite(secIx)) secIx = 0
if (!formData.sections?.length) {
const baseSections = planningFormRef.current?.sections ?? formData.sections ?? []
if (!baseSections.length) {
alert('Keine Abschnitte im Formular.')
return
}
if (secIx < 0 || secIx >= formData.sections.length) secIx = 0
if (secIx < 0 || secIx >= baseSections.length) secIx = 0
let insertBefore = null
if (moduleApplyInsertSlot === '__end__') insertBefore = 'end'
else if (moduleApplyInsertSlot === '__start__') insertBefore = 'start'
else if (typeof moduleApplyInsertSlot === 'string' && moduleApplyInsertSlot.startsWith('before:')) {
const zi = parseInt(moduleApplyInsertSlot.slice('before:'.length), 10)
insertBefore = Number.isFinite(zi) ? zi : 'end'
} else insertBefore = 'end'
setModuleApplyBusy(true)
setModuleApplyErr('')
try {
await api.applyTrainingModuleToTrainingUnit(editingUnit.id, {
module_id: mid,
section_order_index: secIx,
const detail = await api.getTrainingModule(mid)
let nextSections = await insertTrainingModuleIntoPlanningSections({
sections: baseSections,
moduleDetail: detail,
sectionIndex: secIx,
insertBeforeItemIndex: insertBefore,
})
await handleEdit({ id: editingUnit.id })
nextSections = await enrichSectionsWithVariants(nextSections)
setFormData((fd) => ({ ...fd, sections: nextSections }))
setModuleApplyOpen(false)
} catch (e) {
setModuleApplyErr(e.message || 'Übernehmen fehlgeschlagen')
setModuleApplyErr(e.message || 'Einfügen fehlgeschlagen')
} finally {
setModuleApplyBusy(false)
}
}, [
editingUnit?.id,
moduleApplyModuleId,
moduleApplySectionIx,
formData.sections?.length,
handleEdit,
])
}, [moduleApplyModuleId, moduleApplySectionIx, moduleApplyInsertSlot, formData.sections])
const handleTakeLead = async (unit) => {
if (!user?.id) return
@@ -1895,11 +1918,12 @@ function TrainingPlanningPage() {
aria-labelledby="module-apply-title"
>
<h2 id="module-apply-title" style={{ marginBottom: '0.5rem', fontSize: '1.15rem' }}>
Trainingsmodul übernehmen
Modul einfügen
</h2>
<p style={{ color: 'var(--text2)', fontSize: '0.9rem', marginBottom: '1rem', lineHeight: 1.5 }}>
Der Inhalt wird <strong>kopiert</strong> und ans Ende des gewählten Abschnitts angehängt (Herkunft wird
gespeichert). Anschließend kannst du ihn lokal bearbeiten.
Übungen und Notizen des Moduls werden <strong>kopiert</strong> wie bei einer einzelnen Übung —
ohne die Einheit vorher gespeichert zu haben (Speichern am Ende wie gewohnt). Die Herkunft bleibt
am Block sichtbar; du kannst alles weiter anpassen.
</p>
{moduleApplyErr ? (
@@ -1930,7 +1954,10 @@ function TrainingPlanningPage() {
<select
className="form-input"
value={String(moduleApplySectionIx)}
onChange={(e) => setModuleApplySectionIx(parseInt(e.target.value, 10))}
onChange={(e) => {
setModuleApplySectionIx(parseInt(e.target.value, 10))
setModuleApplyInsertSlot('__end__')
}}
disabled={moduleApplyBusy || !formData.sections?.length}
>
{(formData.sections || []).map((s, i) => (
@@ -1941,6 +1968,32 @@ function TrainingPlanningPage() {
</select>
</div>
<div className="form-row">
<label className="form-label">Position in diesem Abschnitt</label>
<select
className="form-input"
value={moduleApplyInsertSlot}
onChange={(e) => setModuleApplyInsertSlot(e.target.value)}
disabled={moduleApplyBusy || !(formData.sections?.length > 0)}
>
<option value="__end__">Ans Ende einfügen (nach allen Einträgen)</option>
<option value="__start__">An den Anfang (vor dem ersten Eintrag)</option>
{moduleApplyTargetItems.map((row, xi) => {
const labelPart =
row.item_type === 'note'
? 'Zwischen-Anmerkung'
: (row.exercise_title || '').trim() || `Übung #${row.exercise_id || '—'}`
const clipped =
labelPart.length > 44 ? `${labelPart.slice(0, 43).trim()}…` : labelPart
return (
<option key={`before-${xi}`} value={`before:${xi}`}>
Vor Eintrag {xi + 1}: {clipped}
</option>
)
})}
</select>
</div>
<div
style={{
display: 'flex',
@@ -1959,7 +2012,7 @@ function TrainingPlanningPage() {
Abbrechen
</button>
<button type="button" className="btn btn-primary" disabled={moduleApplyBusy} onClick={handleApplyTrainingModuleConfirm}>
{moduleApplyBusy ? 'Übernehmen …' : 'Übernehmen'}
{moduleApplyBusy ? 'Einfügen …' : 'Einfügen'}
</button>
</div>
@@ -2473,16 +2526,14 @@ function TrainingPlanningPage() {
<button type="button" className="btn btn-secondary" onClick={handleSaveAsTemplate}>
Vorlage aus Aufbau speichern
</button>
{editingUnit?.id ? (
<button
type="button"
className="btn btn-secondary"
onClick={openModuleApplyModal}
title="Übungen und Notizen aus einem Modul ans Ende eines Abschnitts kopieren"
>
Aus Modul übernehmen…
</button>
) : null}
<button
type="button"
className="btn btn-secondary"
onClick={openModuleApplyModal}
title="Modulpositionen hier einfügen (Kopie, auch ohne zwischengespeicherte Einheit)"
>
Modul einfügen…
</button>
</>
}
sections={formData.sections}