@@ -67,6 +67,12 @@ export function useCart() {
6767export function useAddToCart ( ) {
6868 const qc = useQueryClient ( ) ;
6969 return useMutation ( {
70+ // Serialize all cart mutations (same scope id) so rapid actions run in
71+ // order — the server sets absolute quantities, so out-of-order responses
72+ // would otherwise clobber the cache. mutationKey lets useMutationState
73+ // surface a global "cart busy" indicator.
74+ scope : { id : "cart" } ,
75+ mutationKey : [ "cart" , "add" ] ,
7076 mutationFn : ( input : { merchandiseId : string ; quantity ?: number } ) =>
7177 addItemServerFn ( { data : input } ) ,
7278 // NOTE(DECO-5278): optimistic add is deferred — building an optimistic
@@ -82,6 +88,8 @@ export function useAddToCart() {
8288export function useUpdateCartItem ( ) {
8389 const qc = useQueryClient ( ) ;
8490 return useMutation ( {
91+ scope : { id : "cart" } ,
92+ mutationKey : [ "cart" , "update" ] ,
8593 mutationFn : ( input : { lineId : string ; quantity : number } ) =>
8694 updateItemQuantityServerFn ( { data : input } ) ,
8795 onMutate : ( { lineId, quantity } ) =>
@@ -98,6 +106,8 @@ export function useUpdateCartItem() {
98106export function useRemoveCartItem ( ) {
99107 const qc = useQueryClient ( ) ;
100108 return useMutation ( {
109+ scope : { id : "cart" } ,
110+ mutationKey : [ "cart" , "remove" ] ,
101111 mutationFn : ( input : { lineId : string } ) => removeItemServerFn ( { data : input } ) ,
102112 onMutate : ( { lineId } ) =>
103113 optimisticCartUpdate ( qc , ( items ) => items . filter ( ( i ) => i . lineId !== lineId ) ) ,
0 commit comments