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.

Plan: move a routine up or down, and every screen follows (#142)

Duarte Santos82646843c6c0
diff --git a/frontend/src/views/Plan.jsx b/frontend/src/views/Plan.jsx
index bacca3c0f..ef00d8db4 100644
--- a/frontend/src/views/Plan.jsx
+++ b/frontend/src/views/Plan.jsx
@@ -25,6 +25,15 @@ export default function Plan() {
an instance without the feature sees exactly the Plan screen it saw before. */
const showCoach = coachAvailable(config, user, { demo: DEMO, mobile: MOBILE, coachMode })
+ // Swap with the neighbour, the way the routine editor moves an exercise. `S.routines` is the
+ // one order the whole app reads, so this is all there is to it (#142).
+ const moveRoutine = (i, delta) => update(s => {
+ const to = i + delta
+ if (to < 0 || to >= s.routines.length) return
+ const [moved] = s.routines.splice(i, 1)
+ s.routines.splice(to, 0, moved)
+ })
+
const addRoutine = () => {
const r = { id: uid(), name: t('New routine'), emoji: DEFAULT_GLYPH, ex: [] }
update(s => { s.routines.push(r) })
@@ -83,9 +92,20 @@ export default function Plan() {
<h4 className="sec" style={{ margin: 0 }}>{t('Routines')}</h4>
<Button size="sm" variant="tinted" icon="plus" onClick={addRoutine}>{t('New')}</Button>
</div>
- {S.routines.length ? <div className="list">{S.routines.map(r => <div key={r.id} className="item" {...tappable(() => nav('/plan/r/' + r.id))}>
+ {S.routines.length ? <div className="list">{S.routines.map((r, i) => <div key={r.id} className="item" {...tappable(() => nav('/plan/r/' + r.id))}>
<span className="lrow-i"><Icon name={glyphOf(r.emoji)} /></span>
<div className="grow"><div className="tt">{r.name}</div><div className="ss">{exCount(r.ex.length)}</div></div>
+ {/* The order of this list is the order of `S.routines`, and every other screen reads the
+ same array — the Start screen, the day-assignment sheets, the routine pickers. So
+ moving a routine here moves it everywhere, which is what the request asked for (#142). */}
+ {S.routines.length > 1 && <div style={{ display: 'flex', gap: 2, flex: 'none' }}>
+ <button className="iconbtn" aria-label={t('Move up')} title={t('Move up')} disabled={i === 0}
+ style={{ width: 28, height: 24, borderRadius: 7, fontSize: 12 }}
+ onClick={ev => { ev.stopPropagation(); moveRoutine(i, -1) }}><Icon name="chevronUp" /></button>
+ <button className="iconbtn" aria-label={t('Move down')} title={t('Move down')} disabled={i === S.routines.length - 1}
+ style={{ width: 28, height: 24, borderRadius: 7, fontSize: 12 }}
+ onClick={ev => { ev.stopPropagation(); moveRoutine(i, 1) }}><Icon name="chevronDown" /></button>
+ </div>}
<Icon name="chevronRight" className="chev" /></div>)}</div> : <>
<div className="empty"><div className="ico"><Icon name="clipboard" /></div>{t('No routines yet.')}<br />{t('Create one or load the starter plan.')}</div>
<Button icon="sparkles" onClick={starterPlanSheet}>{t('Load starter plan')}</Button>
diff --git a/frontend/src/views/Plan.routine-order.test.jsx b/frontend/src/views/Plan.routine-order.test.jsx
new file mode 100644
index 000000000..63379b6b5
--- /dev/null
+++ b/frontend/src/views/Plan.routine-order.test.jsx
@@ -0,0 +1,55 @@
+// @vitest-environment happy-dom
+// Issue #142: routines were stuck in the order they were created. `S.routines` is the single
+// order every screen reads — Plan, the Start screen, the day-assignment sheets — so moving one
+// here moves it everywhere.
+import React, { act } from 'react'
+import { createRoot } from 'react-dom/client'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { useStore } from '../store/useStore.js'
+import Plan from './Plan.jsx'
+
+vi.mock('react-router-dom', () => ({ useNavigate: () => vi.fn() }))
+vi.mock('../sheets.jsx', () => ({
+ dayAssignSheet: vi.fn(), dayAddRoutineSheet: vi.fn(), starterPlanSheet: vi.fn(), planToolsSheet: vi.fn(),
+}))
+
+const routine = (id, name) => ({ id, name, emoji: null, ex: [{ id: '0025' }] })
+let host, root
+beforeEach(() => {
+ globalThis.IS_REACT_ACT_ENVIRONMENT = true
+ host = document.createElement('div'); document.body.appendChild(host); root = createRoot(host)
+ useStore.setState(s => ({ S: { ...s.S, routines: [routine('a', 'Push A'), routine('b', 'Pull A'), routine('c', 'Legs A')], week: {}, dayPlan: {} }, user: null }))
+})
+afterEach(() => { act(() => root.unmount()); host.remove() })
+
+const mount = () => act(() => root.render(<Plan />))
+// The weekday rows above the routine list share the `.item` class, so the rows are found by the
+// thing only a routine has: the move controls' own column.
+const rows = () => [...host.querySelectorAll('.item')].filter(e => e.querySelector('button[aria-label="Move up"]'))
+const names = () => rows().map(e => e.querySelector('.tt').textContent)
+const btn = (row, label) => rows()[row].querySelector(`button[aria-label="${label}"]`)
+const click = el => act(() => { el.dispatchEvent(new MouseEvent('click', { bubbles: true })) })
+
+describe('routine order', () => {
+ it('moves a routine down and the stored order follows', () => {
+ mount()
+ expect(names()).toEqual(['Push A', 'Pull A', 'Legs A'])
+ click(btn(0, 'Move down'))
+ expect(useStore.getState().S.routines.map(r => r.name)).toEqual(['Pull A', 'Push A', 'Legs A'])
+ expect(names()).toEqual(['Pull A', 'Push A', 'Legs A'])
+ })
+
+ it('moves one up, and the ends cannot go further', () => {
+ mount()
+ click(btn(2, 'Move up'))
+ expect(useStore.getState().S.routines.map(r => r.name)).toEqual(['Push A', 'Legs A', 'Pull A'])
+ expect(btn(0, 'Move up').disabled).toBe(true)
+ expect(btn(2, 'Move down').disabled).toBe(true)
+ })
+
+ it('leaves the arrows out when there is nothing to reorder', () => {
+ useStore.setState(s => ({ S: { ...s.S, routines: [routine('a', 'Only one')] } }))
+ mount()
+ expect(host.querySelector('button[aria-label="Move up"]')).toBe(null)
+ })
+})