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:
@@ -9,6 +9,7 @@ import { ItemRow } from './item-row'
|
|||||||
type BoughtItemListProps = {
|
type BoughtItemListProps = {
|
||||||
items: ListItem[]
|
items: ListItem[]
|
||||||
updateItem: ReturnType<typeof useUpdateItem>
|
updateItem: ReturnType<typeof useUpdateItem>
|
||||||
|
savingIds: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -16,7 +17,7 @@ type BoughtItemListProps = {
|
|||||||
* default so marking an item visibly moves it down; the user can fold it.
|
* default so marking an item visibly moves it down; the user can fold it.
|
||||||
* Tapping an item here un-buys it and it returns to the pending list.
|
* Tapping an item here un-buys it and it returns to the pending list.
|
||||||
*/
|
*/
|
||||||
export function BoughtItemList({ items, updateItem }: BoughtItemListProps) {
|
export function BoughtItemList({ items, updateItem, savingIds }: BoughtItemListProps) {
|
||||||
return (
|
return (
|
||||||
<Collapsible defaultOpen>
|
<Collapsible defaultOpen>
|
||||||
<CollapsibleTrigger className="group flex w-full items-center gap-1.5 py-1 text-left">
|
<CollapsibleTrigger className="group flex w-full items-center gap-1.5 py-1 text-left">
|
||||||
@@ -27,7 +28,12 @@ export function BoughtItemList({ items, updateItem }: BoughtItemListProps) {
|
|||||||
<CollapsibleContent>
|
<CollapsibleContent>
|
||||||
<ul className="flex flex-col gap-2 pt-2">
|
<ul className="flex flex-col gap-2 pt-2">
|
||||||
{items.map((item) => (
|
{items.map((item) => (
|
||||||
<ItemRow key={item.id} item={item} updateItem={updateItem} />
|
<ItemRow
|
||||||
|
key={item.id}
|
||||||
|
item={item}
|
||||||
|
updateItem={updateItem}
|
||||||
|
isSaving={savingIds.includes(item.id)}
|
||||||
|
/>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
</CollapsibleContent>
|
</CollapsibleContent>
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
|
|||||||
import { Card, CardContent } from '@/components/ui/card'
|
import { Card, CardContent } from '@/components/ui/card'
|
||||||
import { Separator } from '@/components/ui/separator'
|
import { Separator } from '@/components/ui/separator'
|
||||||
import { Skeleton } from '@/components/ui/skeleton'
|
import { Skeleton } from '@/components/ui/skeleton'
|
||||||
import { useItems, useUpdateItem } from '@/hooks/use-items'
|
import { useItems, usePendingItemUpdates, useUpdateItem } from '@/hooks/use-items'
|
||||||
import { useListEvents } from '@/hooks/use-list-events'
|
import { useListEvents } from '@/hooks/use-list-events'
|
||||||
import { useToday } from '@/hooks/use-today'
|
import { useToday } from '@/hooks/use-today'
|
||||||
import { ApiError } from '@/lib/api-client'
|
import { ApiError } from '@/lib/api-client'
|
||||||
@@ -25,6 +25,7 @@ type ItemListProps = {
|
|||||||
export function ItemList({ listId }: ItemListProps) {
|
export function ItemList({ listId }: ItemListProps) {
|
||||||
const { data: items, isPending, error } = useItems(listId)
|
const { data: items, isPending, error } = useItems(listId)
|
||||||
const updateItem = useUpdateItem(listId)
|
const updateItem = useUpdateItem(listId)
|
||||||
|
const savingIds = usePendingItemUpdates(listId)
|
||||||
const today = useToday()
|
const today = useToday()
|
||||||
|
|
||||||
// Live updates: another member adds or buys an item and this list refreshes.
|
// Live updates: another member adds or buys an item and this list refreshes.
|
||||||
@@ -78,7 +79,12 @@ export function ItemList({ listId }: ItemListProps) {
|
|||||||
{pending.length > 0 && (
|
{pending.length > 0 && (
|
||||||
<ul className="flex flex-col gap-2">
|
<ul className="flex flex-col gap-2">
|
||||||
{pending.map((item) => (
|
{pending.map((item) => (
|
||||||
<ItemRow key={item.id} item={item} updateItem={updateItem} />
|
<ItemRow
|
||||||
|
key={item.id}
|
||||||
|
item={item}
|
||||||
|
updateItem={updateItem}
|
||||||
|
isSaving={savingIds.includes(item.id)}
|
||||||
|
/>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
)}
|
)}
|
||||||
@@ -86,7 +92,7 @@ export function ItemList({ listId }: ItemListProps) {
|
|||||||
{boughtToday.length > 0 && (
|
{boughtToday.length > 0 && (
|
||||||
<>
|
<>
|
||||||
{pending.length > 0 && <Separator className="mt-2" />}
|
{pending.length > 0 && <Separator className="mt-2" />}
|
||||||
<BoughtItemList items={boughtToday} updateItem={updateItem} />
|
<BoughtItemList items={boughtToday} updateItem={updateItem} savingIds={savingIds} />
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -9,10 +9,12 @@ import { cn } from '@/lib/utils'
|
|||||||
type ItemRowProps = {
|
type ItemRowProps = {
|
||||||
item: ListItem
|
item: ListItem
|
||||||
updateItem: ReturnType<typeof useUpdateItem>
|
updateItem: ReturnType<typeof useUpdateItem>
|
||||||
|
/** This item's bought state is being saved; only this row waits for it. */
|
||||||
|
isSaving: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A single item row: tapping it toggles its bought state. */
|
/** A single item row: tapping it toggles its bought state. */
|
||||||
export function ItemRow({ item, updateItem }: ItemRowProps) {
|
export function ItemRow({ item, updateItem, isSaving }: ItemRowProps) {
|
||||||
return (
|
return (
|
||||||
<li>
|
<li>
|
||||||
<Card>
|
<Card>
|
||||||
@@ -21,7 +23,7 @@ export function ItemRow({ item, updateItem }: ItemRowProps) {
|
|||||||
type="button"
|
type="button"
|
||||||
aria-pressed={item.completed}
|
aria-pressed={item.completed}
|
||||||
// A placeholder has no server id yet, so it cannot be toggled.
|
// A placeholder has no server id yet, so it cannot be toggled.
|
||||||
disabled={updateItem.isPending || isOptimisticItem(item)}
|
disabled={isSaving || isOptimisticItem(item)}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
updateItem.mutate({ itemId: item.id, completed: !item.completed })
|
updateItem.mutate({ itemId: item.id, completed: !item.completed })
|
||||||
// Back to the input, so the next item can be typed right away.
|
// Back to the input, so the next item can be typed right away.
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import type { ShoppingListSummary } from '@superlist/shared'
|
import type { ShoppingListSummary } from '@superlist/shared'
|
||||||
import { useQuery } from '@tanstack/react-query'
|
import { useQuery } from '@tanstack/react-query'
|
||||||
import { Link, useMatch, useRouterState } from '@tanstack/react-router'
|
import { Link, useMatch, useRouterState } from '@tanstack/react-router'
|
||||||
import { ArrowLeftIcon, SearchIcon, SettingsIcon } from 'lucide-react'
|
import { ArrowLeftIcon, SettingsIcon } from 'lucide-react'
|
||||||
|
|
||||||
|
import { sectionTitle } from '@/components/layout/nav-items'
|
||||||
import { ThemeToggle } from '@/components/theme-toggle'
|
import { ThemeToggle } from '@/components/theme-toggle'
|
||||||
import { Badge } from '@/components/ui/badge'
|
import { Badge } from '@/components/ui/badge'
|
||||||
import { Button } from '@/components/ui/button'
|
import { Button } from '@/components/ui/button'
|
||||||
@@ -13,13 +14,6 @@ import { listsQueryOptions, pickPrimaryList } from '@/hooks/use-lists'
|
|||||||
|
|
||||||
type HeaderList = Pick<ShoppingListSummary, 'id' | 'name' | 'role' | 'isPrimary'>
|
type HeaderList = Pick<ShoppingListSummary, 'id' | 'name' | 'role' | 'isPrimary'>
|
||||||
|
|
||||||
function titleFor(pathname: string) {
|
|
||||||
if (pathname === '/') return 'Panel'
|
|
||||||
if (pathname.startsWith('/lists')) return 'Listas'
|
|
||||||
if (pathname.startsWith('/settings')) return 'Ajustes'
|
|
||||||
return 'Superlist'
|
|
||||||
}
|
|
||||||
|
|
||||||
export function AppHeader() {
|
export function AppHeader() {
|
||||||
const pathname = useRouterState({ select: (state) => state.location.pathname })
|
const pathname = useRouterState({ select: (state) => state.location.pathname })
|
||||||
const listMatch = useMatch({ from: '/_authenticated/lists/$listId/', shouldThrow: false })
|
const listMatch = useMatch({ from: '/_authenticated/lists/$listId/', shouldThrow: false })
|
||||||
@@ -47,19 +41,9 @@ export function AppHeader() {
|
|||||||
{list ? (
|
{list ? (
|
||||||
<ListHeading list={list} showBack={Boolean(listId)} />
|
<ListHeading list={list} showBack={Boolean(listId)} />
|
||||||
) : (
|
) : (
|
||||||
<h1 className="truncate text-sm font-medium">{titleFor(pathname)}</h1>
|
<h1 className="truncate text-sm font-medium">{sectionTitle(pathname)}</h1>
|
||||||
)}
|
)}
|
||||||
<div className="ml-auto flex shrink-0 items-center gap-1">
|
<div className="ml-auto flex shrink-0 items-center gap-1">
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
className="text-muted-foreground hover:bg-accent hover:text-accent-foreground mr-1 hidden items-center gap-2 rounded-md border px-3 py-1.5 text-xs md:flex"
|
|
||||||
>
|
|
||||||
<SearchIcon className="size-3.5" />
|
|
||||||
Buscar
|
|
||||||
<kbd className="bg-muted text-muted-foreground rounded px-1.5 py-0.5 font-mono text-[10px]">
|
|
||||||
⌘K
|
|
||||||
</kbd>
|
|
||||||
</button>
|
|
||||||
{list && (
|
{list && (
|
||||||
<Button variant="outline" size="sm" asChild>
|
<Button variant="outline" size="sm" asChild>
|
||||||
<Link to="/lists/$listId/settings" params={{ listId: list.id }}>
|
<Link to="/lists/$listId/settings" params={{ listId: list.id }}>
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
import { Link, useRouterState } from '@tanstack/react-router'
|
import { Link, useRouterState } from '@tanstack/react-router'
|
||||||
import type { LucideIcon } from 'lucide-react'
|
|
||||||
import { HomeIcon, SettingsIcon, ShoppingCartIcon } from 'lucide-react'
|
|
||||||
|
|
||||||
import { focusNewItemInput } from '@/components/items/create-item-form'
|
import { focusNewItemInput } from '@/components/items/create-item-form'
|
||||||
|
import { isNavItemActive, NAV_ITEMS } from '@/components/layout/nav-items'
|
||||||
import { NavUser } from '@/components/layout/nav-user'
|
import { NavUser } from '@/components/layout/nav-user'
|
||||||
import {
|
import {
|
||||||
Sidebar,
|
Sidebar,
|
||||||
@@ -17,23 +15,6 @@ import {
|
|||||||
SidebarRail,
|
SidebarRail,
|
||||||
} from '@/components/ui/sidebar'
|
} from '@/components/ui/sidebar'
|
||||||
|
|
||||||
type NavItem = {
|
|
||||||
title: string
|
|
||||||
to: string
|
|
||||||
icon: LucideIcon
|
|
||||||
}
|
|
||||||
|
|
||||||
const NAV_ITEMS: NavItem[] = [
|
|
||||||
{ title: 'Principal', to: '/', icon: HomeIcon },
|
|
||||||
{ title: 'Listas', to: '/lists', icon: ShoppingCartIcon },
|
|
||||||
{ title: 'Ajustes', to: '/settings', icon: SettingsIcon },
|
|
||||||
]
|
|
||||||
|
|
||||||
function isActive(pathname: string, to: string) {
|
|
||||||
if (to === '/') return pathname === '/'
|
|
||||||
return pathname === to || pathname.startsWith(`${to}/`)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function AppSidebar() {
|
export function AppSidebar() {
|
||||||
const pathname = useRouterState({ select: (state) => state.location.pathname })
|
const pathname = useRouterState({ select: (state) => state.location.pathname })
|
||||||
|
|
||||||
@@ -59,13 +40,13 @@ export function AppSidebar() {
|
|||||||
|
|
||||||
<SidebarContent>
|
<SidebarContent>
|
||||||
<SidebarGroup>
|
<SidebarGroup>
|
||||||
<SidebarGroupLabel>Plataforma</SidebarGroupLabel>
|
<SidebarGroupLabel>Navegación</SidebarGroupLabel>
|
||||||
<SidebarMenu>
|
<SidebarMenu>
|
||||||
{NAV_ITEMS.map((item) => (
|
{NAV_ITEMS.map((item) => (
|
||||||
<SidebarMenuItem key={item.to}>
|
<SidebarMenuItem key={item.to}>
|
||||||
<SidebarMenuButton
|
<SidebarMenuButton
|
||||||
asChild
|
asChild
|
||||||
isActive={isActive(pathname, item.to)}
|
isActive={isNavItemActive(pathname, item.to)}
|
||||||
tooltip={item.title}
|
tooltip={item.title}
|
||||||
>
|
>
|
||||||
<Link
|
<Link
|
||||||
|
|||||||
@@ -1,73 +1,34 @@
|
|||||||
import { Link, useRouterState } from '@tanstack/react-router'
|
import { Link, useRouterState } from '@tanstack/react-router'
|
||||||
import { HomeIcon, SettingsIcon, ShoppingCartIcon, UserIcon } from 'lucide-react'
|
|
||||||
|
|
||||||
import { focusNewItemInput } from '@/components/items/create-item-form'
|
import { focusNewItemInput } from '@/components/items/create-item-form'
|
||||||
|
import { isNavItemActive, NAV_ITEMS } from '@/components/layout/nav-items'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
|
|
||||||
function isActive(pathname: string, to: string) {
|
|
||||||
if (to === '/') return pathname === '/'
|
|
||||||
return pathname === to || pathname.startsWith(`${to}/`)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function BottomNav() {
|
export function BottomNav() {
|
||||||
const pathname = useRouterState({ select: (state) => state.location.pathname })
|
const pathname = useRouterState({ select: (state) => state.location.pathname })
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<footer className="fixed inset-x-0 bottom-0 z-30 border-t bg-background/95 supports-[backdrop-filter]:bg-background/60 backdrop-blur md:hidden pb-[env(safe-area-inset-bottom)]">
|
<footer className="fixed inset-x-0 bottom-0 z-30 border-t bg-background/95 supports-[backdrop-filter]:bg-background/60 backdrop-blur md:hidden pb-[env(safe-area-inset-bottom)]">
|
||||||
<nav className="mx-auto flex h-16 max-w-lg items-center justify-around px-2">
|
<nav className="mx-auto flex h-16 max-w-lg items-center justify-around px-2">
|
||||||
<Link
|
{NAV_ITEMS.map((item) => {
|
||||||
to="/"
|
const active = isNavItemActive(pathname, item.to)
|
||||||
onClick={() => focusNewItemInput(pathname)}
|
|
||||||
className={cn(
|
|
||||||
'flex flex-col items-center gap-1 rounded-md px-2 py-1.5 text-xs transition-colors',
|
|
||||||
isActive(pathname, '/')
|
|
||||||
? 'text-foreground'
|
|
||||||
: 'text-muted-foreground hover:text-foreground',
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
<HomeIcon className="size-5" />
|
|
||||||
<span>Inicio</span>
|
|
||||||
</Link>
|
|
||||||
|
|
||||||
<Link
|
return (
|
||||||
to="/lists"
|
<Link
|
||||||
className={cn(
|
key={item.to}
|
||||||
'flex flex-col items-center gap-1 rounded-md px-2 py-1.5 text-xs transition-colors',
|
to={item.to}
|
||||||
isActive(pathname, '/lists')
|
onClick={item.to === '/' ? () => focusNewItemInput(pathname) : undefined}
|
||||||
? 'text-foreground'
|
aria-current={active ? 'page' : undefined}
|
||||||
: 'text-muted-foreground hover:text-foreground',
|
className={cn(
|
||||||
)}
|
'flex flex-col items-center gap-1 rounded-md px-3 py-1.5 text-xs transition-colors',
|
||||||
>
|
active ? 'text-foreground' : 'text-muted-foreground hover:text-foreground',
|
||||||
<ShoppingCartIcon className="size-5" />
|
)}
|
||||||
<span>Listas</span>
|
>
|
||||||
</Link>
|
<item.icon className="size-5" />
|
||||||
|
<span>{item.title}</span>
|
||||||
<Link
|
</Link>
|
||||||
to="/settings"
|
)
|
||||||
className={cn(
|
})}
|
||||||
'flex flex-col items-center gap-1 rounded-md px-2 py-1.5 text-xs transition-colors',
|
|
||||||
isActive(pathname, '/settings')
|
|
||||||
? 'text-foreground'
|
|
||||||
: 'text-muted-foreground hover:text-foreground',
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
<SettingsIcon className="size-5" />
|
|
||||||
<span>Ajustes</span>
|
|
||||||
</Link>
|
|
||||||
|
|
||||||
<Link
|
|
||||||
to="/settings"
|
|
||||||
className={cn(
|
|
||||||
'flex flex-col items-center gap-1 rounded-md px-2 py-1.5 text-xs transition-colors',
|
|
||||||
isActive(pathname, '/settings')
|
|
||||||
? 'text-foreground'
|
|
||||||
: 'text-muted-foreground hover:text-foreground',
|
|
||||||
)}
|
|
||||||
aria-label="Cuenta"
|
|
||||||
>
|
|
||||||
<UserIcon className="size-5" />
|
|
||||||
<span>Cuenta</span>
|
|
||||||
</Link>
|
|
||||||
</nav>
|
</nav>
|
||||||
</footer>
|
</footer>
|
||||||
)
|
)
|
||||||
|
|||||||
25
apps/web/src/components/layout/nav-items.ts
Normal file
25
apps/web/src/components/layout/nav-items.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import type { LucideIcon } from 'lucide-react'
|
||||||
|
import { HomeIcon, SettingsIcon, ShoppingCartIcon } from 'lucide-react'
|
||||||
|
|
||||||
|
type NavItem = {
|
||||||
|
title: string
|
||||||
|
to: '/' | '/lists' | '/settings'
|
||||||
|
icon: LucideIcon
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The app's sections, shared by the sidebar (desktop), the bottom nav (mobile) and the header. */
|
||||||
|
export const NAV_ITEMS: NavItem[] = [
|
||||||
|
{ title: 'Inicio', to: '/', icon: HomeIcon },
|
||||||
|
{ title: 'Listas', to: '/lists', icon: ShoppingCartIcon },
|
||||||
|
{ title: 'Ajustes', to: '/settings', icon: SettingsIcon },
|
||||||
|
]
|
||||||
|
|
||||||
|
export function isNavItemActive(pathname: string, to: NavItem['to']) {
|
||||||
|
if (to === '/') return pathname === '/'
|
||||||
|
return pathname === to || pathname.startsWith(`${to}/`)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Title of the section the pathname belongs to, for the header. */
|
||||||
|
export function sectionTitle(pathname: string) {
|
||||||
|
return NAV_ITEMS.find((item) => isNavItemActive(pathname, item.to))?.title ?? 'Superlist'
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { useNavigate } from '@tanstack/react-router'
|
import { useNavigate } from '@tanstack/react-router'
|
||||||
import { ChevronsUpDownIcon, LogOutIcon, UserIcon } from 'lucide-react'
|
import { ChevronsUpDownIcon, LogOutIcon, SettingsIcon } from 'lucide-react'
|
||||||
|
|
||||||
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'
|
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'
|
||||||
import {
|
import {
|
||||||
@@ -78,8 +78,8 @@ export function NavUser() {
|
|||||||
<DropdownMenuSeparator />
|
<DropdownMenuSeparator />
|
||||||
<DropdownMenuGroup>
|
<DropdownMenuGroup>
|
||||||
<DropdownMenuItem onClick={() => void navigate({ to: '/settings' })}>
|
<DropdownMenuItem onClick={() => void navigate({ to: '/settings' })}>
|
||||||
<UserIcon />
|
<SettingsIcon />
|
||||||
Cuenta
|
Ajustes
|
||||||
</DropdownMenuItem>
|
</DropdownMenuItem>
|
||||||
</DropdownMenuGroup>
|
</DropdownMenuGroup>
|
||||||
<DropdownMenuSeparator />
|
<DropdownMenuSeparator />
|
||||||
|
|||||||
@@ -1,7 +1,15 @@
|
|||||||
import type { ListItem } from '@superlist/shared'
|
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) =>
|
export const itemsQueryOptions = (listId: string) =>
|
||||||
queryOptions({
|
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) {
|
export function useUpdateItem(listId: string) {
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
const queryKey = ['lists', listId, 'items']
|
const { queryKey } = itemsQueryOptions(listId)
|
||||||
|
const mutationKey = updateItemMutationKey(listId)
|
||||||
|
|
||||||
return useMutation({
|
return useMutation({
|
||||||
mutationFn: (input: { itemId: string; completed: boolean }) =>
|
mutationKey,
|
||||||
|
mutationFn: (input: UpdateItemInput) =>
|
||||||
apiFetch<ListItem>(`/lists/${listId}/items/${input.itemId}`, {
|
apiFetch<ListItem>(`/lists/${listId}/items/${input.itemId}`, {
|
||||||
method: 'PATCH',
|
method: 'PATCH',
|
||||||
body: JSON.stringify({ completed: input.completed }),
|
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.
|
// Cancel the outgoing refetch so it cannot overwrite our optimistic one.
|
||||||
await queryClient.cancelQueries({ queryKey })
|
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) =>
|
items?.map((item) =>
|
||||||
item.id === itemId
|
item.id === itemId
|
||||||
? { ...item, completed, boughtAt: completed ? new Date().toISOString() : null }
|
? { ...item, completed, boughtAt: completed ? new Date().toISOString() : null }
|
||||||
@@ -95,11 +109,35 @@ export function useUpdateItem(listId: string) {
|
|||||||
|
|
||||||
return { previous }
|
return { previous }
|
||||||
},
|
},
|
||||||
onError: (_error, _variables, context) => {
|
// Only this item is rolled back, so other toggles in flight keep their state.
|
||||||
context?.previous.forEach(([key, items]) => {
|
onError: (error, _variables, context) => {
|
||||||
queryClient.setQueryData(key, items)
|
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,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,113 +1,27 @@
|
|||||||
import { zodResolver } from '@hookform/resolvers/zod'
|
import { zodResolver } from '@hookform/resolvers/zod'
|
||||||
import { createFileRoute, useNavigate } from '@tanstack/react-router'
|
import { createFileRoute, useNavigate } from '@tanstack/react-router'
|
||||||
import { useState } from 'react'
|
import { MonitorIcon, MoonIcon, SunIcon } from 'lucide-react'
|
||||||
import { useForm } from 'react-hook-form'
|
import { useForm } from 'react-hook-form'
|
||||||
|
import { toast } from 'sonner'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
|
import { type Theme, useTheme } from '@/components/theme-provider'
|
||||||
import { Button } from '@/components/ui/button'
|
import { Button } from '@/components/ui/button'
|
||||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
|
||||||
import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel } from '@/components/ui/field'
|
import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel } from '@/components/ui/field'
|
||||||
import { Input } from '@/components/ui/input'
|
import { Input } from '@/components/ui/input'
|
||||||
import { Switch } from '@/components/ui/switch'
|
import { authClient, signOut, useSession } from '@/lib/auth-client'
|
||||||
import { signOut, useSession } from '@/lib/auth-client'
|
|
||||||
|
|
||||||
export const Route = createFileRoute('/_authenticated/settings')({
|
export const Route = createFileRoute('/_authenticated/settings')({
|
||||||
component: SettingsPage,
|
component: SettingsPage,
|
||||||
})
|
})
|
||||||
|
|
||||||
const profileSchema = z.object({
|
|
||||||
name: z.string().min(1, 'El nombre es obligatorio').max(80),
|
|
||||||
email: z.email('Introduce un correo electrónico válido'),
|
|
||||||
})
|
|
||||||
|
|
||||||
type ProfileValues = z.infer<typeof profileSchema>
|
|
||||||
|
|
||||||
function SettingsPage() {
|
function SettingsPage() {
|
||||||
const { data: session } = useSession()
|
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
const [saved, setSaved] = useState(false)
|
|
||||||
|
|
||||||
const form = useForm<ProfileValues>({
|
|
||||||
resolver: zodResolver(profileSchema),
|
|
||||||
defaultValues: {
|
|
||||||
name: session?.user.name ?? '',
|
|
||||||
email: session?.user.email ?? '',
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="grid max-w-3xl gap-4 md:gap-6">
|
<div className="mx-auto grid w-full max-w-2xl gap-4 md:gap-6">
|
||||||
<Card>
|
<ProfileCard />
|
||||||
<CardHeader>
|
<AppearanceCard />
|
||||||
<CardTitle>Perfil</CardTitle>
|
|
||||||
<CardDescription>Así es como te verán las demás personas.</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent>
|
|
||||||
<form
|
|
||||||
onSubmit={form.handleSubmit(() => {
|
|
||||||
// Wire this to a `PATCH /users/me` feature on the API.
|
|
||||||
setSaved(true)
|
|
||||||
})}
|
|
||||||
>
|
|
||||||
<FieldGroup>
|
|
||||||
<Field data-invalid={!!form.formState.errors.name}>
|
|
||||||
<FieldLabel htmlFor="name">Nombre</FieldLabel>
|
|
||||||
<Input id="name" placeholder="Ada Lovelace" {...form.register('name')} />
|
|
||||||
<FieldError errors={[form.formState.errors.name]} />
|
|
||||||
</Field>
|
|
||||||
|
|
||||||
<Field data-invalid={!!form.formState.errors.email}>
|
|
||||||
<FieldLabel htmlFor="email">Correo electrónico</FieldLabel>
|
|
||||||
<Input
|
|
||||||
id="email"
|
|
||||||
type="email"
|
|
||||||
placeholder="ada@ejemplo.com"
|
|
||||||
{...form.register('email')}
|
|
||||||
/>
|
|
||||||
<FieldDescription>Solo lo usamos para iniciar tu sesión.</FieldDescription>
|
|
||||||
<FieldError errors={[form.formState.errors.email]} />
|
|
||||||
</Field>
|
|
||||||
|
|
||||||
<div className="flex items-center gap-4">
|
|
||||||
<Button type="submit" disabled={form.formState.isSubmitting}>
|
|
||||||
Guardar cambios
|
|
||||||
</Button>
|
|
||||||
{saved && (
|
|
||||||
<span className="text-muted-foreground text-sm">Guardado localmente.</span>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</FieldGroup>
|
|
||||||
</form>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
|
|
||||||
<Card>
|
|
||||||
<CardHeader>
|
|
||||||
<CardTitle>Apariencia</CardTitle>
|
|
||||||
<CardDescription>Notificaciones y preferencias de visualización.</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent className="flex flex-col gap-4">
|
|
||||||
{[
|
|
||||||
{
|
|
||||||
id: 'digest',
|
|
||||||
label: 'Resumen semanal',
|
|
||||||
hint: 'Un resumen de tus listas cada lunes.',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'mentions',
|
|
||||||
label: 'Menciones',
|
|
||||||
hint: 'Avísame cuando alguien mencione mi nombre.',
|
|
||||||
},
|
|
||||||
].map((option) => (
|
|
||||||
<div key={option.id} className="flex items-center justify-between gap-4">
|
|
||||||
<div>
|
|
||||||
<p className="text-sm font-medium">{option.label}</p>
|
|
||||||
<p className="text-muted-foreground text-sm">{option.hint}</p>
|
|
||||||
</div>
|
|
||||||
<Switch defaultChecked aria-label={option.label} />
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
|
|
||||||
<Card>
|
<Card>
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
@@ -129,3 +43,103 @@ function SettingsPage() {
|
|||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const profileSchema = z.object({
|
||||||
|
name: z.string().trim().min(1, 'El nombre es obligatorio').max(80, 'Máximo 80 caracteres'),
|
||||||
|
})
|
||||||
|
|
||||||
|
type ProfileValues = z.infer<typeof profileSchema>
|
||||||
|
|
||||||
|
function ProfileCard() {
|
||||||
|
const { data: session } = useSession()
|
||||||
|
|
||||||
|
const form = useForm<ProfileValues>({
|
||||||
|
resolver: zodResolver(profileSchema),
|
||||||
|
defaultValues: { name: session?.user.name ?? '' },
|
||||||
|
})
|
||||||
|
|
||||||
|
const onSubmit = form.handleSubmit(async (values) => {
|
||||||
|
const { error } = await authClient.updateUser({ name: values.name })
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
toast.error(error.message ?? 'No pudimos guardar tu nombre.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
form.reset(values)
|
||||||
|
toast.success('Nombre actualizado.')
|
||||||
|
})
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>Perfil</CardTitle>
|
||||||
|
<CardDescription>Así te ven las personas con las que compartes listas.</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<form onSubmit={onSubmit}>
|
||||||
|
<FieldGroup>
|
||||||
|
<Field data-invalid={!!form.formState.errors.name}>
|
||||||
|
<FieldLabel htmlFor="name">Nombre</FieldLabel>
|
||||||
|
<Input id="name" autoComplete="name" {...form.register('name')} />
|
||||||
|
<FieldError errors={[form.formState.errors.name]} />
|
||||||
|
</Field>
|
||||||
|
|
||||||
|
<Field>
|
||||||
|
<FieldLabel htmlFor="email">Correo electrónico</FieldLabel>
|
||||||
|
<Input id="email" type="email" value={session?.user.email ?? ''} readOnly disabled />
|
||||||
|
<FieldDescription>
|
||||||
|
Lo usas para iniciar sesión y recibir invitaciones.
|
||||||
|
</FieldDescription>
|
||||||
|
</Field>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
type="submit"
|
||||||
|
className="w-full sm:w-auto sm:self-start"
|
||||||
|
disabled={!form.formState.isDirty || form.formState.isSubmitting}
|
||||||
|
>
|
||||||
|
{form.formState.isSubmitting ? 'Guardando…' : 'Guardar cambios'}
|
||||||
|
</Button>
|
||||||
|
</FieldGroup>
|
||||||
|
</form>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const THEME_OPTIONS: { value: Theme; label: string; icon: typeof SunIcon }[] = [
|
||||||
|
{ value: 'light', label: 'Claro', icon: SunIcon },
|
||||||
|
{ value: 'dark', label: 'Oscuro', icon: MoonIcon },
|
||||||
|
{ value: 'system', label: 'Sistema', icon: MonitorIcon },
|
||||||
|
]
|
||||||
|
|
||||||
|
function AppearanceCard() {
|
||||||
|
const { theme, setTheme } = useTheme()
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>Apariencia</CardTitle>
|
||||||
|
<CardDescription>Elige el tema de la app en este dispositivo.</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<fieldset className="grid grid-cols-3 gap-2">
|
||||||
|
<legend className="sr-only">Tema</legend>
|
||||||
|
{THEME_OPTIONS.map((option) => (
|
||||||
|
<Button
|
||||||
|
key={option.value}
|
||||||
|
type="button"
|
||||||
|
variant={theme === option.value ? 'default' : 'outline'}
|
||||||
|
aria-pressed={theme === option.value}
|
||||||
|
onClick={() => setTheme(option.value)}
|
||||||
|
className="h-auto flex-col gap-1.5 py-3"
|
||||||
|
>
|
||||||
|
<option.icon className="size-5" />
|
||||||
|
{option.label}
|
||||||
|
</Button>
|
||||||
|
))}
|
||||||
|
</fieldset>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user