feat: Enhance activity session handling and schema retrieval
- Updated `get_activity_session_logical_unit` to support optional parameters for form training context, allowing for more flexible schema resolution. - Introduced a new endpoint `/attribute-schema` to fetch activity attribute schemas without an existing session, improving manual data entry capabilities. - Enhanced the `getActivitySession` API method to accept query parameters for training category and type, facilitating dynamic schema retrieval. - Updated the frontend `ActivityPage` to utilize the new schema fetching logic, ensuring a smoother user experience when managing activity sessions and metrics.
This commit is contained in:
1 parent
92e334dcd2
commit
1220ee54fb
4 files changed
+214
-21
No files matched your search
@@ -554,6 +554,12 @@ export default function ActivityPage() {
|
||||
const [categories, setCategories] = useState({}) // v9d: Training categories
|
||||
const [sessionDetail, setSessionDetail] = useState(null)
|
||||
const [metricDraft, setMetricDraft] = useState({})
|
||||
/** Beim Wechsel Kategorie/Typ: Nutzerwerte für weiterhin vorhandene Schema-Keys nicht mit Server überschreiben */
|
||||
const editSchemaKeysPrevRef = useRef(new Set())
|
||||
const prevEditingIdRef = useRef(null)
|
||||
const [manualSchema, setManualSchema] = useState(null)
|
||||
const [manualMetricDraft, setManualMetricDraft] = useState({})
|
||||
const manualSchemaKeysPrevRef = useRef(new Set())
|
||||
const [sessionLoadError, setSessionLoadError] = useState(null)
|
||||
const [savingEdit, setSavingEdit] = useState(false)
|
||||
const [listLoadingMore, setListLoadingMore] = useState(false)
|
||||
@@ -639,18 +645,31 @@ export default function ActivityPage() {
|
||||
api.getTrainingCategories().then(setCategories).catch(err => console.error('Failed to load categories:', err))
|
||||
}, [fetchMonthsChain])
|
||||
|
||||
useEffect(() => {
|
||||
editSchemaKeysPrevRef.current = new Set()
|
||||
}, [editing?.id])
|
||||
|
||||
useEffect(() => {
|
||||
if (!editing?.id) {
|
||||
setSessionDetail(null)
|
||||
setMetricDraft({})
|
||||
setSessionLoadError(null)
|
||||
prevEditingIdRef.current = null
|
||||
return
|
||||
}
|
||||
let cancelled = false
|
||||
setSessionLoadError(null)
|
||||
if (prevEditingIdRef.current !== editing.id) {
|
||||
setSessionDetail(null)
|
||||
prevEditingIdRef.current = editing.id
|
||||
}
|
||||
;(async () => {
|
||||
try {
|
||||
const d = await api.getActivitySession(editing.id)
|
||||
const d = await api.getActivitySession(editing.id, {
|
||||
useFormSchema: true,
|
||||
training_category: editing.training_category,
|
||||
training_type_id: editing.training_type_id,
|
||||
})
|
||||
if (!cancelled) setSessionDetail(d)
|
||||
} catch (err) {
|
||||
if (!cancelled) {
|
||||
@@ -660,25 +679,89 @@ export default function ActivityPage() {
|
||||
}
|
||||
})()
|
||||
return () => { cancelled = true }
|
||||
}, [editing?.id])
|
||||
}, [editing?.id, editing?.training_category, editing?.training_type_id])
|
||||
|
||||
useEffect(() => {
|
||||
if (!sessionDetail) {
|
||||
setMetricDraft({})
|
||||
return
|
||||
}
|
||||
const m = {}
|
||||
for (const row of sessionDetail.metrics || []) {
|
||||
m[row.key] = row.value
|
||||
}
|
||||
for (const s of sessionDetail.schema || []) {
|
||||
if (!(s.key in m)) {
|
||||
m[s.key] = s.data_type === 'boolean' ? false : ''
|
||||
const newKeys = new Set((sessionDetail.schema || []).map((s) => s.key))
|
||||
const oldKeys = editSchemaKeysPrevRef.current
|
||||
|
||||
setMetricDraft((prev) => {
|
||||
const next = { ...prev }
|
||||
for (const row of sessionDetail.metrics || []) {
|
||||
const k = row.key
|
||||
if (oldKeys.size > 0 && oldKeys.has(k) && newKeys.has(k) && k in prev) {
|
||||
continue
|
||||
}
|
||||
next[k] = row.value
|
||||
}
|
||||
}
|
||||
setMetricDraft(m)
|
||||
for (const s of sessionDetail.schema || []) {
|
||||
if (!(s.key in next)) {
|
||||
next[s.key] = s.data_type === 'boolean' ? false : ''
|
||||
}
|
||||
}
|
||||
return next
|
||||
})
|
||||
editSchemaKeysPrevRef.current = newKeys
|
||||
}, [sessionDetail])
|
||||
|
||||
useEffect(() => {
|
||||
if (tab !== 'add') {
|
||||
setManualSchema(null)
|
||||
setManualMetricDraft({})
|
||||
manualSchemaKeysPrevRef.current = new Set()
|
||||
return
|
||||
}
|
||||
const tid = form.training_type_id
|
||||
const cat = form.training_category
|
||||
if (tid == null && (cat == null || cat === '')) {
|
||||
setManualSchema(null)
|
||||
setManualMetricDraft({})
|
||||
manualSchemaKeysPrevRef.current = new Set()
|
||||
return
|
||||
}
|
||||
let cancelled = false
|
||||
;(async () => {
|
||||
try {
|
||||
const r = await api.getActivityAttributeSchema({
|
||||
training_category: cat || undefined,
|
||||
training_type_id: tid ?? undefined,
|
||||
})
|
||||
if (!cancelled) setManualSchema(Array.isArray(r.schema) ? r.schema : [])
|
||||
} catch (err) {
|
||||
console.error('attribute-schema:', err)
|
||||
if (!cancelled) setManualSchema([])
|
||||
}
|
||||
})()
|
||||
return () => { cancelled = true }
|
||||
}, [tab, form.training_category, form.training_type_id])
|
||||
|
||||
useEffect(() => {
|
||||
if (tab !== 'add' || !manualSchema) {
|
||||
return
|
||||
}
|
||||
const newKeys = new Set(manualSchema.map((s) => s.key))
|
||||
const oldKeys = manualSchemaKeysPrevRef.current
|
||||
|
||||
setManualMetricDraft((prev) => {
|
||||
const next = { ...prev }
|
||||
for (const s of manualSchema) {
|
||||
const k = s.key
|
||||
if (oldKeys.size > 0 && oldKeys.has(k) && newKeys.has(k) && k in prev) {
|
||||
continue
|
||||
}
|
||||
if (!(k in next)) {
|
||||
next[k] = s.data_type === 'boolean' ? false : ''
|
||||
}
|
||||
}
|
||||
return next
|
||||
})
|
||||
manualSchemaKeysPrevRef.current = newKeys
|
||||
}, [tab, manualSchema])
|
||||
|
||||
const handleSave = async () => {
|
||||
setSaving(true)
|
||||
setError(null)
|
||||
@@ -698,7 +781,20 @@ export default function ActivityPage() {
|
||||
if(payload.hr_max) payload.hr_max = parseFloat(payload.hr_max)
|
||||
if(payload.rpe) payload.rpe = parseInt(payload.rpe)
|
||||
payload.source = 'manual'
|
||||
await api.createActivity(payload)
|
||||
const created = await api.createActivity(payload)
|
||||
if (manualSchema && manualSchema.length > 0 && created?.id) {
|
||||
try {
|
||||
const metrics = buildMetricsPayload(manualSchema, manualMetricDraft)
|
||||
await api.putActivityMetrics(created.id, { metrics })
|
||||
} catch (metErr) {
|
||||
console.error(metErr)
|
||||
setError(
|
||||
metErr.message ||
|
||||
'Eintrag gespeichert, aber Zusatzfelder konnten nicht gespeichert werden.',
|
||||
)
|
||||
setTimeout(() => setError(null), 8000)
|
||||
}
|
||||
}
|
||||
setSaved(true)
|
||||
await load()
|
||||
await loadUsage() // Reload usage after save
|
||||
@@ -864,9 +960,23 @@ export default function ActivityPage() {
|
||||
<span>Training eintragen</span>
|
||||
{activityUsage && <UsageBadge {...activityUsage} />}
|
||||
</div>
|
||||
<EntryForm form={form} setForm={setForm}
|
||||
onSave={handleSave} saveLabel={saved?'✓ Gespeichert!':'Speichern'}
|
||||
saving={saving} error={error} usage={activityUsage}/>
|
||||
<EntryForm
|
||||
form={form}
|
||||
setForm={setForm}
|
||||
onSave={handleSave}
|
||||
saveLabel={saved ? '✓ Gespeichert!' : 'Speichern'}
|
||||
saving={saving}
|
||||
error={error}
|
||||
usage={activityUsage}
|
||||
formExtras={
|
||||
<SessionMetricsFields
|
||||
schema={manualSchema}
|
||||
metrics={[]}
|
||||
values={manualMetricDraft}
|
||||
setValues={setManualMetricDraft}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
|
||||
Reference in new issue
Block a user