Skip to content

Commit

Permalink
Add noCartId retry logic back to addItem
Browse files Browse the repository at this point in the history
  • Loading branch information
sirugh committed Dec 9, 2019
1 parent a4402a4 commit 2b3ab66
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/peregrine/lib/store/actions/cart/asyncActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ export const addItemToCart = (payload = {}) => {
const { cart } = getState();
const { cartId } = cart;

// After checkout cartId is null, so we need to throw to trigger
// the retry logic.
if (!cartId) {
const missingCartIdError = new Error(
'Missing required information: cartId'
);
missingCartIdError.noCartId = true;
throw missingCartIdError;
}

const variables = {
cartId,
parentSku,
Expand Down Expand Up @@ -93,8 +103,8 @@ export const addItemToCart = (payload = {}) => {

const shouldRetry = !error.networkError && isInvalidCart(error);

// Only retry if the cart is invalid
if (shouldRetry) {
// Only retry if the cart is invalid or the cartId is missing.
if (shouldRetry || error.noCartId) {
// Delete the cached ID from local storage and Redux.
// In contrast to the save, make sure storage deletion is
// complete before dispatching the error--you don't want an
Expand Down
2 changes: 2 additions & 0 deletions packages/peregrine/lib/store/actions/checkout/asyncActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const cancelCheckout = () =>
export const resetCheckout = () =>
async function thunk(dispatch) {
await dispatch(closeDrawer());
// TODO: create cart after removing the old one
await dispatch(removeCart());
dispatch(actions.reset());
};
Expand Down Expand Up @@ -284,6 +285,7 @@ export const submitOrder = () =>
);

// Clear out everything we've saved about this cart from local storage.
// TODO: create cart after removing the old one
await dispatch(removeCart());
await clearCheckoutDataFromStorage();

Expand Down
2 changes: 2 additions & 0 deletions packages/peregrine/lib/store/actions/user/asyncActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export const signOut = ({ revokeToken }) => async dispatch => {
await clearCheckoutDataFromStorage();

// Now that we're signed out, forget the old (customer) cart.
// We don't need to create a new cart here because we're going to refresh
// the page immediately after.
await dispatch(removeCart());
};

Expand Down

0 comments on commit 2b3ab66

Please # to comment.