mirror of
https://github.com/vercel/commerce.git
synced 2025-03-31 01:05:53 +00:00
Fix bugs with optimistic.
This commit is contained in:
parent
0ebf071826
commit
cea56f608b
@ -47,7 +47,7 @@ export function EditItemQuantityButton({
|
|||||||
return (
|
return (
|
||||||
<form
|
<form
|
||||||
action={async () => {
|
action={async () => {
|
||||||
optimisticUpdate({ itemId: payload.lineId, newQuantity: payload.quantity });
|
optimisticUpdate({ itemId: payload.lineId, newQuantity: payload.quantity, type });
|
||||||
await actionWithVariant();
|
await actionWithVariant();
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
@ -24,6 +24,7 @@ type MerchandiseSearchParams = {
|
|||||||
type NewState = {
|
type NewState = {
|
||||||
itemId: string;
|
itemId: string;
|
||||||
newQuantity: number;
|
newQuantity: number;
|
||||||
|
type: 'plus' | 'minus';
|
||||||
};
|
};
|
||||||
|
|
||||||
function reducer(state: Cart | undefined, newState: NewState) {
|
function reducer(state: Cart | undefined, newState: NewState) {
|
||||||
@ -31,10 +32,17 @@ function reducer(state: Cart | undefined, newState: NewState) {
|
|||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
const updatedLines = state.lines.map((item: CartItem) => {
|
let updatedLines = state.lines
|
||||||
|
.map((item: CartItem) => {
|
||||||
if (item.id === newState.itemId) {
|
if (item.id === newState.itemId) {
|
||||||
|
if (newState.type === 'minus' && newState.newQuantity === 0) {
|
||||||
|
// Remove the item if quantity becomes 0
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
const singleItemAmount = Number(item.cost.totalAmount.amount) / item.quantity;
|
const singleItemAmount = Number(item.cost.totalAmount.amount) / item.quantity;
|
||||||
const newTotalAmount = Number(item.cost.totalAmount.amount) + singleItemAmount;
|
const newTotalAmount = singleItemAmount * newState.newQuantity;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...item,
|
...item,
|
||||||
quantity: newState.newQuantity,
|
quantity: newState.newQuantity,
|
||||||
@ -48,7 +56,8 @@ function reducer(state: Cart | undefined, newState: NewState) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
return item;
|
return item;
|
||||||
});
|
})
|
||||||
|
.filter(Boolean) as CartItem[];
|
||||||
|
|
||||||
const newTotalQuantity = updatedLines.reduce((sum, item) => sum + item.quantity, 0);
|
const newTotalQuantity = updatedLines.reduce((sum, item) => sum + item.quantity, 0);
|
||||||
const newTotalAmount = updatedLines.reduce(
|
const newTotalAmount = updatedLines.reduce(
|
||||||
@ -56,6 +65,22 @@ function reducer(state: Cart | undefined, newState: NewState) {
|
|||||||
0
|
0
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// If there are no items left, return an empty cart
|
||||||
|
if (updatedLines.length === 0) {
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
lines: [],
|
||||||
|
totalQuantity: 0,
|
||||||
|
cost: {
|
||||||
|
...state.cost,
|
||||||
|
totalAmount: {
|
||||||
|
...state.cost.totalAmount,
|
||||||
|
amount: '0'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
lines: updatedLines,
|
lines: updatedLines,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user