Skip to content

Commit

Permalink
Fix various issues with local currency setting
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmcdermott committed Mar 16, 2017
1 parent 49af244 commit 934bd85
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"env": {
"browser": true,
"node": true
"node": false
},
"extends": "airbnb"
}
2 changes: 1 addition & 1 deletion src/3-refactorable/bad/Inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default class Inventory extends Component {
</th>

<th>
Add to Cart
Cart
</th>
</tr>

Expand Down
5 changes: 5 additions & 0 deletions src/3-refactorable/good/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ export const addToCart = productId => ({
type: types.ADD_TO_CART,
productId,
});

export const changeCurrency = currency => ({
type: types.CHANGE_CURRENCY,
currency,
});
1 change: 0 additions & 1 deletion src/3-refactorable/good/components/Cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export default class Cart extends Component {
localCurrency: props.localCurrency,
};

console.warn(props);
this.CurrencyConverter = props.currencyConverter;
}

Expand Down
16 changes: 8 additions & 8 deletions src/3-refactorable/good/components/Inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@ export default class Inventory extends Component {
constructor(props) {
super();
this.state = {
localCurrency: window.localCurrency,
localCurrency: props.localCurrency,
inventory: props.inventory,
};

console.warn(props);

this.addToCart = props.addToCart;

this.changeCurrency = props.changeCurrency;
this.CurrencyConverter = props.currencyConverter;
}

onSelectCurrency(e) {
window.localCurrency = e.target.value;
this.setState({
localCurrency: e.target.value,
});
console.warn(e.target.value);
this.changeCurrency(e.target.value);
}

onAddToCart(itemId) {
Expand Down Expand Up @@ -58,6 +54,10 @@ export default class Inventory extends Component {
<th>
Price
</th>

<th>
Cart
</th>
</tr>

{Object.keys(this.state.inventory).map(itemId => (
Expand Down
5 changes: 3 additions & 2 deletions src/3-refactorable/good/containers/Cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { connect } from 'react-redux';
import CurrencyConverter from '../lib/CurrencyConverter';
import Cart from '../components/Cart';

const CartContainer = ({ cart, inventory, currencies }) => (
const CartContainer = ({ cart, inventory, currencies, localCurrency }) => (
<Cart
cart={cart}
currencyConverter={new CurrencyConverter(currencies)}
localCurrency={window.localCurrency}
localCurrency={localCurrency}
inventory={inventory}
/>
);
Expand All @@ -17,6 +17,7 @@ const mapStateToProps = state => ({
cart: state.cart,
inventory: state.inventory,
currencies: state.currencies,
localCurrency: state.localCurrency,
});

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

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

const InventoryContainer = ({ inventory, currencies, addToCart }) => (
const InventoryContainer = ({ inventory, currencies, localCurrency, addToCart, changeCurrency }) => (
<Inventory
currencyConverter={new CurrencyConverter(currencies)}
inventory={inventory}
localCurrency={localCurrency}
changeCurrency={currency => changeCurrency(currency)}
addToCart={productId => addToCart(productId)}
/>
);

const mapStateToProps = state => ({
inventory: state.inventory,
currencies: state.currencies,
localCurrency: state.localCurrency,
});

export default connect(mapStateToProps, { addToCart })(InventoryContainer);
export default connect(mapStateToProps, { addToCart, changeCurrency })(InventoryContainer);
2 changes: 0 additions & 2 deletions src/3-refactorable/good/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const store = createStore(reducer);
export default class RefactorableGood extends Component {
constructor(props) {
super();

window.localCurrency = 'usd';
}

render() {
Expand Down
2 changes: 2 additions & 0 deletions src/3-refactorable/good/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { combineReducers } from 'redux';
import cart from './cart';
import inventory from './inventory';
import currencies from './currencies';
import localCurrency from './localCurrency';

export default combineReducers({
cart,
inventory,
currencies,
localCurrency,
});
4 changes: 2 additions & 2 deletions src/3-refactorable/good/reducers/inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const initialState = {
2: {
product: 'Tin can',
img: '/tin_can.jpg',
desc: 'A ridiculously expensive tin can',
price: 42,
desc: 'Pretty much what you would expect from a tin can',
price: 32,
currency: 'usd',
},
3: {
Expand Down
12 changes: 12 additions & 0 deletions src/3-refactorable/good/reducers/localCurrency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { CHANGE_CURRENCY } from '../constants/ActionTypes';

const initialState = 'usd';

export default (state = initialState, action) => {
switch (action.type) {
case 'CHANGE_CURRENCY':
return action.currency;
default:
return state;
}
};

0 comments on commit 934bd85

Please sign in to comment.