diff --git a/apps/api/src/lib/auth.ts b/apps/api/src/lib/auth.ts index a3b0fdf..daaf3df 100644 --- a/apps/api/src/lib/auth.ts +++ b/apps/api/src/lib/auth.ts @@ -14,6 +14,15 @@ export const auth = betterAuth({ baseURL: process.env.BETTER_AUTH_URL, secret: process.env.BETTER_AUTH_SECRET, trustedOrigins, + session: { + // A signed copy of the session lives in a cookie for 5 minutes, so protected + // routes skip the session lookup in Postgres on every request. Trade-off: a + // session revoked elsewhere stays valid on other devices until the cache expires. + cookieCache: { + enabled: true, + maxAge: 5 * 60, + }, + }, emailAndPassword: { enabled: true, minPasswordLength: 8, diff --git a/apps/web/src/components/items/create-item-form.tsx b/apps/web/src/components/items/create-item-form.tsx index 4211756..6cd2fb2 100644 --- a/apps/web/src/components/items/create-item-form.tsx +++ b/apps/web/src/components/items/create-item-form.tsx @@ -7,6 +7,7 @@ import { Button } from '@/components/ui/button' import { Field, FieldError } from '@/components/ui/field' import { Input } from '@/components/ui/input' import { useCreateItem } from '@/hooks/use-items' +import { ApiError } from '@/lib/api-client' const createItemSchema = z.object({ name: z.string().trim().min(1, 'Escribe un artículo').max(120, 'Máximo 120 caracteres'), @@ -46,8 +47,21 @@ export function CreateItemForm({ listId, autoFocus = false }: CreateItemFormProp defaultValues: { name: '' }, }) + // The input clears at once (the item is added optimistically) so several items + // can be typed in a row; if the request fails the text comes back with the error. const onSubmit = form.handleSubmit((values) => { - createItem.mutate(values, { onSuccess: () => form.reset() }) + form.reset() + createItem.mutate(values, { + onError: (error) => { + form.setValue('name', values.name) + form.setError('name', { + message: + error instanceof ApiError + ? (error.problem.detail ?? error.problem.title) + : 'No pudimos añadir el artículo.', + }) + }, + }) }) return ( @@ -64,9 +78,9 @@ export function CreateItemForm({ listId, autoFocus = false }: CreateItemFormProp - ) diff --git a/apps/web/src/components/items/item-row.tsx b/apps/web/src/components/items/item-row.tsx index 4508e39..a62cef8 100644 --- a/apps/web/src/components/items/item-row.tsx +++ b/apps/web/src/components/items/item-row.tsx @@ -2,7 +2,7 @@ import type { ListItem } from '@superlist/shared' import { CheckIcon } from 'lucide-react' import { Card, CardContent } from '@/components/ui/card' -import type { useUpdateItem } from '@/hooks/use-items' +import { isOptimisticItem, type useUpdateItem } from '@/hooks/use-items' import { cn } from '@/lib/utils' type ItemRowProps = { @@ -19,7 +19,8 @@ export function ItemRow({ item, updateItem }: ItemRowProps) {