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.

Routine editor: Add exercise heads the exercise list

Duarte Santosbd2843a3c93a
diff --git a/frontend/src/views/RoutineEdit.add-top.test.jsx b/frontend/src/views/RoutineEdit.add-top.test.jsx
new file mode 100644
index 000000000..edec02f17
--- /dev/null
+++ b/frontend/src/views/RoutineEdit.add-top.test.jsx
@@ -0,0 +1,69 @@
+// @vitest-environment happy-dom
+// v1.3.11: "Add exercise" heads the routine's exercise list. Under the last exercise it had to be
+// scrolled to on every long routine before anything could be added. Both ways of adding still
+// work: "+" on a picker row adds at the routine's defaults, a tap opens the settings first.
+import React, { act } from 'react'
+import { createRoot } from 'react-dom/client'
+import { MemoryRouter, Route, Routes } from 'react-router-dom'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+vi.mock('../lib/api.js', () => ({ api: vi.fn(() => Promise.resolve({})) }))
+vi.mock('../sheets.jsx', () => ({ exConfigSheet: vi.fn(), exercisePicker: vi.fn(), glyphPicker: vi.fn(), confirmSheet: vi.fn() }))
+vi.mock('../components/Media.jsx', () => ({ Thumb: () => null }))
+vi.mock('../components/BodyMap.jsx', () => ({ default: () => null }))
+
+import RoutineEdit from './RoutineEdit.jsx'
+import { DEF, useStore } from '../store/useStore.js'
+import { exercisePicker, exConfigSheet } from '../sheets.jsx'
+import { EXIDX } from '../lib/exercises.js'
+
+globalThis.IS_REACT_ACT_ENVIRONMENT = true
+let root, host
+
+function mount(ex) {
+ const S = JSON.parse(JSON.stringify(DEF))
+ S.routines = [{ id: 'r1', name: 'Push day', emoji: 'dumbbell', ex }]
+ useStore.setState({ S, user: null })
+ host = document.createElement('div')
+ document.body.appendChild(host)
+ root = createRoot(host)
+ act(() => root.render(<MemoryRouter initialEntries={['/plan/r/r1']}><Routes><Route path="/plan/r/:id" element={<RoutineEdit />} /></Routes></MemoryRouter>))
+}
+const addButton = () => [...host.querySelectorAll('button')].find(b => b.textContent.trim() === 'Add exercise')
+const stored = () => useStore.getState().S.routines[0].ex
+
+beforeEach(() => { vi.clearAllMocks() })
+afterEach(() => { act(() => root.unmount()); host.remove() })
+
+describe('RoutineEdit — Add exercise at the top', () => {
+ it('sits above the first exercise, with the list heading', () => {
+ mount([{ id: '0025', sets: 3, mode: 'reps', reps: 5, weight: 80 }, { id: '0025', sets: 3, mode: 'reps', reps: 8, weight: 60 }])
+ const add = addButton()
+ const firstRow = host.querySelector('[data-routine-row]')
+ expect(add.compareDocumentPosition(firstRow) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy()
+ expect(add.closest('.routine-ex-h').querySelector('h4').textContent).toBe('Exercises')
+ // and there is only the one
+ expect([...host.querySelectorAll('button')].filter(b => b.textContent.trim() === 'Add exercise').length).toBe(1)
+ })
+
+ it('is there on an empty routine too', () => {
+ mount([])
+ expect(addButton()).toBeTruthy()
+ expect(host.textContent).toContain('No exercises yet.')
+ })
+
+ it('adds at the defaults from "+", and through the settings from a tap', () => {
+ mount([])
+ act(() => addButton().click())
+ const onPick = exercisePicker.mock.calls[0][0]
+ const ex = EXIDX['0025']
+ act(() => onPick(ex, true))
+ expect(stored().map(e => e.id)).toEqual(['0025'])
+ act(() => onPick(ex, false))
+ expect(exConfigSheet).toHaveBeenCalledOnce()
+ const save = exConfigSheet.mock.calls[0][2]
+ act(() => save({ sets: 4, reps: 6, mode: 'reps', weight: 50 }))
+ expect(stored()).toHaveLength(2)
+ expect(stored()[1]).toMatchObject({ id: '0025', sets: 4, reps: 6 })
+ })
+})
diff --git a/frontend/src/views/RoutineEdit.jsx b/frontend/src/views/RoutineEdit.jsx
index 8f6dbd6be..9913b9162 100644
--- a/frontend/src/views/RoutineEdit.jsx
+++ b/frontend/src/views/RoutineEdit.jsx
@@ -389,6 +389,15 @@ export default function RoutineEdit() {
cleanupSg(ex)
})
+ const addExercise = () => exercisePicker((ex, quick) => {
+ if (quick) {
+ edit(x => x.push({ id: ex.id, ...defaultConfig(ex.id) }))
+ toast(t('“{0}” added to {1}', exerciseNameText(ex), r.name))
+ } else {
+ exConfigSheet(ex, null, cfg => edit(x => { x.push({ id: ex.id, ...cfg }) }), null, r)
+ }
+ })
+
const units = supersetUnits(r.ex)
const unitFirst = new Set(units.filter(u => u.length > 1).map(u => u[0]))
const inSS = new Set(units.filter(u => u.length > 1).flat())
@@ -436,6 +445,12 @@ export default function RoutineEdit() {
</div>
</div>}
+ {/* "Add exercise" heads the list (v1.3.11): with it under the last exercise, a long routine
+ had to be scrolled to the bottom before anything could be added. */}
+ <div className="row between plan-sec-h routine-ex-h">
+ <h4 className="sec">{t('Exercises')}</h4>
+ <Button size="sm" variant="tinted" icon="plus" onClick={addExercise}>{t('Add exercise')}</Button>
+ </div>
{r.ex.length ? <div ref={reorder.listRef} onClickCapture={reorder.onClickCapture}
className={'list routine-list' + (reorder.drag ? ' is-reordering' : '')}>{r.ex.map((e, i) => {
// An unresolvable id is shown rather than skipped — hiding it left an entry you
@@ -491,15 +506,6 @@ export default function RoutineEdit() {
})()}
<div className="small dim row" style={{ margin: '10px 2px', gap: 5 }}><Icon name="link" style={{ fontSize: 13 }} />{t('Tap the link button on an exercise to superset it with the one above. You’ll do them back-to-back.')}</div>
- <Button variant="primary" onClick={() => exercisePicker((ex, quick) => {
- if (quick) {
- edit(x => x.push({ id: ex.id, ...defaultConfig(ex.id) }))
- toast(t('“{0}” added to {1}', exerciseNameText(ex), r.name))
- } else {
- exConfigSheet(ex, null, cfg => edit(x => { x.push({ id: ex.id, ...cfg }) }), null, r)
- }
- })} icon="plus">{t('Add exercise')}</Button>
- <div style={{ height: 10 }} />
<Button onClick={() => {
const copy = copyRoutine(r, t('Copy'))
update(s => { s.routines.push(copy) })