Skip to content

Commit

Permalink
feat: add error handling for cart
Browse files Browse the repository at this point in the history
  • Loading branch information
apledger committed Jan 16, 2025
1 parent a672f6e commit e27d762
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions apps/web/vibes/soul/sections/cart/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { startTransition, useActionState, useEffect, useOptimistic } from 'react
import { useFormStatus } from 'react-dom';

import { Button } from '@/vibes/soul/primitives/button';
import { toast } from '@/vibes/soul/primitives/toaster';
import { StickySidebarLayout } from '@/vibes/soul/sections/sticky-sidebar-layout';

import { cartLineItemActionFormDataSchema } from './schema';
Expand Down Expand Up @@ -79,6 +80,16 @@ export function CartClient<LineItem extends CartLineItem>({
lastResult: null,
});

const [form] = useForm({ lastResult: state.lastResult });

useEffect(() => {
if (form.errors) {
form.errors.forEach((error) => {
toast.error(error);
});
}
}, [form.errors]);

const [optimisticLineItems, setOptimisticLineItems] = useOptimistic<CartLineItem[], FormData>(
state.lineItems,
(prevState, formData) => {
Expand Down

0 comments on commit e27d762

Please sign in to comment.