fix: stop rest timer when removing active exercise
This commit is contained in:
1 parent
5314b342ff
commit
03a5bd22ff
2 files changed
+95
-2
No files matched your search
@@ -258,9 +258,10 @@ function ExerciseBlock({ entryIdx, compact, onToggle, onField, onAddSet, onRemov
|
||||
|
||||
/* ---------- active workout ---------- */
|
||||
export function removeActiveExercise(idx) {
|
||||
// Clear the work callback before indexes can shift. This also protects a confirmation sheet
|
||||
// that was opened first and confirmed after a timed hold started.
|
||||
// Clear timer state and callbacks before indexes can shift. This also protects a confirmation
|
||||
// sheet that was opened first and confirmed after a timed hold or rest countdown started.
|
||||
useUI.getState().stopWork()
|
||||
useUI.getState().stopRest()
|
||||
useStore.getState().update(s => {
|
||||
if (!s.active || !Array.isArray(s.active.entries)) return
|
||||
if (idx < 0 || idx >= s.active.entries.length) return
|
||||
|
||||
@@ -21,6 +21,8 @@ const entry = (id, sg) => ({
|
||||
|
||||
let root
|
||||
let container
|
||||
let sheetRoot
|
||||
let sheetContainer
|
||||
|
||||
function setActive(entries, cur = 0) {
|
||||
const S = clone(DEF)
|
||||
@@ -43,6 +45,18 @@ function removeButton() {
|
||||
return [...container.querySelectorAll('button')].find(button => button.textContent.includes('Remove exercise'))
|
||||
}
|
||||
|
||||
function renderTopSheet() {
|
||||
if (sheetRoot) act(() => sheetRoot.unmount())
|
||||
if (sheetContainer) sheetContainer.remove()
|
||||
const sheet = useUI.getState().sheets.at(-1)
|
||||
expect(sheet).toBeTruthy()
|
||||
sheetContainer = document.createElement('div')
|
||||
document.body.appendChild(sheetContainer)
|
||||
sheetRoot = createRoot(sheetContainer)
|
||||
act(() => sheetRoot.render(sheet.render(() => useUI.getState().closeSheet(sheet.id))))
|
||||
return sheetContainer
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers()
|
||||
localStorage.clear()
|
||||
@@ -52,9 +66,13 @@ beforeEach(() => {
|
||||
useStore.setState({ S: clone(DEF), user: null })
|
||||
root = null
|
||||
container = null
|
||||
sheetRoot = null
|
||||
sheetContainer = null
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
if (sheetRoot) act(() => sheetRoot.unmount())
|
||||
if (sheetContainer) sheetContainer.remove()
|
||||
if (root) act(() => root.unmount())
|
||||
if (container) container.remove()
|
||||
useUI.getState().stopRest()
|
||||
@@ -94,6 +112,58 @@ describe('active-session exercise removal', () => {
|
||||
expect(active.entries[0].sets[0].sec).toBeUndefined()
|
||||
})
|
||||
|
||||
it('cancels a running rest countdown before removing the selected occurrence', () => {
|
||||
setActive([entry('1001'), entry('1002')], 0)
|
||||
useUI.getState().startRest(90)
|
||||
expect(useUI.getState().timer).not.toBeNull()
|
||||
|
||||
act(() => { removeActiveExercise(0) })
|
||||
|
||||
expect(useUI.getState().timer).toBeNull()
|
||||
expect(useStore.getState().S.active.entries.map(e => e.id)).toEqual(['1002'])
|
||||
})
|
||||
|
||||
it('does not mutate before confirmation, leaves state unchanged on Cancel, and removes on Confirm', () => {
|
||||
renderWorkout([entry('1001'), entry('1002')])
|
||||
|
||||
act(() => removeButton().click())
|
||||
expect(useStore.getState().S.active.entries.map(e => e.id)).toEqual(['1001', '1002'])
|
||||
|
||||
let dialog = renderTopSheet()
|
||||
const cancel = [...dialog.querySelectorAll('button')].find(button => button.textContent === 'Cancel')
|
||||
act(() => cancel.click())
|
||||
expect(useStore.getState().S.active.entries.map(e => e.id)).toEqual(['1001', '1002'])
|
||||
|
||||
act(() => removeButton().click())
|
||||
dialog = renderTopSheet()
|
||||
const confirm = [...dialog.querySelectorAll('button')].find(button => button.textContent === 'Remove')
|
||||
act(() => confirm.click())
|
||||
expect(useStore.getState().S.active.entries.map(e => e.id)).toEqual(['1002'])
|
||||
})
|
||||
|
||||
it('uses the selected superset member occurrence rather than its exercise id', () => {
|
||||
renderWorkout([entry('1001', 'pair'), entry('1001', 'pair'), entry('1002')])
|
||||
useStore.getState().update(s => {
|
||||
s.active.entries[0].target.marker = 'keep-first'
|
||||
s.active.entries[1].target.marker = 'remove-second'
|
||||
}, false)
|
||||
|
||||
act(() => removeButton().click())
|
||||
const chooser = renderTopSheet()
|
||||
const choices = [...chooser.querySelectorAll('.item')]
|
||||
expect(choices).toHaveLength(2)
|
||||
act(() => choices[1].click())
|
||||
|
||||
const dialog = renderTopSheet()
|
||||
const confirm = [...dialog.querySelectorAll('button')].find(button => button.textContent === 'Remove')
|
||||
act(() => confirm.click())
|
||||
|
||||
const active = useStore.getState().S.active
|
||||
expect(active.entries.map(e => e.id)).toEqual(['1001', '1002'])
|
||||
expect(active.entries[0].target.marker).toBe('keep-first')
|
||||
expect(active.entries[0].sg).toBeUndefined()
|
||||
})
|
||||
|
||||
it('hides the remove control for an empty freestyle session', () => {
|
||||
renderWorkout([])
|
||||
expect(removeButton()).toBeUndefined()
|
||||
@@ -143,4 +213,26 @@ describe('remove-exercise edge cases', () => {
|
||||
expect(A.entries.map(e => e.id)).toEqual(['b', 'c'])
|
||||
expect(A.entries[A.cur].id).toBe('c')
|
||||
})
|
||||
|
||||
it('persists the shortened active session for reload without changing completed history', () => {
|
||||
const first = entry('same')
|
||||
first.target.marker = 'keep-first'
|
||||
const second = entry('same')
|
||||
second.target.marker = 'remove-second'
|
||||
setActive([first, second, entry('other')], 1)
|
||||
const workouts = [{ id: 'completed', d: '2026-08-10', entries: [{ id: 'same', sets: [{ w: 42, r: 5, done: true }] }] }]
|
||||
useStore.setState(state => ({ S: { ...state.S, workouts: clone(workouts) } }))
|
||||
|
||||
act(() => { removeActiveExercise(1) })
|
||||
|
||||
const persisted = JSON.parse(localStorage.getItem('gym_state_v1'))
|
||||
expect(persisted.workouts).toEqual(workouts)
|
||||
expect(persisted.active.entries.map(e => e.id)).toEqual(['same', 'other'])
|
||||
expect(persisted.active.entries[0].target.marker).toBe('keep-first')
|
||||
expect(persisted.active.cur).toBe(1)
|
||||
|
||||
useStore.setState({ S: Object.assign(clone(DEF), persisted), user: null })
|
||||
expect(useStore.getState().S.active.entries.map(e => e.id)).toEqual(['same', 'other'])
|
||||
expect(useStore.getState().S.workouts).toEqual(workouts)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user