feat(items): edit quantity and notes inline, with swipe feedback

The PATCH endpoint now also accepts notes. Swiping an item to the left (or
right-clicking it) opens an inline editor below the row for its quantity and
notes, focused on the quantity input, instead of the quantity dialog. The row
slides with the finger during the swipe and snaps back on release, and only
one row edits at a time. Toggling an item no longer moves the focus to the
add-item input, and notes show under the item's name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jose Selesan
2026-10-20 18:30:04 -03:00
parent db7cdd5585
commit d02ab28fc5
10 changed files with 332 additions and 181 deletions

View File

@@ -9,7 +9,7 @@ import type { AppEnv } from '@/types'
import { ItemParamsSchema, UpdateItemSchema } from './schema'
import { UpdateItem } from './use-case'
/** PATCH /lists/:listId/items/:itemId — updates an item's bought state and/or quantity. */
/** PATCH /lists/:listId/items/:itemId — updates an item's bought state, quantity and/or notes. */
export const updateItemRoute = new Hono<AppEnv>().patch(
'/:itemId',
requireSession,
@@ -17,13 +17,14 @@ export const updateItemRoute = new Hono<AppEnv>().patch(
validate.json(UpdateItemSchema),
async (c) => {
const { listId, itemId } = c.req.valid('param')
const { completed, quantity } = c.req.valid('json')
const { completed, quantity, notes } = c.req.valid('json')
const result = await new UpdateItem(prisma).execute({
listId,
itemId,
userId: c.get('user').id,
completed,
quantity,
notes,
})
return resultJson(c, result)

View File

@@ -13,9 +13,14 @@ export const UpdateItemSchema = z
.object({
completed: z.boolean().optional(),
quantity: z.string().trim().max(40, 'Máximo 40 caracteres').nullable().optional(),
notes: z.string().trim().max(280, 'Máximo 280 caracteres').nullable().optional(),
})
.refine((value) => value.completed !== undefined || value.quantity !== undefined, {
message: 'Indica al menos un campo para actualizar.',
})
.refine(
(value) =>
value.completed !== undefined || value.quantity !== undefined || value.notes !== undefined,
{
message: 'Indica al menos un campo para actualizar.',
},
)
export type UpdateItemInput = z.infer<typeof UpdateItemSchema>

View File

@@ -48,7 +48,7 @@ function createDb(options: { membership: unknown; item: ItemRow | null }) {
// The use case only sends the fields it changes, so the stored row is
// simulated by merging those fields into the row the query returned.
update: async (args: {
data: Partial<Pick<ItemRow, 'completed' | 'boughtAt' | 'quantity'>>
data: Partial<Pick<ItemRow, 'completed' | 'boughtAt' | 'quantity' | 'notes'>>
}) => {
updates.push(args)
return { ...options.item, ...args.data } as ItemRow
@@ -170,6 +170,45 @@ describe('UpdateItem', () => {
expect(result.value.quantity).toBeNull()
})
it('updates only the notes, leaving the rest untouched', async () => {
const { db, updates } = createDb({ membership: { role: 'owner' }, item: itemRow(false, null) })
const { events, published } = createBus()
const result = await new UpdateItem(db, events).execute({
listId: 'list-1',
itemId: 'item-1',
userId: 'user-1',
notes: ' sin lactosa ',
})
expect(result.ok).toBe(true)
if (!result.ok) throw new Error('expected the use case to succeed')
const data = updates[0] as { data: { notes?: string | null } }
expect(data.data).toEqual({ notes: 'sin lactosa' })
expect(result.value.notes).toBe('sin lactosa')
expect(result.value.quantity).toBe('1 L')
expect(published).toEqual([{ type: 'item.updated', listId: 'list-1', item: result.value }])
})
it('clears the notes when an empty value is sent', async () => {
const { db, updates } = createDb({ membership: { role: 'owner' }, item: itemRow(false, null) })
const { events } = createBus()
const result = await new UpdateItem(db, events).execute({
listId: 'list-1',
itemId: 'item-1',
userId: 'user-1',
notes: '',
})
expect(result.ok).toBe(true)
if (!result.ok) throw new Error('expected the use case to succeed')
const data = updates[0] as { data: { notes?: string | null } }
expect(data.data).toEqual({ notes: null })
expect(result.value.notes).toBeNull()
})
it('marks the item back to pending, clears boughtAt and publishes item.updated', async () => {
const { db, updates } = createDb({
membership: { role: 'collaborator' },

View File

@@ -20,10 +20,12 @@ type UpdateItemFields = {
completed?: boolean
/** New quantity (`null` or an empty string clears it); when omitted it stays untouched. */
quantity?: string | null
/** New notes (`null` or an empty string clears them); when omitted they stay untouched. */
notes?: string | null
}
/**
* Updates an item's bought state and/or its quantity. Only members can do it.
* Updates an item's bought state, its quantity and/or its notes. Only members can do it.
* `boughtAt` records the moment an item was marked as bought, for future
* statistics, and is cleared when the item goes back to pending.
*/
@@ -50,7 +52,12 @@ export class UpdateItem {
return Failure(problems.notFound('El artículo no existe en esta lista.'))
}
const data: { completed?: boolean; boughtAt?: Date | null; quantity?: string | null } = {}
const data: {
completed?: boolean
boughtAt?: Date | null
quantity?: string | null
notes?: string | null
} = {}
if (input.completed !== undefined) {
data.completed = input.completed
@@ -61,6 +68,10 @@ export class UpdateItem {
data.quantity = input.quantity?.trim() || null
}
if (input.notes !== undefined) {
data.notes = input.notes?.trim() || null
}
const updated = await this.db.listItem.update({
where: { id: item.id },
data,