diff --git a/frontend/src/views/Plan.jsx b/frontend/src/views/Plan.jsx index bacca3c..ef00d8d 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() {

{t('Routines')}

- {S.routines.length ?
{S.routines.map(r =>
nav('/plan/r/' + r.id))}> + {S.routines.length ?
{S.routines.map((r, i) =>
nav('/plan/r/' + r.id))}>
{r.name}
{exCount(r.ex.length)}
+ {/* 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 &&
+ + +
}
)}
: <>
{t('No routines yet.')}
{t('Create one or load the starter plan.')}
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 0000000..63379b6 --- /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()) +// 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) + }) +})