feat(web): UI quick wins for items, navigation and settings
- Toggling an item only disables that row, so several items can be marked in a row; failures roll back just that item and show a toast, and the list refetches once the last toggle settles. - Sidebar, bottom nav and header share one set of sections (Inicio / Listas / Ajustes); the bottom nav drops the duplicated "Cuenta" link and the header drops the non-functional search button. - Settings: the name is saved through BetterAuth, the email is shown read-only, and the fake notification switches are replaced by a real theme picker. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,15 @@
|
||||
import type { ListItem } from '@superlist/shared'
|
||||
import { queryOptions, useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import {
|
||||
queryOptions,
|
||||
useMutation,
|
||||
useMutationState,
|
||||
useQuery,
|
||||
useQueryClient,
|
||||
} from '@tanstack/react-query'
|
||||
|
||||
import { apiFetch } from '@/lib/api-client'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
import { ApiError, apiFetch } from '@/lib/api-client'
|
||||
|
||||
export const itemsQueryOptions = (listId: string) =>
|
||||
queryOptions({
|
||||
@@ -69,12 +77,18 @@ export function useCreateItem(listId: string) {
|
||||
})
|
||||
}
|
||||
|
||||
type UpdateItemInput = { itemId: string; completed: boolean }
|
||||
|
||||
const updateItemMutationKey = (listId: string) => ['lists', listId, 'items', 'update']
|
||||
|
||||
export function useUpdateItem(listId: string) {
|
||||
const queryClient = useQueryClient()
|
||||
const queryKey = ['lists', listId, 'items']
|
||||
const { queryKey } = itemsQueryOptions(listId)
|
||||
const mutationKey = updateItemMutationKey(listId)
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (input: { itemId: string; completed: boolean }) =>
|
||||
mutationKey,
|
||||
mutationFn: (input: UpdateItemInput) =>
|
||||
apiFetch<ListItem>(`/lists/${listId}/items/${input.itemId}`, {
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify({ completed: input.completed }),
|
||||
@@ -83,9 +97,9 @@ export function useUpdateItem(listId: string) {
|
||||
// Cancel the outgoing refetch so it cannot overwrite our optimistic one.
|
||||
await queryClient.cancelQueries({ queryKey })
|
||||
|
||||
const previous = queryClient.getQueriesData<ListItem[]>({ queryKey })
|
||||
const previous = queryClient.getQueryData(queryKey)?.find((item) => item.id === itemId)
|
||||
|
||||
queryClient.setQueriesData<ListItem[]>({ queryKey }, (items) =>
|
||||
queryClient.setQueryData(queryKey, (items) =>
|
||||
items?.map((item) =>
|
||||
item.id === itemId
|
||||
? { ...item, completed, boughtAt: completed ? new Date().toISOString() : null }
|
||||
@@ -95,11 +109,35 @@ export function useUpdateItem(listId: string) {
|
||||
|
||||
return { previous }
|
||||
},
|
||||
onError: (_error, _variables, context) => {
|
||||
context?.previous.forEach(([key, items]) => {
|
||||
queryClient.setQueryData(key, items)
|
||||
})
|
||||
// Only this item is rolled back, so other toggles in flight keep their state.
|
||||
onError: (error, _variables, context) => {
|
||||
toast.error(
|
||||
error instanceof ApiError
|
||||
? (error.problem.detail ?? error.problem.title)
|
||||
: 'No pudimos guardar el cambio.',
|
||||
)
|
||||
|
||||
const previous = context?.previous
|
||||
if (!previous) return
|
||||
|
||||
queryClient.setQueryData(queryKey, (items) =>
|
||||
items?.map((item) => (item.id === previous.id ? previous : item)),
|
||||
)
|
||||
},
|
||||
// Several items can be toggled in a row: refetch once the last one settles, so an
|
||||
// early refetch cannot bring back the server state of a toggle still in flight.
|
||||
onSettled: () => {
|
||||
if (queryClient.isMutating({ mutationKey }) === 1) {
|
||||
return queryClient.invalidateQueries({ queryKey })
|
||||
}
|
||||
},
|
||||
onSettled: () => queryClient.invalidateQueries({ queryKey }),
|
||||
})
|
||||
}
|
||||
|
||||
/** Ids of the items of a list whose bought state is being saved right now. */
|
||||
export function usePendingItemUpdates(listId: string): string[] {
|
||||
return useMutationState({
|
||||
filters: { mutationKey: updateItemMutationKey(listId), status: 'pending' },
|
||||
select: (mutation) => (mutation.state.variables as UpdateItemInput).itemId,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user