Merge !120: the equipment filter survives a body-part change in the exercise picker, #150 (ErrorUsernameAlreadyTaken)

This commit is contained in:
Duarte Santos committed 2026-09-12 22:54:14 +02:00
commit f472f37c3d
3 files changed
+74 -3

No files matched your search

+6 -2
View File
@@ -920,11 +920,15 @@ function ExercisePicker({ onPick, close }) {
{showAll ? t('Filter by "{0}"', profile.name) : t('Show all equipment')}
</button>
</div>}
{/* Changing muscle group keeps the equipment filter: if it still has exercises under the new
group the filter stays applied, and if not the eqOn fallback above drops it for this view
without forgetting the choice (issue #71). The favourites/chosen chips still clear it —
those are cross-body-part views where a stale equipment filter would be confusing. */}
<div className="chips" ref={bpStrip} style={{ margin: eqOpts.length > 1 ? '10px 0 6px' : '10px 0' }}>
{favCount > 0 && <button className={'chip' + (bp === '☆' ? ' on' : '')} onClick={() => { setBp('☆'); setEq(''); setShown(50) }}><Icon name="starFill" className="fav-star" />{t('Favourites')} ({favCount})</button>}
{chosenCount > 0 && <button className={'chip' + (bp === '★' ? ' on' : '')} onClick={() => { setBp('★'); setEq(''); setShown(50) }}><Icon name="starFill" style={{ fontSize: 12, display: 'inline-block', marginRight: 4, verticalAlign: '-1px' }} />{t('Chosen')} ({chosenCount})</button>}
<button className={'chip nocap' + (!bp ? ' on' : '')} onClick={() => { setBp(''); setEq(''); setShown(50) }}>{t('All')}</button>
{BODYPARTS.map(b => <button key={b} className={'chip' + (bp === b ? ' on' : '')} onClick={() => { setBp(b); setEq(''); setShown(50) }}>{t(b)}</button>)}
<button className={'chip nocap' + (!bp ? ' on' : '')} onClick={() => { setBp(''); setShown(50) }}>{t('All')}</button>
{BODYPARTS.map(b => <button key={b} className={'chip' + (bp === b ? ' on' : '')} onClick={() => { setBp(b); setShown(50) }}>{t(b)}</button>)}
</div>
{eqOpts.length > 1 && <div className="chips" ref={eqStrip} style={{ marginBottom: 10 }}>
<button className={'chip nocap' + (!eqOn ? ' on' : '')} onClick={() => { setEq(''); setShown(50) }}>{t('Any equipment')}</button>
@@ -0,0 +1,64 @@
// @vitest-environment happy-dom
// Issue #71: choosing a body part after picking an equipment type used to reset the equipment
// filter to "all equipment", even when the new muscle group still had exercises for it. The
// per-body-part chips no longer clear the equipment selection; the eqOn fallback only drops it
// for the current view when the new body part has nothing under that equipment.
import React, { act } from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { createRoot } from 'react-dom/client'
import { DEF, useStore } from './store/useStore.js'
import { useUI } from './store/useUI.js'
import { exercisePicker } from './sheets.jsx'
const mounted = []
function renderTop() {
const sheet = useUI.getState().sheets.at(-1)
const host = document.createElement('div')
document.body.appendChild(host)
const root = createRoot(host)
mounted.push(root)
act(() => root.render(sheet.render(() => useUI.getState().closeSheet(sheet.id))))
return host
}
// The two chip strips render in order: body parts first, then equipment. Grab a chip by its
// visible label out of the whole picker.
const chipByText = (host, text) =>
[...host.querySelectorAll('.chips .chip')].find(b => b.textContent.trim() === text)
const isOn = el => el.className.split(/\s+/).includes('on')
beforeEach(() => {
globalThis.IS_REACT_ACT_ENVIRONMENT = true
useUI.setState({ sheets: [], toastMsg: '' })
useStore.setState({ S: structuredClone(DEF), user: null })
document.body.innerHTML = ''
})
afterEach(() => {
act(() => { mounted.splice(0).forEach(root => root.unmount()) })
})
describe('exercise picker equipment filter', () => {
// The exact reproduction from issue #71: "lower arms" + "dumbbell", then switch to "upper arms".
// Both body parts have dumbbell exercises, so the filter must stay selected across the switch.
it('keeps the dumbbell filter when switching from lower arms to upper arms', () => {
exercisePicker(vi.fn())
const host = renderTop()
// lower arms → dumbbell
act(() => chipByText(host, 'lower arms').click())
const eqChip = chipByText(host, 'dumbbell')
expect(eqChip, 'dumbbell chip should be offered under lower arms').toBeTruthy()
act(() => eqChip.click())
expect(isOn(chipByText(host, 'dumbbell'))).toBe(true)
// switch to upper arms — dumbbell is still valid there, so it must remain selected
act(() => chipByText(host, 'upper arms').click())
expect(isOn(chipByText(host, 'upper arms'))).toBe(true)
const eqAfter = chipByText(host, 'dumbbell')
expect(eqAfter, 'dumbbell chip should still be present under upper arms').toBeTruthy()
expect(isOn(eqAfter)).toBe(true)
expect(isOn(chipByText(host, 'Any equipment'))).toBe(false)
})
})
+4 -1
View File
@@ -47,9 +47,12 @@ export default function Library() {
{showAll ? t('Filter by "{0}"', profile.name) : t('Show all equipment')}
</button>
</div>}
{/* Changing body part keeps the equipment filter (issue #71): the eqOn fallback above drops
it only for the current view if the new body part has nothing under it, without forgetting
the choice. "All" clears it, since it spans every body part. */}
<div className="chips" ref={bpStrip} style={{ marginBottom: eqOpts.length > 1 ? 8 : 12 }}>
<button className={'chip nocap' + (!bp ? ' on' : '')} onClick={() => { setBp(''); setEq(''); setShown(40) }}>{t('All')}</button>
{BODYPARTS.map(b => <button key={b} className={'chip' + (bp === b ? ' on' : '')} onClick={() => { setBp(b); setEq(''); setShown(40) }}>{t(b)}</button>)}
{BODYPARTS.map(b => <button key={b} className={'chip' + (bp === b ? ' on' : '')} onClick={() => { setBp(b); setShown(40) }}>{t(b)}</button>)}
</div>
{eqOpts.length > 1 && <div className="chips" ref={eqStrip} style={{ marginBottom: 12 }}>
<button className={'chip nocap' + (!eqOn ? ' on' : '')} onClick={() => { setEq(''); setShown(40) }}>{t('Any equipment')}</button>