-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexercise-card.tsx
More file actions
94 lines (87 loc) · 2.92 KB
/
Copy pathexercise-card.tsx
File metadata and controls
94 lines (87 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
'use client'
import React, { useState } from 'react'
import { SeriesForm } from '@/modules/training/components/series-form'
import { getTrackingFields, type MetricField } from '@/modules/training/exercises'
import { getExerciseName, type WorkoutExerciseTree } from '@/modules/training/plans'
import type { SetLog } from '@/payload-types'
import {
toMetricFormValues,
type MetricFormValues,
} from '@/modules/training/logs'
import { AddSetActions } from './components/add-set-actions'
import { ExerciseHeader } from './components/exercise-header'
import { ExerciseNote } from './components/exercise-note'
import { MetaLine } from './components/meta-line'
import { SeriesList } from './components/series-list'
export function ExerciseCard({
exercise,
sets,
clientNote = '',
onAdd,
onUpdate,
onDelete,
onSaveNote,
readOnly,
}: {
exercise: WorkoutExerciseTree
sets: SetLog[]
clientNote?: string
onAdd?: (
exercise: WorkoutExerciseTree,
fields: MetricField[],
values: MetricFormValues,
) => Promise<void>
onUpdate?: (id: number, fields: MetricField[], values: MetricFormValues) => Promise<void>
onDelete?: (id: number) => Promise<void>
onSaveNote?: (exercise: WorkoutExerciseTree, note: string) => Promise<void>
readOnly?: boolean
}) {
const [open, setOpen] = useState(false)
const fields: MetricField[] =
exercise.targetType === 'duration'
? ['weightLeft', 'weightRight', 'durationSec']
: getTrackingFields(exercise.exercise?.trackingType)
const prefillValues: MetricFormValues = {
repsLeft: exercise.repsLeft ?? '',
repsRight: exercise.repsRight ?? '',
note: '',
}
const name = getExerciseName(exercise)
const note = exercise.exercise && exercise.note !== name ? exercise.note : null
return (
<div className="border-t border-ui-border-base py-2.5 first:border-t-0 first:pt-0 last:pb-0">
<ExerciseHeader
numer={exercise.numer}
name={name}
videoUrl={exercise.exercise?.videoUrl}
/>
{exercise.meta.length > 0 && <MetaLine>{exercise.meta.join(' · ')}</MetaLine>}
{note && <MetaLine>{note}</MetaLine>}
<SeriesList sets={sets} fields={fields} onUpdate={onUpdate} onDelete={onDelete} readOnly={readOnly} />
{!readOnly &&
(open ? (
<SeriesForm
fields={fields}
initial={prefillValues}
onSubmit={async (values) => {
await onAdd?.(exercise, fields, values)
setOpen(false)
}}
onCancel={() => setOpen(false)}
/>
) : (
<AddSetActions
onAdd={() => setOpen(true)}
onDuplicate={
sets.length > 0 ? () => onAdd?.(exercise, fields, toMetricFormValues(sets.at(-1)!, fields)) : undefined
}
/>
))}
<ExerciseNote
note={clientNote}
readOnly={readOnly}
onSave={(note) => onSaveNote!(exercise, note)}
/>
</div>
)
}