diff --git a/frontend/src/sheets.custom-target.test.jsx b/frontend/src/sheets.custom-target.test.jsx index 2b87ee7..68c8e00 100644 --- a/frontend/src/sheets.custom-target.test.jsx +++ b/frontend/src/sheets.custom-target.test.jsx @@ -110,4 +110,33 @@ describe('custom exercise target (QA C10)', () => { expect(c.primaries).toEqual(['gluteal']) expect(c.tg).toBe('gluteal') }) + + // Dropping the target used to hand it to the head of the map-sorted list, which is where C10 + // started: an upper-legs exercise read "Traps ยท barbell" again the moment its target went away. + it('hands a dropped target to the primary tapped in this sheet, not to the topmost muscle', () => { + const ex = custom({ tg: 'hamstring', primaries: ['trapezius', 'gluteal', 'hamstring'], secondaries: ['forearm'], sm: ['forearm'], + muscleGroups: ['trapezius', 'gluteal', 'hamstring', 'forearm'] }) + seed(ex) + customExSheet(EXIDX[ex.id]) + const form = renderTop() + pickMuscles(form, 'Primary muscle groups', ['Hamstrings', 'Quads']) // untick the target, tap another + click(form, 'button', 'Save') + const c = S().customEx[0] + expect(c.primaries).toEqual(['trapezius', 'gluteal', 'quadriceps']) + expect(c.tg).toBe('quadriceps') + }) + + // Tapping the same chip twice leaves the selection as it was, so it says nothing about what the + // user wants the target to be โ€” the muscle actually added does. + it('ignores a primary that was tapped on and off again', () => { + const ex = custom({ tg: 'hamstring', primaries: ['trapezius', 'hamstring'], secondaries: [], sm: [], muscleGroups: ['trapezius', 'hamstring'] }) + seed(ex) + customExSheet(EXIDX[ex.id]) + const form = renderTop() + pickMuscles(form, 'Primary muscle groups', ['Hamstrings', 'Chest', 'Chest', 'Glutes']) + click(form, 'button', 'Save') + const c = S().customEx[0] + expect(c.primaries).toEqual(['trapezius', 'gluteal']) + expect(c.tg).toBe('gluteal') + }) }) diff --git a/frontend/src/sheets.jsx b/frontend/src/sheets.jsx index e0faa62..b395342 100644 --- a/frontend/src/sheets.jsx +++ b/frontend/src/sheets.jsx @@ -780,7 +780,15 @@ function CustomExForm({ existing, prefill, onDone, close }) { const norm = hasExplicitMuscleMetadata(existing || {}) ? normalizeMuscleGroups(existing || {}) : [] return norm.slice(1) }) - const togglePrimary = value => setPrimaries(current => current.includes(value) ? current.filter(m => m !== value) : [...current, value]) + // Every primary chip this sheet saw a tap on, in that order. `primaries` alone cannot say what the + // user reached for first: an existing exercise seeds it in the map's order, because that is how the + // muscles are stored. The taps are the only record of the user's own order, and they decide the + // target when the one the exercise had is un-ticked. + const [primaryTaps, setPrimaryTaps] = useState([]) + const togglePrimary = value => { + setPrimaryTaps(current => [...current, value]) + setPrimaries(current => current.includes(value) ? current.filter(m => m !== value) : [...current, value]) + } const toggleSecondary = value => setSecondaries(current => current.includes(value) ? current.filter(m => m !== value) : [...current, value]) const save = () => { const name = n.trim() @@ -798,7 +806,10 @@ function CustomExForm({ existing, prefill, onDone, close }) { // The one-word target (the library row, the picker, the Muscles view) is not the sorted list's // head โ€” a hip thrust with Traps as an extra primary is not a Traps exercise. It is the primary // tapped first, and an edit keeps the exercise's target as long as that muscle is still a primary. - const tg = (existing && prim.includes(existing.tg)) ? existing.tg : (primaries.find(m => prim.includes(m)) || prim[0] || '') + // Once that muscle is gone the next answer is the first one tapped here that survived โ€” a tap that + // only turned a chip off says nothing and is skipped with it. The sorted list is the last resort, + // for the user who drops the target and adds nothing in its place. + const tg = (existing && prim.includes(existing.tg)) ? existing.tg : (primaryTaps.find(m => prim.includes(m)) || prim[0] || '') let id = existing && existing.id if (existing) update(s => { const c = (s.customEx || []).find(x => x.id === id); if (c) { c.n = name; c.bp = bp; c.desc = d; c.tg = tg; c.sm = sm; c.muscleGroups = groups; c.primaries = prim; c.secondaries = sm; c.eq = eq