Merge feat/favourites: Favoriten-Uebungen (#6)
# Conflicts: # frontend/src/lib/pt-br-locale.test.js
This commit is contained in:
commit
d021d9b5f3
23 files changed
+350
-14
No files matched your search
@@ -0,0 +1,29 @@
|
||||
// Favourite exercises (issue #6): a personal shortlist that floats to the top of the picker,
|
||||
// the Library and the muscle explorer so building a routine takes fewer scrolls.
|
||||
//
|
||||
// Stored as a flat id list in synced state (S.favEx) — catalogue or custom ids alike. It is
|
||||
// personal, so it stays out of shared plan bundles (lib/plan-share.js) on purpose. Profiles
|
||||
// written before the field existed simply have no list, hence every reader tolerates undefined.
|
||||
|
||||
export const favIds = S => (Array.isArray(S?.favEx) ? S.favEx : [])
|
||||
|
||||
export const isFav = (S, id) => favIds(S).includes(id)
|
||||
|
||||
/** Flip one exercise in a state draft. Returns true when it is a favourite afterwards. */
|
||||
export function toggleFav(s, id) {
|
||||
const list = favIds(s)
|
||||
const on = !list.includes(id)
|
||||
s.favEx = on ? [...list, id] : list.filter(x => x !== id)
|
||||
return on
|
||||
}
|
||||
|
||||
/**
|
||||
* Favourites first, everything else after — both halves keep the order they came in, so a
|
||||
* list that is already sorted by name (or by usage) stays that way within each half.
|
||||
*/
|
||||
export function sortFavouritesFirst(list, S) {
|
||||
const fav = favIds(S)
|
||||
if (!fav.length) return list
|
||||
const set = new Set(fav)
|
||||
return [...list.filter(e => set.has(e.id)), ...list.filter(e => !set.has(e.id))]
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { favIds, isFav, toggleFav, sortFavouritesFirst } from './favourites.js'
|
||||
|
||||
const ex = id => ({ id, n: id })
|
||||
|
||||
describe('favourites', () => {
|
||||
it('reads a profile without the field as no favourites', () => {
|
||||
expect(favIds({})).toEqual([])
|
||||
expect(favIds({ favEx: null })).toEqual([])
|
||||
expect(isFav({}, '0001')).toBe(false)
|
||||
})
|
||||
|
||||
it('toggles an id in and out of the list and reports the new state', () => {
|
||||
const s = {}
|
||||
expect(toggleFav(s, '0001')).toBe(true)
|
||||
expect(toggleFav(s, 'c123')).toBe(true)
|
||||
expect(s.favEx).toEqual(['0001', 'c123'])
|
||||
expect(isFav(s, '0001')).toBe(true)
|
||||
expect(toggleFav(s, '0001')).toBe(false)
|
||||
expect(s.favEx).toEqual(['c123'])
|
||||
})
|
||||
|
||||
it('moves favourites to the front and keeps both halves in their original order', () => {
|
||||
const list = ['a', 'b', 'c', 'd', 'e'].map(ex)
|
||||
const S = { favEx: ['d', 'b', 'zzz-not-listed'] }
|
||||
expect(sortFavouritesFirst(list, S).map(e => e.id)).toEqual(['b', 'd', 'a', 'c', 'e'])
|
||||
})
|
||||
|
||||
it('returns the very same list when there are no favourites', () => {
|
||||
const list = ['a', 'b'].map(ex)
|
||||
expect(sortFavouritesFirst(list, {})).toBe(list)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user