Commit 72c2834d authored by Shaphen Pangburn's avatar Shaphen Pangburn

[AFP-48 Shaphen Pangburn]: Add redux cycle to persist user cart to global state

parent 7ebecb53
import * as ApiUtil from '../util/cart_api_util';
export const RECEIVE_USER_CART = "RECEIVE_USER_CART";
const receiveUserCart = cart => ({
type: RECEIVE_USER_CART,
cart
})
export const fetchUserCart = userId => dispatch => ApiUtil.fetchUserCart(userId)
.then(cart => dispatch(receiveUserCart(cart)));
\ No newline at end of file
...@@ -7,6 +7,7 @@ export default class ProductItem extends Component { ...@@ -7,6 +7,7 @@ export default class ProductItem extends Component {
constructor(props) { constructor(props) {
super(props) super(props)
this.state = { this.state = {
orderItemQuantity: 0,
showDetailsModal: false showDetailsModal: false
} }
this.handleSubmit = this.handleSubmit.bind(this); this.handleSubmit = this.handleSubmit.bind(this);
...@@ -18,12 +19,29 @@ export default class ProductItem extends Component { ...@@ -18,12 +19,29 @@ export default class ProductItem extends Component {
this.setState({ showDetailsModal: !prevState }); this.setState({ showDetailsModal: !prevState });
} }
handleChange(type) {
return e => {
this.setState({ [type]: e.target.value })
}
}
calculateDiscount(price, discount) { calculateDiscount(price, discount) {
return (price * ((100 - discount) / 100)).toFixed(2); return (price * ((100 - discount) / 100)).toFixed(2);
} }
handleSubmit(e, product) { handleSubmit(e, product) {
e.preventDefault(); e.preventDefault();
// const newCart = Object.assign({}, this.props.cart);
// const newCartItem = {
// productRef: {
// id: product.sku,
// sku: product.sku,
// upc: product.upc,
// },
// quantity: this.state.orderItemQuantity;
// }
// newCart.cartItems.push(newCartItem);
// this.ptops.addCartItem(newCart);
} }
render() { render() {
...@@ -56,7 +74,12 @@ export default class ProductItem extends Component { ...@@ -56,7 +74,12 @@ export default class ProductItem extends Component {
{ "Save " + this.props.item.promo + "% on this item!"} { "Save " + this.props.item.promo + "% on this item!"}
</p> : ""} </p> : ""}
<form onSubmit={ e => this.handleSubmit(e, this.props.item) } className="add-cart-and-quantity"> <form onSubmit={ e => this.handleSubmit(e, this.props.item) } className="add-cart-and-quantity">
<input className="order-quantity" type="number" placeholder="Qty" /> <input
className="order-quantity"
type="number"
placeholder="Qty"
onChange={ this.handleChange("orderItemQuantity") }
/>
<button className="add-to-cart-button">Add to Cart</button> <button className="add-to-cart-button">Add to Cart</button>
</form> </form>
......
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import ProductMarket from './product-market'; import ProductMarket from './product-market';
import { fetchProducts, fetchPromotions } from '../../actions/product_actions'; import { fetchProducts, fetchPromotions } from '../../actions/product_actions';
import { fetchUserCart } from '../../actions/cart_actions';
const mSTP = state => ({ const mSTP = state => ({
products: state.market.products, products: state.market.products,
promotions: state.market.promotions, promotions: state.market.promotions,
user: state.user
}); });
const mDTP = dispatch => ({ const mDTP = dispatch => ({
getProducts: () => dispatch(fetchProducts()), getProducts: () => dispatch(fetchProducts()),
getPromotions: () => dispatch(fetchPromotions()) getPromotions: () => dispatch(fetchPromotions()),
getUserCart: userId => dispatch(fetchUserCart(userId))
}); });
export default connect(mSTP, mDTP)(ProductMarket); export default connect(mSTP, mDTP)(ProductMarket);
\ No newline at end of file
...@@ -14,6 +14,7 @@ export default class ProductMarket extends Component { ...@@ -14,6 +14,7 @@ export default class ProductMarket extends Component {
componentDidMount() { componentDidMount() {
this.props.getProducts(); this.props.getProducts();
this.props.getPromotions(); this.props.getPromotions();
// this.props.getUserCart(this.props.user.emailAddress);
} }
render() { render() {
......
import {SET_CURRENT_USER, LOGOUT_USER} from '../actions/session_actions' import {SET_CURRENT_USER, LOGOUT_USER} from '../actions/session_actions'
import { RECEIVE_USER_CART } from '../actions/cart_actions';
const initialState = {currentUser: null} const initialState = {
currentUser: null,
cart: {},
}
const userReducer = (state = initialState, action) => { const userReducer = (state = initialState, action) => {
Object.freeze(state); Object.freeze(state);
let newState = Object.assign({}, state); let newState = Object.assign({}, state);
switch (action.type) { switch (action.type) {
case SET_CURRENT_USER: case SET_CURRENT_USER:
return newState.currentUser = { emailAddress: 'fakeEmail@123'}; return newState.currentUser = { emailAddress: 'fakeEmail@123'};
case LOGOUT_USER: case LOGOUT_USER:
return newState.currentUser = null; return newState.currentUser = null;
case RECEIVE_USER_CART:
return newState.cart = action.cart
default: default:
return state; return state;
} }
......
import axios from 'axios';
export const fetchUserCart = userId => {
return axios.get(`http://localhost:8080/api/carts/${userId}`)
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment