Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

Change duration and Log a past workout: the duration field can be emptied to type a new length, so a 48 retyped as 75 saves 75 minutes instead of 175; the one-minute minimum applies on leaving the field

Duarte Santos206d1939d059
diff --git a/frontend/src/components/ui.jsx b/frontend/src/components/ui.jsx
index 06a708e46..d63061079 100644
--- a/frontend/src/components/ui.jsx
+++ b/frontend/src/components/ui.jsx
@@ -126,8 +126,11 @@ export function Segmented({ options, value, onChange, className = '' }) {
/* ============================ stepper ============================ */
-export function Stepper({ value, step = 1, onChange, decimal = true, className = '', label, unit, invalid = false }) {
- const set = v => onChange(Math.max(0, Math.round((v || 0) * 100) / 100))
+// `min` holds for the buttons at once, but for typing only once the field is left: clamped on
+// every keystroke, a field emptied to type a new number snapped to the minimum, and the digits
+// typed next landed after it (a 48 retyped as 75 saved 175).
+export function Stepper({ value, step = 1, min = 0, onChange, decimal = true, className = '', label, unit, invalid = false }) {
+ const set = v => onChange(Math.max(min, Math.round((v || 0) * 100) / 100))
// Holding a button repeats the step; the latest value/step live in a ref so
// the interval doesn't keep stepping from the value it was started with.
const live = useRef({ value, step, set })
@@ -167,7 +170,7 @@ export function Stepper({ value, step = 1, onChange, decimal = true, className =
const inner = (
<div className={'stp ' + className}>
<button {...holdProps(-1)} aria-label="Decrease"><Icon name="minus" /></button>
- <span className="val">
+ <span className="val" onBlur={() => { if ((+value || 0) < min) onChange(min) }}>
<NumberField value={value} decimal={decimal} onChange={onChange} aria-invalid={invalid ? 'true' : undefined} />
{unit && <i>{unit}</i>}
</span>
diff --git a/frontend/src/components/ui.test.jsx b/frontend/src/components/ui.test.jsx
index e52f62d1f..488110bc3 100644
--- a/frontend/src/components/ui.test.jsx
+++ b/frontend/src/components/ui.test.jsx
@@ -153,6 +153,33 @@ describe('Stepper', () => {
expect(onChange).toHaveBeenCalledTimes(3) // nothing keeps ticking after release
})
+ // A minimum holds for the buttons at once but for typing only once the field is left, so a
+ // field can be emptied to type a new number without the minimum landing in front of it.
+ it('applies `min` to the buttons at once and to typing on leaving the field', () => {
+ const onChange = vi.fn()
+ function MinHost() {
+ const [v, setV] = React.useState(5)
+ return <Stepper value={v} step={5} min={1} decimal={false} onChange={n => { setV(n); onChange(n) }} />
+ }
+ act(() => root.render(<MinHost />))
+ const minus = host.querySelector('button[aria-label="Decrease"]')
+ const field = host.querySelector('input.num')
+ act(() => minus.click())
+ expect(onChange).toHaveBeenLastCalledWith(1)
+ const type = value => {
+ Object.getOwnPropertyDescriptor(field.constructor.prototype, 'value').set.call(field, value)
+ field.dispatchEvent(new Event('input', { bubbles: true }))
+ }
+ act(() => type(''))
+ expect(field.value).toBe('')
+ act(() => type('7'))
+ expect(field.value).toBe('7')
+ act(() => type(''))
+ act(() => field.dispatchEvent(new FocusEvent('focusout', { bubbles: true })))
+ expect(onChange).toHaveBeenLastCalledWith(1)
+ expect(field.value).toBe('1')
+ })
+
it('keeps working from the keyboard (click without a pointer)', () => {
const onChange = vi.fn()
const plus = mountStepper(10, onChange)
diff --git a/frontend/src/sheets.backfill.test.jsx b/frontend/src/sheets.backfill.test.jsx
index 501a085ce..e85ca6695 100644
--- a/frontend/src/sheets.backfill.test.jsx
+++ b/frontend/src/sheets.backfill.test.jsx
@@ -86,4 +86,26 @@ describe('log a past workout', () => {
expect(new Date(A.start).getHours()).toBe(18)
expect(A.backfill).toEqual({ durationMin: 60, replaceId: null })
})
+
+ // The same keystroke clamp as the saved workout's duration: a 60 emptied to type 45 read 145.
+ it('lets the duration be emptied and retyped, and never starts a session of no length', () => {
+ logPastWorkoutSheet()
+ const host = mountTopSheet()
+ act(() => { type(host.querySelector('input[type=date]'), '2020-01-02') })
+ const field = host.querySelector('input.num')
+ act(() => { type(field, '') })
+ expect(field.value).toBe('')
+ act(() => { type(field, '45') })
+ act(() => { button(host, 'Continue').click() })
+ expect(useStore.getState().S.active.backfill.durationMin).toBe(45)
+ })
+
+ it('an emptied duration still starts a one-minute session, not the default hour', () => {
+ logPastWorkoutSheet()
+ const host = mountTopSheet()
+ act(() => { type(host.querySelector('input[type=date]'), '2020-01-02') })
+ act(() => { type(host.querySelector('input.num'), '') })
+ act(() => { button(host, 'Continue').click() })
+ expect(useStore.getState().S.active.backfill.durationMin).toBe(1)
+ })
})
diff --git a/frontend/src/sheets.jsx b/frontend/src/sheets.jsx
index bbb321e1c..59ac266bb 100644
--- a/frontend/src/sheets.jsx
+++ b/frontend/src/sheets.jsx
@@ -1871,7 +1871,7 @@ function WorkoutDurationEdit({ w, onDone, close }) {
const save = () => {
let changed = false
update(s => {
- const next = setWorkoutDuration(s.workouts, w, dur)
+ const next = setWorkoutDuration(s.workouts, w, dur) // at least a minute, however the field was left
if (next) { s.workouts = next; changed = true }
})
close()
@@ -1882,7 +1882,7 @@ function WorkoutDurationEdit({ w, onDone, close }) {
return <>
<h3>{t('Change duration')}</h3>
<div className="muted small" style={{ marginBottom: 12 }}>{t('Forgot to finish on time? Set how long the session really took. It keeps its start time and its sets.')}</div>
- <Stepper label={t('Duration')} unit="min" value={dur} step={5} decimal={false} onChange={v => setDur(Math.max(1, Math.round(v)))} />
+ <Stepper label={t('Duration')} unit="min" value={dur} step={5} min={1} decimal={false} onChange={v => setDur(Math.round(v))} />
<div style={{ height: 18 }} />
<Button variant="primary" onClick={save}>{t('Save')}</Button>
</>
@@ -2142,7 +2142,7 @@ function LogPastWorkout({ initial, close }) {
const go = replaceId => {
close()
const routineIds = routineId === PLANNED_DAY ? planned.map(r => r.id) : routineId ? [routineId] : []
- beginBackfill({ iso: date, time, durationMin: dur, routineIds, replaceId })
+ beginBackfill({ iso: date, time, durationMin: Math.max(1, dur), routineIds, replaceId })
}
const submit = () => {
if (!date || date > today) { toast(t('Pick a day up to today')); return }
@@ -2159,7 +2159,7 @@ function LogPastWorkout({ initial, close }) {
<input type="date" className="timef" value={date} max={today} onChange={e => setDate(e.target.value)} /></Row>
<Row icon="clock" title={t('Start time')}>
<input type="time" className="timef" value={time} onChange={e => setTime(e.target.value)} /></Row>
- <Stepper label={t('Duration')} unit="min" value={dur} step={5} decimal={false} onChange={v => setDur(Math.max(1, Math.round(v)))} />
+ <Stepper label={t('Duration')} unit="min" value={dur} step={5} min={1} decimal={false} onChange={v => setDur(Math.round(v))} />
<div style={{ height: 8 }} />
<SelectRow icon="dumbbell" title={t('Routine')} value={routineId} options={options} onChange={setRoutineId} />
<div style={{ height: 18 }} />
diff --git a/frontend/src/sheets.workoutdate.test.jsx b/frontend/src/sheets.workoutdate.test.jsx
index 05c7093b8..5d649bff1 100644
--- a/frontend/src/sheets.workoutdate.test.jsx
+++ b/frontend/src/sheets.workoutdate.test.jsx
@@ -218,6 +218,33 @@ describe('changing the duration of a saved workout', () => {
expect(useUI.getState().toast).toHaveBeenCalledWith('Duration changed')
})
+ // The minimum of a minute used to apply on every keystroke: emptying the field put a 1 back
+ // at once, and the 75 typed after it read 175 — saved as 2h 55m without a word.
+ it('lets the field be emptied to type a new length, and saves what was typed', () => {
+ const host = render(() => workoutDurationSheet(history()[1]))
+ const field = host.querySelector('input.num')
+ act(() => { type(field, '3') })
+ act(() => { type(field, '') })
+ expect(field.value).toBe('')
+ act(() => { type(field, '75') })
+ expect(field.value).toBe('75')
+ act(() => { button(host, 'Save').click() })
+ const after = history()[1]
+ expect(after.end - after.start).toBe(75 * 60000)
+ })
+
+ it('a field left empty or at 0 comes back as one minute, and saves as one', () => {
+ const host = render(() => workoutDurationSheet(history()[1]))
+ const field = host.querySelector('input.num')
+ act(() => { type(field, '0') })
+ expect(field.value).toBe('0')
+ act(() => { field.dispatchEvent(new FocusEvent('focusout', { bubbles: true })) })
+ expect(field.value).toBe('1')
+ act(() => { button(host, 'Save').click() })
+ const after = history()[1]
+ expect(after.end - after.start).toBe(60000)
+ })
+
it('saving the same length closes and touches nothing', () => {
const before = history()
const host = render(() => workoutDurationSheet(before[1]))