feat(items): edit an item's quantity by swiping or right-clicking it

The PATCH endpoint now accepts quantity alongside completed (at least one
field is required), and the row shows the quantity only when its leading
number is above one. A small dialog edits it, opened by a left swipe on
touch or by right-clicking the row on desktop.
This commit is contained in:
Jose Selesan
2026-10-20 15:02:41 -03:00
parent 247ea903a9
commit db7cdd5585
10 changed files with 464 additions and 38 deletions

View File

@@ -1,7 +1,7 @@
import type { ListItem } from '@superlist/shared'
import { describe, expect, it } from 'vitest'
import { splitItems } from './items'
import { quantityLabel, splitItems } from './items'
function item(overrides: Partial<ListItem>): ListItem {
return {
@@ -85,3 +85,25 @@ describe('splitItems', () => {
expect(boughtToday).toEqual([])
})
})
describe('quantityLabel', () => {
it('shows quantities above one, with their unit when present', () => {
expect(quantityLabel('2')).toBe('2')
expect(quantityLabel('2 kg')).toBe('2 kg')
expect(quantityLabel('1,5')).toBe('1,5')
expect(quantityLabel(' 3 ')).toBe('3')
})
it('hides empty, absent and exactly-one quantities', () => {
expect(quantityLabel(null)).toBeNull()
expect(quantityLabel('')).toBeNull()
expect(quantityLabel('1')).toBeNull()
expect(quantityLabel('1 kg')).toBeNull()
expect(quantityLabel('0,5')).toBeNull()
})
it('hides quantities whose text does not start with a number', () => {
expect(quantityLabel('un par')).toBeNull()
expect(quantityLabel('kg')).toBeNull()
})
})

View File

@@ -38,3 +38,19 @@ export function splitItems(items: ListItem[], now: Date): SplitItems {
return { pending, boughtToday }
}
/**
* The quantity to show at the right of an item's name, or `null` when it must
* stay hidden. Only quantities above one earn their place in the row, so a
* plain "1" does not add noise: the leading number of the string is parsed
* ("2" → 2, "2 kg" → 2, "0,5" → 0.5) and anything without one is hidden.
*/
export function quantityLabel(quantity: string | null): string | null {
if (!quantity) return null
const trimmed = quantity.trim()
const leadingNumber = /^(\d+(?:[.,]\d+)?)/.exec(trimmed)
if (!leadingNumber) return null
return Number.parseFloat(leadingNumber[1].replace(',', '.')) > 1 ? trimmed : null
}