-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
29,498 additions
and
7 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict'; | ||
|
||
import CartItemConstants from '../constants/CartItemConstants'; | ||
|
||
export default { | ||
add: (product, qty) => { | ||
let data = { | ||
'product': product, | ||
'cartQty': parseInt(qty, 10) | ||
}; | ||
return (dispatch) => { | ||
dispatch({ | ||
'type': CartItemConstants.ADD_CART_ITEM, | ||
'data': data | ||
}); | ||
}; | ||
}, | ||
clearCart: () => { | ||
return (dispatch) => { | ||
dispatch({ | ||
'type': CartItemConstants.CLEAR_CART, | ||
'data': true | ||
}); | ||
}; | ||
}, | ||
toggle: (showHide) => { | ||
return (dispatch) => { | ||
dispatch({ | ||
'type': CartItemConstants.TOGGLE, | ||
'data': showHide | ||
}); | ||
}; | ||
}, | ||
update: (product, newCartQty) => { | ||
let data = { | ||
'product': product, | ||
'cartQty': parseInt(newCartQty, 10) | ||
}; | ||
return (dispatch) => { | ||
dispatch({ | ||
'type': CartItemConstants.UPDATE_CART_ITEM, | ||
'data': data | ||
}); | ||
}; | ||
}, | ||
remove: (itemId) => { | ||
return (dispatch) => { | ||
dispatch({ | ||
'type': CartItemConstants.REMOVE_CART_ITEM, | ||
'id': itemId | ||
}); | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
import keyMirror from 'keymirror'; | ||
|
||
export default keyMirror({ | ||
ADD_CART_ITEM: null, | ||
CLEAR_CART: null, | ||
TOGGLE: null, | ||
UPDATE_CART_ITEM: null, | ||
REMOVE_CART_ITEM: null, | ||
UPDATE_CART_TOTAL: null | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
import CartActions from './actions/CartActions'; | ||
import {cartIsActive, cartItems, cartQtyPlaceholders} from './reducers/cartItems'; | ||
|
||
export { | ||
CartActions, | ||
cartIsActive, | ||
cartItems, | ||
cartQtyPlaceholders | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
'use strict'; | ||
|
||
import CartItemConstants from '../constants/CartItemConstants'; | ||
|
||
const updateSessionCart = (newCartItems) => { | ||
sessionStorage.setItem('cartItems', JSON.stringify(newCartItems)); | ||
}; | ||
|
||
const updateSessionPlaceholders = (placeholders) => { | ||
sessionStorage.setItem('cartQuantities', JSON.stringify(placeholders)); | ||
}; | ||
|
||
const cartIsActive = (state = 'hide', action) => { | ||
switch (action.type) { | ||
case CartItemConstants.TOGGLE: | ||
return action.data; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
const cartItems = (state = [], action) => { | ||
let cartItems, newItem, index; | ||
switch (action.type) { | ||
case CartItemConstants.ADD_CART_ITEM: | ||
cartItems = [...state]; | ||
newItem = action.data; | ||
index = state.findIndex((item) => item.product.id === action.data.product.id); | ||
if (index < 0) { | ||
cartItems.push(newItem); | ||
} else { | ||
cartItems[index].product = action.data.product; | ||
cartItems[index].cartQty += action.data.cartQty; | ||
} | ||
updateSessionCart(cartItems); | ||
return cartItems; | ||
case CartItemConstants.CLEAR_CART: | ||
return []; | ||
case CartItemConstants.UPDATE_CART_ITEM: | ||
cartItems = [...state]; | ||
newItem = action.data; | ||
index = cartItems.findIndex((item) => item.product.id === action.data.product.id); | ||
if (index < 0) { | ||
cartItems.push(newItem); | ||
} else { | ||
cartItems[index].product = action.data.product; | ||
cartItems[index].cartQty = action.data.cartQty; | ||
} | ||
if (cartItems[index].cartQty < 1) { | ||
cartItems.splice(index, 1); | ||
} | ||
updateSessionCart(cartItems); | ||
return cartItems; | ||
case CartItemConstants.REMOVE_CART_ITEM: | ||
cartItems = [...state]; | ||
index = state.findIndex((cartItem) => cartItem.product.id === action.id); | ||
if (index !== -1) { | ||
cartItems.splice(index, 1); | ||
} | ||
updateSessionCart(cartItems); | ||
return cartItems; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
const cartQtyPlaceholders = (state = {}, action) => { | ||
let placeholders, productId; | ||
switch (action.type) { | ||
case CartItemConstants.ADD_CART_ITEM: | ||
placeholders = Object.assign({}, state); | ||
productId = action.data.product.id; | ||
placeholders[productId] = placeholders[productId] ? placeholders[productId] + action.data.cartQty: action.data.cartQty; | ||
updateSessionPlaceholders(placeholders); | ||
return placeholders; | ||
case CartItemConstants.CLEAR_CART: | ||
return {}; | ||
case CartItemConstants.UPDATE_CART_ITEM: | ||
placeholders = Object.assign({}, state); | ||
productId = action.data.product.id; | ||
placeholders[productId] = action.data.cartQty; | ||
updateSessionPlaceholders(placeholders); | ||
return placeholders; | ||
case CartItemConstants.REMOVE_CART_ITEM: | ||
placeholders = Object.assign({}, state); | ||
productId = action.id; | ||
placeholders[productId] = 0; | ||
updateSessionPlaceholders(placeholders); | ||
return placeholders; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export { | ||
cartItems, | ||
cartQtyPlaceholders, | ||
cartIsActive | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters