Skip to content

Commit

Permalink
Finish good refactorable cart
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmcdermott committed Mar 13, 2017
1 parent c1e0ff6 commit 7e10342
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 26 deletions.
10 changes: 4 additions & 6 deletions src/3-refactorable/good/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as types from '../constants/ActionTypes';

export const addToCart = productId => (dispatch, getState) => {
dispatch({
type: types.ADD_TO_CART,
productId,
});
};
export const addToCart = productId => ({
type: types.ADD_TO_CART,
productId,
});
11 changes: 11 additions & 0 deletions src/3-refactorable/good/components/Cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@ export default class Cart extends Component {
constructor(props) {
super();

this.state = {
cart: props.cart,
inventory: props.inventory,
localCurrency: props.localCurrency,
};

this.CurrencyConverter = props.currencyConverter;
}

componentWillReceiveProps(nextProps) {
this.setState({
cart: nextProps.cart,
});
}
render() {
return (
<div>
Expand Down
3 changes: 2 additions & 1 deletion src/3-refactorable/good/components/Inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class Inventory extends Component {
}

onSelectCurrency(e) {
window.localCurrency = e.target.value;
this.setState({
localCurrency: e.target.value,
});
Expand Down Expand Up @@ -80,7 +81,7 @@ export default class Inventory extends Component {
</td>

<td>
<button onClick={() => this.onAddToCart(itemId).bind(this)}>
<button onClick={() => this.onAddToCart(itemId)}>
Add to Cart
</button>
</td>
Expand Down
10 changes: 2 additions & 8 deletions src/3-refactorable/good/containers/App.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import React, { Component } from 'react';
import { createStore } from 'redux';
import { Provider, connect } from 'react-redux';

import Inventory from '../components/Inventory';
import Cart from '../components/Cart';
import Inventory from '../containers/Inventory';
import Cart from '../containers/Cart';

export default class App extends Component {
constructor() {
super();
}

render() {
return (
<div>
Expand Down
9 changes: 4 additions & 5 deletions src/3-refactorable/good/containers/Cart.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import React, { PropTypes } from 'react';
import React from 'react';
import { connect } from 'react-redux';
import addToCart from '../actions';

import CurrencyConverter from '../lib/CurrencyConverter';
import Cart from '../components/Cart';

const CartContainer = ({ cart, addToCart }) => (
const CartContainer = ({ cart }) => (
<Cart
cart={cart}
currencyConverter={new CurrencyConverter(window.currencyConversions)}
localCurrency={window.localCurrency}
inventory={window.inventory}
addToCart={productId => addToCart(productId)}
/>
);

const mapStateToProps = state => ({
cart: state.cart,
});

export default connect(mapStateToProps, { addToCart })(CartContainer);
export default connect(mapStateToProps, {})(CartContainer);
4 changes: 2 additions & 2 deletions src/3-refactorable/good/containers/Inventory.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { PropTypes } from 'react';
import React from 'react';
import { connect } from 'react-redux';
import addToCart from '../actions';
import { addToCart } from '../actions';

import CurrencyConverter from '../lib/CurrencyConverter';
import Inventory from '../components/Inventory';
Expand Down
6 changes: 2 additions & 4 deletions src/3-refactorable/good/reducers/cart.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { ADD_TO_CART } from '../constants/ActionTypes';

const initialState = {
productIds: [],
};
const initialState = [];

export default (state = initialState, action) => {
switch (action.type) {
case 'ADD_TO_CART':
return [...state, action.productId];
return [...state, parseInt(action.productId)];
default:
return state;
}
Expand Down

0 comments on commit 7e10342

Please sign in to comment.