Fix routine move controls for grouped exercises
This commit is contained in:
1 parent
5314b342ff
commit
933891ee99
17 files changed
+223
-5
No files matched your search
@@ -184,6 +184,7 @@ body.resting #app{padding-bottom:calc(250px + var(--sab))}
|
||||
}
|
||||
.iconbtn:active{background:var(--surface-2);transform:scale(.92)}
|
||||
.iconbtn.on-ss{background:var(--acc-soft);color:var(--acc)}
|
||||
.iconbtn:disabled{opacity:.32;pointer-events:none}
|
||||
|
||||
h4.sec{
|
||||
font-size:13px;font-weight:400;letter-spacing:-.004em;color:var(--label-2);
|
||||
|
||||
@@ -421,6 +421,22 @@ export function supersetUnits(items) {
|
||||
})
|
||||
return units
|
||||
}
|
||||
|
||||
// Move the selected occurrence's complete display unit by one neighbouring unit. Returning a
|
||||
// new array keeps this helper pure; the caller decides how to persist it. Index identity matters
|
||||
// here because the same exercise id may appear more than once with different setup.
|
||||
export function moveSupersetUnit(items, index, direction) {
|
||||
if (!Array.isArray(items) || (direction !== -1 && direction !== 1)) return null
|
||||
const units = supersetUnits(items)
|
||||
const source = units.findIndex(unit => unit.includes(index))
|
||||
const target = source + direction
|
||||
if (source < 0 || target < 0 || target >= units.length) return null
|
||||
const reordered = [...units]
|
||||
const selected = reordered[source]
|
||||
reordered[source] = reordered[target]
|
||||
reordered[target] = selected
|
||||
return reordered.flat().map(i => items[i])
|
||||
}
|
||||
export function unitOf(units, idx) { return units.find(u => u.includes(idx)) || [idx] }
|
||||
|
||||
export function streakWeeks(S) {
|
||||
|
||||
@@ -27,7 +27,7 @@ describe('Brazilian Portuguese locale', () => {
|
||||
.sort(byCodeUnit)
|
||||
const fingerprint = createHash('sha256').update(JSON.stringify(inherited)).digest('hex')
|
||||
|
||||
expect(Object.keys(PT_BR_OVERRIDES)).toHaveLength(291)
|
||||
expect(Object.keys(PT_BR_OVERRIDES)).toHaveLength(293)
|
||||
expect(inherited).toHaveLength(449)
|
||||
// If this fails, review the changed keys and wording before accepting a new hash. From
|
||||
// frontend/: node scripts/pt-br-inheritance-fingerprint.mjs --list
|
||||
|
||||
@@ -36,6 +36,8 @@ export default {
|
||||
'rescheduled': 'verschoben',
|
||||
'Prev': 'Zurück',
|
||||
'Next': 'Weiter',
|
||||
'Move up': 'Nach oben',
|
||||
'Move down': 'Nach unten',
|
||||
// --- days & months ---
|
||||
'Sunday': 'Sonntag', 'Monday': 'Montag', 'Tuesday': 'Dienstag', 'Wednesday': 'Mittwoch',
|
||||
'Thursday': 'Donnerstag', 'Friday': 'Freitag', 'Saturday': 'Samstag',
|
||||
|
||||
@@ -35,6 +35,8 @@ export default {
|
||||
'rescheduled': 'reprogramado',
|
||||
'Prev': 'Anterior',
|
||||
'Next': 'Siguiente',
|
||||
'Move up': 'Subir',
|
||||
'Move down': 'Bajar',
|
||||
'Sunday': 'Domingo', 'Monday': 'Lunes', 'Tuesday': 'Martes', 'Wednesday': 'Miércoles',
|
||||
'Thursday': 'Jueves', 'Friday': 'Viernes', 'Saturday': 'Sábado',
|
||||
'Su': 'Do', 'Mo': 'Lu', 'Tu': 'Ma', 'We': 'Mi', 'Th': 'Ju', 'Fr': 'Vi', 'Sa': 'Sá',
|
||||
|
||||
@@ -35,6 +35,8 @@ export default {
|
||||
'rescheduled': 'reprogrammé',
|
||||
'Prev': 'Préc.',
|
||||
'Next': 'Suiv.',
|
||||
'Move up': 'Monter',
|
||||
'Move down': 'Descendre',
|
||||
'Sunday': 'Dimanche', 'Monday': 'Lundi', 'Tuesday': 'Mardi', 'Wednesday': 'Mercredi',
|
||||
'Thursday': 'Jeudi', 'Friday': 'Vendredi', 'Saturday': 'Samedi',
|
||||
'Su': 'Di', 'Mo': 'Lu', 'Tu': 'Ma', 'We': 'Me', 'Th': 'Je', 'Fr': 'Ve', 'Sa': 'Sa',
|
||||
|
||||
@@ -35,6 +35,8 @@ export default {
|
||||
'rescheduled': 'बदला गया',
|
||||
'Prev': 'पिछला',
|
||||
'Next': 'अगला',
|
||||
'Move up': 'ऊपर ले जाएँ',
|
||||
'Move down': 'नीचे ले जाएँ',
|
||||
'Sunday': 'रविवार', 'Monday': 'सोमवार', 'Tuesday': 'मंगलवार', 'Wednesday': 'बुधवार',
|
||||
'Thursday': 'गुरुवार', 'Friday': 'शुक्रवार', 'Saturday': 'शनिवार',
|
||||
'Su': 'र', 'Mo': 'सो', 'Tu': 'मं', 'We': 'बु', 'Th': 'गु', 'Fr': 'शु', 'Sa': 'श',
|
||||
|
||||
@@ -35,6 +35,8 @@ export default {
|
||||
'rescheduled': 'riprogrammato',
|
||||
'Prev': 'Prec.',
|
||||
'Next': 'Succ.',
|
||||
'Move up': 'Sposta su',
|
||||
'Move down': 'Sposta giù',
|
||||
'Sunday': 'Domenica', 'Monday': 'Lunedì', 'Tuesday': 'Martedì', 'Wednesday': 'Mercoledì',
|
||||
'Thursday': 'Giovedì', 'Friday': 'Venerdì', 'Saturday': 'Sabato',
|
||||
'Su': 'Do', 'Mo': 'Lu', 'Tu': 'Ma', 'We': 'Me', 'Th': 'Gi', 'Fr': 'Ve', 'Sa': 'Sa',
|
||||
|
||||
@@ -35,6 +35,8 @@ export default {
|
||||
'rescheduled': '변경됨',
|
||||
'Prev': '이전',
|
||||
'Next': '다음',
|
||||
'Move up': '위로 이동',
|
||||
'Move down': '아래로 이동',
|
||||
'Sunday': '일요일', 'Monday': '월요일', 'Tuesday': '화요일', 'Wednesday': '수요일',
|
||||
'Thursday': '목요일', 'Friday': '금요일', 'Saturday': '토요일',
|
||||
'Su': '일', 'Mo': '월', 'Tu': '화', 'We': '수', 'Th': '목', 'Fr': '금', 'Sa': '토',
|
||||
|
||||
@@ -35,6 +35,8 @@ export default {
|
||||
'rescheduled': 'przełożono',
|
||||
'Prev': 'Wstecz',
|
||||
'Next': 'Dalej',
|
||||
'Move up': 'Przenieś w górę',
|
||||
'Move down': 'Przenieś w dół',
|
||||
'Sunday': 'Niedziela', 'Monday': 'Poniedziałek', 'Tuesday': 'Wtorek', 'Wednesday': 'Środa',
|
||||
'Thursday': 'Czwartek', 'Friday': 'Piątek', 'Saturday': 'Sobota',
|
||||
'Su': 'Nd', 'Mo': 'Pn', 'Tu': 'Wt', 'We': 'Śr', 'Th': 'Cz', 'Fr': 'Pt', 'Sa': 'So',
|
||||
|
||||
@@ -180,6 +180,8 @@ export const PT_BR_OVERRIDES = {
|
||||
|
||||
// Regional wording inherited from pt-PT would otherwise leak into pt-BR.
|
||||
'Next': 'Próximo',
|
||||
'Move up': 'Mover para cima',
|
||||
'Move down': 'Mover para baixo',
|
||||
'{0} to gain': '{0} para ganhar',
|
||||
'{0} to lose': '{0} para perder',
|
||||
'{0} set still unchecked. Finish the workout now?': 'Falta {0} série sem marcar. Finalizar o treino agora?',
|
||||
|
||||
@@ -35,6 +35,8 @@ export default {
|
||||
'rescheduled': 'reagendado',
|
||||
'Prev': 'Anterior',
|
||||
'Next': 'Seguinte',
|
||||
'Move up': 'Mover para cima',
|
||||
'Move down': 'Mover para baixo',
|
||||
'Sunday': 'Domingo', 'Monday': 'Segunda-feira', 'Tuesday': 'Terça-feira', 'Wednesday': 'Quarta-feira',
|
||||
'Thursday': 'Quinta-feira', 'Friday': 'Sexta-feira', 'Saturday': 'Sábado',
|
||||
'Su': 'Do', 'Mo': 'Se', 'Tu': 'Te', 'We': 'Qa', 'Th': 'Qi', 'Fr': 'Sx', 'Sa': 'Sá',
|
||||
|
||||
@@ -35,6 +35,8 @@ export default {
|
||||
'rescheduled': 'перенесено',
|
||||
'Prev': 'Назад',
|
||||
'Next': 'Далее',
|
||||
'Move up': 'Переместить вверх',
|
||||
'Move down': 'Переместить вниз',
|
||||
'Sunday': 'Воскресенье', 'Monday': 'Понедельник', 'Tuesday': 'Вторник', 'Wednesday': 'Среда',
|
||||
'Thursday': 'Четверг', 'Friday': 'Пятница', 'Saturday': 'Суббота',
|
||||
'Su': 'Вс', 'Mo': 'Пн', 'Tu': 'Вт', 'We': 'Ср', 'Th': 'Чт', 'Fr': 'Пт', 'Sa': 'Сб',
|
||||
|
||||
@@ -35,6 +35,8 @@ export default {
|
||||
'rescheduled': 'ertelendi',
|
||||
'Prev': 'Önceki',
|
||||
'Next': 'Sonraki',
|
||||
'Move up': 'Yukarı taşı',
|
||||
'Move down': 'Aşağı taşı',
|
||||
'Sunday': 'Pazar', 'Monday': 'Pazartesi', 'Tuesday': 'Salı', 'Wednesday': 'Çarşamba',
|
||||
'Thursday': 'Perşembe', 'Friday': 'Cuma', 'Saturday': 'Cumartesi',
|
||||
'Su': 'Pz', 'Mo': 'Pt', 'Tu': 'Sa', 'We': 'Ça', 'Th': 'Pe', 'Fr': 'Cu', 'Sa': 'Ct',
|
||||
|
||||
@@ -35,6 +35,8 @@ export default {
|
||||
'rescheduled': '已改期',
|
||||
'Prev': '上一个',
|
||||
'Next': '下一个',
|
||||
'Move up': '上移',
|
||||
'Move down': '下移',
|
||||
'Sunday': '星期日', 'Monday': '星期一', 'Tuesday': '星期二', 'Wednesday': '星期三',
|
||||
'Thursday': '星期四', 'Friday': '星期五', 'Saturday': '星期六',
|
||||
'Su': '日', 'Mo': '一', 'Tu': '二', 'We': '三', 'Th': '四', 'Fr': '五', 'Sa': '六',
|
||||
|
||||
@@ -5,7 +5,7 @@ import { exOr } from '../lib/exercises.js'
|
||||
import { activeProfile, exAvailable } from '../lib/equipment.js'
|
||||
import { uid } from '../lib/format.js'
|
||||
import { t, exerciseNameFor } from '../lib/i18n.js'
|
||||
import { supersetUnits, cleanupSg, exLine } from '../lib/history.js'
|
||||
import { supersetUnits, moveSupersetUnit, cleanupSg, exLine } from '../lib/history.js'
|
||||
import { Thumb } from '../components/Media.jsx'
|
||||
import { glyphPicker, exercisePicker, exConfigSheet, confirmSheet } from '../sheets.jsx'
|
||||
import Icon from '../components/Icon.jsx'
|
||||
@@ -29,7 +29,16 @@ export default function RoutineEdit() {
|
||||
if (!r) return null
|
||||
|
||||
const edit = fn => update(s => { fn(s.routines.find(x => x.id === id).ex) })
|
||||
const move = (i, dir) => edit(ex => { const j = i + dir; if (j < 0 || j >= ex.length) return;[ex[i], ex[j]] = [ex[j], ex[i]]; cleanupSg(ex) })
|
||||
const move = (i, dir) => {
|
||||
// Guard before update so a stale/boundary activation cannot trigger persistence or cleanup.
|
||||
if (!moveSupersetUnit(r.ex, i, dir)) return
|
||||
edit(ex => {
|
||||
const reordered = moveSupersetUnit(ex, i, dir)
|
||||
if (!reordered) return
|
||||
ex.splice(0, ex.length, ...reordered)
|
||||
cleanupSg(ex)
|
||||
})
|
||||
}
|
||||
const toggleLink = i => edit(ex => {
|
||||
if (i < 1) return
|
||||
const cur = ex[i], prev = ex[i - 1]
|
||||
@@ -39,6 +48,7 @@ export default function RoutineEdit() {
|
||||
})
|
||||
|
||||
const units = supersetUnits(r.ex)
|
||||
const unitIndex = new Map(units.flatMap((unit, index) => unit.map(i => [i, index])))
|
||||
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())
|
||||
const profile = activeProfile(S)
|
||||
@@ -88,8 +98,8 @@ export default function RoutineEdit() {
|
||||
<div style={{ display: 'flex', flexDirection: 'column', gap: 2, flex: 'none', alignItems: 'center' }}>
|
||||
{i > 0 && <button className={'iconbtn' + (linkedPrev ? ' on-ss' : '')} title={t('Superset with exercise above')} style={{ width: 32, height: 28, borderRadius: 8, fontSize: 15 }} onClick={ev => { ev.stopPropagation(); toggleLink(i) }}><Icon name="link" /></button>}
|
||||
<div style={{ display: 'flex', gap: 2 }}>
|
||||
<button className="iconbtn" aria-label="Move up" style={{ width: 28, height: 24, borderRadius: 7, fontSize: 12 }} onClick={ev => { ev.stopPropagation(); move(i, -1) }}><Icon name="chevronUp" /></button>
|
||||
<button className="iconbtn" aria-label="Move down" style={{ width: 28, height: 24, borderRadius: 7, fontSize: 12 }} onClick={ev => { ev.stopPropagation(); move(i, 1) }}><Icon name="chevronDown" /></button>
|
||||
<button className="iconbtn" aria-label={t('Move up')} title={t('Move up')} disabled={unitIndex.get(i) === 0} style={{ width: 28, height: 24, borderRadius: 7, fontSize: 12 }} onClick={ev => { ev.stopPropagation(); move(i, -1) }}><Icon name="chevronUp" /></button>
|
||||
<button className="iconbtn" aria-label={t('Move down')} title={t('Move down')} disabled={unitIndex.get(i) === units.length - 1} style={{ width: 28, height: 24, borderRadius: 7, fontSize: 12 }} onClick={ev => { ev.stopPropagation(); move(i, 1) }}><Icon name="chevronDown" /></button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
// @vitest-environment happy-dom
|
||||
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'
|
||||
import RoutineEdit from './RoutineEdit.jsx'
|
||||
import { DEF, useStore } from '../store/useStore.js'
|
||||
import { _setLangState } from '../lib/i18n-core.js'
|
||||
import de from '../locales/de.js'
|
||||
import { buildPlanBundle, parsePlan } from '../lib/plan-share.js'
|
||||
|
||||
const mocks = vi.hoisted(() => ({ exConfigSheet: vi.fn() }))
|
||||
vi.mock('../lib/api.js', () => ({ api: vi.fn(() => Promise.resolve({})) }))
|
||||
vi.mock('../sheets.jsx', () => ({
|
||||
glyphPicker: vi.fn(), exercisePicker: vi.fn(), exConfigSheet: mocks.exConfigSheet, confirmSheet: vi.fn()
|
||||
}))
|
||||
vi.mock('../components/Media.jsx', () => ({ Thumb: () => null }))
|
||||
vi.mock('../components/BodyMap.jsx', () => ({ default: () => null }))
|
||||
|
||||
globalThis.IS_REACT_ACT_ENVIRONMENT = true
|
||||
const clone = value => JSON.parse(JSON.stringify(value))
|
||||
const custom = id => ({ id, n: id, bp: 'chest', eq: 'body weight', tg: 'chest', custom: true })
|
||||
const entry = (id, setup, sg) => ({
|
||||
id, sets: 3, reps: 5, weight: setup, note: `setup-${setup}`,
|
||||
...(sg ? { sg } : {})
|
||||
})
|
||||
|
||||
let root
|
||||
let container
|
||||
|
||||
function setRoutine(ex) {
|
||||
const S = clone(DEF)
|
||||
S.routines = [{ id: 'r1', name: 'Move test', emoji: 'dumbbell', ex }]
|
||||
S.customEx = ['c1', 'c2', 'c3', 'c4'].map(custom)
|
||||
useStore.setState({ S, user: null })
|
||||
}
|
||||
|
||||
function renderRoutine() {
|
||||
container = document.createElement('div')
|
||||
document.body.appendChild(container)
|
||||
root = createRoot(container)
|
||||
act(() => root.render(
|
||||
<MemoryRouter initialEntries={['/routine/r1']}>
|
||||
<Routes><Route path="/routine/:id" element={<RoutineEdit />} /></Routes>
|
||||
</MemoryRouter>
|
||||
))
|
||||
}
|
||||
|
||||
function itemFor(name) {
|
||||
return [...container.querySelectorAll('.item')].find(item => item.textContent.includes(name))
|
||||
}
|
||||
|
||||
function moveButton(name, direction) {
|
||||
return itemFor(name).querySelector(`button[aria-label="${direction}"]`)
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
localStorage.clear()
|
||||
mocks.exConfigSheet.mockClear()
|
||||
_setLangState('en', null, null, null)
|
||||
root = null
|
||||
container = null
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
if (root) act(() => root.unmount())
|
||||
if (container) container.remove()
|
||||
_setLangState('en', null, null, null)
|
||||
})
|
||||
|
||||
describe('routine move controls', () => {
|
||||
it('moves a selected superset member as one contiguous unit and persists complete occurrences', () => {
|
||||
setRoutine([
|
||||
entry('c1', 10, 'g'),
|
||||
entry('c2', 20, 'g'),
|
||||
entry('c3', 30)
|
||||
])
|
||||
renderRoutine()
|
||||
|
||||
act(() => moveButton('setup-20', 'Move down').click())
|
||||
|
||||
const moved = useStore.getState().S.routines[0].ex
|
||||
expect(moved).toEqual([
|
||||
entry('c3', 30),
|
||||
entry('c1', 10, 'g'),
|
||||
entry('c2', 20, 'g')
|
||||
])
|
||||
expect(JSON.parse(localStorage.getItem('gym_state_v1')).routines[0].ex).toEqual(moved)
|
||||
})
|
||||
|
||||
it('moves the selected duplicate occurrence without aliasing its configuration', () => {
|
||||
setRoutine([
|
||||
entry('c1', 10),
|
||||
entry('c2', 20),
|
||||
entry('c1', 30)
|
||||
])
|
||||
renderRoutine()
|
||||
|
||||
act(() => moveButton('setup-30', 'Move up').click())
|
||||
|
||||
expect(useStore.getState().S.routines[0].ex).toEqual([
|
||||
entry('c1', 10),
|
||||
entry('c1', 30),
|
||||
entry('c2', 20)
|
||||
])
|
||||
})
|
||||
|
||||
it('disables unit-boundary directions without cleaning or persisting state', () => {
|
||||
setRoutine([
|
||||
entry('c1', 10, 'orphan'),
|
||||
entry('c2', 20)
|
||||
])
|
||||
renderRoutine()
|
||||
const before = useStore.getState().S
|
||||
|
||||
expect(moveButton('c1', 'Move up').disabled).toBe(true)
|
||||
expect(moveButton('c2', 'Move down').disabled).toBe(true)
|
||||
act(() => moveButton('c1', 'Move up').click())
|
||||
|
||||
expect(useStore.getState().S).toBe(before)
|
||||
expect(useStore.getState().S.routines[0].ex[0].sg).toBe('orphan')
|
||||
expect(localStorage.getItem('gym_state_v1')).toBeNull()
|
||||
})
|
||||
|
||||
it('uses localized accessible names and titles, retains button focus, and does not open config', () => {
|
||||
_setLangState('de', de, null, null)
|
||||
setRoutine([entry('c1', 10), entry('c2', 20)])
|
||||
renderRoutine()
|
||||
const button = moveButton('c2', 'Nach oben')
|
||||
|
||||
expect(button.title).toBe('Nach oben')
|
||||
button.focus()
|
||||
act(() => button.click())
|
||||
|
||||
expect(document.activeElement?.tagName).toBe('BUTTON')
|
||||
expect(mocks.exConfigSheet).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('retains reordered occurrence order and grouping through plan export and import', () => {
|
||||
setRoutine([
|
||||
entry('c1', 10, 'g'),
|
||||
entry('c2', 20, 'g'),
|
||||
entry('c3', 30)
|
||||
])
|
||||
renderRoutine()
|
||||
act(() => moveButton('setup-10', 'Move down').click())
|
||||
|
||||
const parsed = parsePlan(JSON.stringify(buildPlanBundle(useStore.getState().S, 'Move test')))
|
||||
expect(parsed.routines[0].ex).toEqual([
|
||||
entry('c3', 30),
|
||||
entry('c1', 10, 'g'),
|
||||
entry('c2', 20, 'g')
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe('routine move-control locale coverage', () => {
|
||||
const packs = import.meta.glob('../locales/*.js', { eager: true, import: 'default' })
|
||||
|
||||
it('defines both accessible names in all twelve locale packs', () => {
|
||||
expect(Object.keys(packs)).toHaveLength(12)
|
||||
Object.entries(packs).forEach(([path, pack]) => {
|
||||
expect(pack, `${path} is missing Move up`).toHaveProperty('Move up')
|
||||
expect(pack, `${path} is missing Move down`).toHaveProperty('Move down')
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user