From 9f6d256a96cf6c1a4f83e034b20657b9fd8023de Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 8 Oct 2017 16:55:00 +0100 Subject: [PATCH] Pretify code, remove unnecessary semicolon, function => const --- src/actions/index.js | 1 - src/actions/product.js | 17 +++++------ src/components/Compare/index.js | 4 +-- src/components/Product/index.js | 4 +-- src/components/ProductList/index.js | 6 ++-- src/constants/types.js | 4 +-- src/containers/App/index.js | 12 ++++---- src/containers/Home/index.js | 46 +++++++++++++---------------- src/containers/NotFound/index.js | 10 ++++--- src/index.js | 17 ++++++----- src/reducers/product_reducer.js | 12 ++++---- 11 files changed, 66 insertions(+), 67 deletions(-) delete mode 100644 src/actions/index.js diff --git a/src/actions/index.js b/src/actions/index.js deleted file mode 100644 index b8cdfb1..0000000 --- a/src/actions/index.js +++ /dev/null @@ -1 +0,0 @@ -export * from './product'; \ No newline at end of file diff --git a/src/actions/product.js b/src/actions/product.js index 0f9139e..9d14b68 100644 --- a/src/actions/product.js +++ b/src/actions/product.js @@ -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 + }) diff --git a/src/components/Compare/index.js b/src/components/Compare/index.js index 5e119e1..a970dae 100644 --- a/src/components/Compare/index.js +++ b/src/components/Compare/index.js @@ -43,6 +43,6 @@ const Compare = ({products}) => - ; + -export default Compare; +export default Compare diff --git a/src/components/Product/index.js b/src/components/Product/index.js index 6b11804..083973a 100644 --- a/src/components/Product/index.js +++ b/src/components/Product/index.js @@ -17,6 +17,6 @@ const Product = ({product, compare}) => - ; + -export default Product; +export default Product diff --git a/src/components/ProductList/index.js b/src/components/ProductList/index.js index 453ef29..5abcdaa 100644 --- a/src/components/ProductList/index.js +++ b/src/components/ProductList/index.js @@ -1,5 +1,5 @@ import React from 'react' -import {Product} from '../'; +import {Product} from '../' const ProductList = ({products, compare}) =>
@@ -8,6 +8,6 @@ const ProductList = ({products, compare}) => )}
- ; + -export default ProductList; +export default ProductList diff --git a/src/constants/types.js b/src/constants/types.js index 11a8217..c8bf56b 100644 --- a/src/constants/types.js +++ b/src/constants/types.js @@ -1,2 +1,2 @@ -export const FETCH_PRODUCTS = 'FETCH_PRODUCTS'; -export const COMPARE_PRODUCT = 'COMPARE_PRODUCT'; \ No newline at end of file +export const FETCH_PRODUCTS = 'FETCH_PRODUCTS' +export const COMPARE_PRODUCT = 'COMPARE_PRODUCT' diff --git a/src/containers/App/index.js b/src/containers/App/index.js index 6ee8c11..95ecee9 100644 --- a/src/containers/App/index.js +++ b/src/containers/App/index.js @@ -1,12 +1,12 @@ -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 ( -
+
@@ -14,6 +14,8 @@ export default class App extends Component {
- ); + ) } } + +export default App diff --git a/src/containers/Home/index.js b/src/containers/Home/index.js index 9e21910..c5acc8a 100644 --- a/src/containers/Home/index.js +++ b/src/containers/Home/index.js @@ -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 ( -
+
- {compareProducts.length >= 2 ? : null} + {compareProducts.length >= 2 && + + }
- ); + ) } } -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) diff --git a/src/containers/NotFound/index.js b/src/containers/NotFound/index.js index f765568..f001b00 100644 --- a/src/containers/NotFound/index.js +++ b/src/containers/NotFound/index.js @@ -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 ( -
+

404

Page not found

- ); + ) } } + +export default NotFound diff --git a/src/index.js b/src/index.js index 1848291..1e849ed 100644 --- a/src/index.js +++ b/src/index.js @@ -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' @@ -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 ) ) @@ -27,5 +27,8 @@ ReactDOM.render( - , document.getElementById('root')); -registerServiceWorker(); + , + document.getElementById('root') +) + +registerServiceWorker() diff --git a/src/reducers/product_reducer.js b/src/reducers/product_reducer.js index 8dfd65a..ae6d415 100644 --- a/src/reducers/product_reducer.js +++ b/src/reducers/product_reducer.js @@ -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) { @@ -11,7 +11,7 @@ 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 => @@ -19,8 +19,8 @@ export default function (state = INITIAL_STATE, action) { ({...product, compare: !product.compare}) : product ) - }; + } default: - return state; + return state } -} \ No newline at end of file +}