Skip to content

Commit

Permalink
Merge pull request #2 from Rhymond/responsiveFixes
Browse files Browse the repository at this point in the history
Pretify code
  • Loading branch information
Rhymond authored Oct 8, 2017
2 parents 30f3f97 + 9f6d256 commit eeecbb5
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 67 deletions.
1 change: 0 additions & 1 deletion src/actions/index.js

This file was deleted.

17 changes: 8 additions & 9 deletions src/actions/product.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import * as types from '../constants/types';
import * as types from '../constants/types'

export function getProducts() {
return dispatch => {
export const getProducts = () =>
dispatch =>
fetch(`products.json`)
.then(response => response.json())
.then(response => {
dispatch({
type: types.FETCH_PRODUCTS,
payload: response.products
});
})
})
}
}

export function compare(product) {
return {type: types.COMPARE_PRODUCT, product};
}
export const compare = product => ({
type: types.COMPARE_PRODUCT,
product
})
4 changes: 2 additions & 2 deletions src/components/Compare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ const Compare = ({products}) =>
</tbody>
</table>
</div>
</div>;
</div>

export default Compare;
export default Compare
4 changes: 2 additions & 2 deletions src/components/Product/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ const Product = ({product, compare}) =>
</div>
</div>
</div>
</div>;
</div>

export default Product;
export default Product
6 changes: 3 additions & 3 deletions src/components/ProductList/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import {Product} from '../';
import {Product} from '../'

const ProductList = ({products, compare}) =>
<div>
Expand All @@ -8,6 +8,6 @@ const ProductList = ({products, compare}) =>
<Product key={product.id} product={product} compare={compare} />
)}
</div>
</div>;
</div>

export default ProductList;
export default ProductList
4 changes: 2 additions & 2 deletions src/constants/types.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const FETCH_PRODUCTS = 'FETCH_PRODUCTS';
export const COMPARE_PRODUCT = 'COMPARE_PRODUCT';
export const FETCH_PRODUCTS = 'FETCH_PRODUCTS'
export const COMPARE_PRODUCT = 'COMPARE_PRODUCT'
12 changes: 7 additions & 5 deletions src/containers/App/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import React, {Component} from 'react';
import React, {Component} from 'react'
import {Route, Switch} from 'react-router-dom'

import {Home, NotFound} from '../';
import {Home, NotFound} from '../'

export default class App extends Component {
class App extends Component {
render() {
return (
<div className="App">
<div className="app">
<div className="container mt-4">
<Switch>
<Route exact path="/" component={Home}/>
<Route component={NotFound}/>
</Switch>
</div>
</div>
);
)
}
}

export default App
46 changes: 20 additions & 26 deletions src/containers/Home/index.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
import React from 'react';
import {bindActionCreators} from 'redux';
import {Compare, ProductList} from '../../components';
import * as productActions from '../../actions';
import {connect} from 'react-redux';
import React, {Component} from 'react'
import {bindActionCreators} from 'redux'
import {Compare, ProductList} from '../../components'
import * as productActions from '../../actions/product'
import {connect} from 'react-redux'

class Home extends React.Component {
class Home extends Component {
componentWillMount() {
this.props.actions.getProducts();
this.props.actions.getProducts()
}

render() {
const {products, actions} = this.props;
const compareProducts = products.filter(product => product.compare);
const {products, actions} = this.props
const compareProducts = products.filter(product => product.compare)

return (
<div className="Home mt-5">
<div className="home mt-5">
<ProductList products={products} compare={actions.compare}/>
{compareProducts.length >= 2 ? <Compare products={compareProducts}/> : null}
{compareProducts.length >= 2 &&
<Compare products={compareProducts}/>
}
</div>
);
)
}
}

function mapStateToProps(state) {
return {
export default connect(
state => ({
products: state.product.products
}
}

function mapDispatchToProps(dispatch) {
return {
}),
dispatch => ({
actions: bindActionCreators(productActions, dispatch)
};
}

export default connect(
mapStateToProps,
mapDispatchToProps
)(Home);
})
)(Home)
10 changes: 6 additions & 4 deletions src/containers/NotFound/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import React from 'react'
import React, {Component} from 'react'

export default class NotFound extends React.Component {
class NotFound extends Component {
render() {
return (
<div>
<div className="not-found">
<h1>404</h1>
<h3>Page not found</h3>
</div>
);
)
}
}

export default NotFound
17 changes: 10 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import registerServiceWorker from './registerServiceWorker';
import React from 'react';
import ReactDOM from 'react-dom';
import registerServiceWorker from './registerServiceWorker'
import React from 'react'
import ReactDOM from 'react-dom'
import {BrowserRouter} from 'react-router-dom'
import {createStore, applyMiddleware} from 'redux'
import {Provider} from 'react-redux'
Expand All @@ -17,8 +17,8 @@ const loggerMiddleware = createLogger()
const store = createStore(
reducer,
applyMiddleware(
thunkMiddleware, // lets us dispatch() functions
loggerMiddleware // neat middleware that logs actions
thunkMiddleware,
loggerMiddleware
)
)

Expand All @@ -27,5 +27,8 @@ ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>
</Provider>, document.getElementById('root'));
registerServiceWorker();
</Provider>,
document.getElementById('root')
)

registerServiceWorker()
12 changes: 6 additions & 6 deletions src/reducers/product_reducer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as types from '../constants/types';
import * as types from '../constants/types'

const INITIAL_STATE = {
products: []
};
}

export default function (state = INITIAL_STATE, action) {
switch (action.type) {
Expand All @@ -11,16 +11,16 @@ export default function (state = INITIAL_STATE, action) {
...state, products: action.payload.map(product =>
({...product, compare: false})
)
};
}
case types.COMPARE_PRODUCT:
return {
...state, products: state.products.map(product =>
product.id === action.product.id ?
({...product, compare: !product.compare}) :
product
)
};
}
default:
return state;
return state
}
}
}

0 comments on commit eeecbb5

Please sign in to comment.