feat(items): move an item to another list with a long swipe
A longer swipe left on an item (past the edit threshold) opens a dialog with the user's other lists; picking one moves the item there while the user stays on the current list. On desktop, right-click now opens a context menu with "Editar" and "Mover a otra lista…". The API gets POST /api/lists/:listId/items/:itemId/move, which requires membership in both lists and publishes item.removed on the source list and item.created on the target, so other members see the change live. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -162,3 +162,57 @@ export function usePendingItemUpdates(listId: string): string[] {
|
||||
select: (mutation) => (mutation.state.variables as UpdateItemInput).itemId,
|
||||
})
|
||||
}
|
||||
|
||||
type MoveItemInput = {
|
||||
itemId: string
|
||||
targetListId: string
|
||||
/** Name of the target list, only used for the confirmation toast. */
|
||||
targetListName: string
|
||||
}
|
||||
|
||||
export function useMoveItem(listId: string) {
|
||||
const queryClient = useQueryClient()
|
||||
const { queryKey } = itemsQueryOptions(listId)
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({ itemId, targetListId }: MoveItemInput) =>
|
||||
apiFetch<ListItem>(`/lists/${listId}/items/${itemId}/move`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ targetListId }),
|
||||
}),
|
||||
// The item leaves this list right away; the user stays on it.
|
||||
onMutate: async ({ itemId }) => {
|
||||
await queryClient.cancelQueries({ queryKey })
|
||||
|
||||
const items = queryClient.getQueryData(queryKey)
|
||||
const index = items?.findIndex((item) => item.id === itemId) ?? -1
|
||||
const previous = index >= 0 ? items?.[index] : undefined
|
||||
|
||||
queryClient.setQueryData(queryKey, (current) => current?.filter((item) => item.id !== itemId))
|
||||
|
||||
return { previous, index }
|
||||
},
|
||||
onSuccess: (_moved, { targetListId, targetListName }) => {
|
||||
toast.success(`Movido a «${targetListName}»`)
|
||||
return queryClient.invalidateQueries({ queryKey: itemsQueryOptions(targetListId).queryKey })
|
||||
},
|
||||
// The item goes back to where it was, so the list does not jump around.
|
||||
onError: (error, _variables, context) => {
|
||||
toast.error(
|
||||
error instanceof ApiError
|
||||
? (error.problem.detail ?? error.problem.title)
|
||||
: 'No pudimos mover el artículo.',
|
||||
)
|
||||
|
||||
const previous = context?.previous
|
||||
if (!previous) return
|
||||
|
||||
queryClient.setQueryData(queryKey, (items) => {
|
||||
if (!items || items.some((item) => item.id === previous.id)) return items
|
||||
const next = [...items]
|
||||
next.splice(Math.min(context.index, next.length), 0, previous)
|
||||
return next
|
||||
})
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user