Compare commits
1 Commits
responsive
...
1.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
30f3f9787a |
1
src/actions/index.js
Normal file
1
src/actions/index.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export * from './product';
|
||||||
@@ -1,17 +1,18 @@
|
|||||||
import * as types from '../constants/types'
|
import * as types from '../constants/types';
|
||||||
|
|
||||||
export const getProducts = () =>
|
export function getProducts() {
|
||||||
dispatch =>
|
return dispatch => {
|
||||||
fetch(`products.json`)
|
fetch(`products.json`)
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(response => {
|
.then(response => {
|
||||||
dispatch({
|
dispatch({
|
||||||
type: types.FETCH_PRODUCTS,
|
type: types.FETCH_PRODUCTS,
|
||||||
payload: response.products
|
payload: response.products
|
||||||
|
});
|
||||||
})
|
})
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const compare = product => ({
|
export function compare(product) {
|
||||||
type: types.COMPARE_PRODUCT,
|
return {type: types.COMPARE_PRODUCT, product};
|
||||||
product
|
}
|
||||||
})
|
|
||||||
|
|||||||
@@ -43,6 +43,6 @@ const Compare = ({products}) =>
|
|||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>;
|
||||||
|
|
||||||
export default Compare
|
export default Compare;
|
||||||
|
|||||||
@@ -17,6 +17,6 @@ const Product = ({product, compare}) =>
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>;
|
||||||
|
|
||||||
export default Product
|
export default Product;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import {Product} from '../'
|
import {Product} from '../';
|
||||||
|
|
||||||
const ProductList = ({products, compare}) =>
|
const ProductList = ({products, compare}) =>
|
||||||
<div>
|
<div>
|
||||||
@@ -8,6 +8,6 @@ const ProductList = ({products, compare}) =>
|
|||||||
<Product key={product.id} product={product} compare={compare} />
|
<Product key={product.id} product={product} compare={compare} />
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>;
|
||||||
|
|
||||||
export default ProductList
|
export default ProductList;
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
export const FETCH_PRODUCTS = 'FETCH_PRODUCTS'
|
export const FETCH_PRODUCTS = 'FETCH_PRODUCTS';
|
||||||
export const COMPARE_PRODUCT = 'COMPARE_PRODUCT'
|
export const COMPARE_PRODUCT = 'COMPARE_PRODUCT';
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
import React, {Component} from 'react'
|
import React, {Component} from 'react';
|
||||||
import {Route, Switch} from 'react-router-dom'
|
import {Route, Switch} from 'react-router-dom'
|
||||||
|
|
||||||
import {Home, NotFound} from '../'
|
import {Home, NotFound} from '../';
|
||||||
|
|
||||||
class App extends Component {
|
export default class App extends Component {
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div className="app">
|
<div className="App">
|
||||||
<div className="container mt-4">
|
<div className="container mt-4">
|
||||||
<Switch>
|
<Switch>
|
||||||
<Route exact path="/" component={Home}/>
|
<Route exact path="/" component={Home}/>
|
||||||
@@ -14,8 +14,6 @@ class App extends Component {
|
|||||||
</Switch>
|
</Switch>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default App
|
|
||||||
|
|||||||
@@ -1,34 +1,40 @@
|
|||||||
import React, {Component} from 'react'
|
import React from 'react';
|
||||||
import {bindActionCreators} from 'redux'
|
import {bindActionCreators} from 'redux';
|
||||||
import {Compare, ProductList} from '../../components'
|
import {Compare, ProductList} from '../../components';
|
||||||
import * as productActions from '../../actions/product'
|
import * as productActions from '../../actions';
|
||||||
import {connect} from 'react-redux'
|
import {connect} from 'react-redux';
|
||||||
|
|
||||||
class Home extends Component {
|
class Home extends React.Component {
|
||||||
componentWillMount() {
|
componentWillMount() {
|
||||||
this.props.actions.getProducts()
|
this.props.actions.getProducts();
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const {products, actions} = this.props
|
const {products, actions} = this.props;
|
||||||
const compareProducts = products.filter(product => product.compare)
|
const compareProducts = products.filter(product => product.compare);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="home mt-5">
|
<div className="Home mt-5">
|
||||||
<ProductList products={products} compare={actions.compare}/>
|
<ProductList products={products} compare={actions.compare}/>
|
||||||
{compareProducts.length >= 2 &&
|
{compareProducts.length >= 2 ? <Compare products={compareProducts}/> : null}
|
||||||
<Compare products={compareProducts}/>
|
|
||||||
}
|
|
||||||
</div>
|
</div>
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default connect(
|
function mapStateToProps(state) {
|
||||||
state => ({
|
return {
|
||||||
products: state.product.products
|
products: state.product.products
|
||||||
}),
|
}
|
||||||
dispatch => ({
|
}
|
||||||
|
|
||||||
|
function mapDispatchToProps(dispatch) {
|
||||||
|
return {
|
||||||
actions: bindActionCreators(productActions, dispatch)
|
actions: bindActionCreators(productActions, dispatch)
|
||||||
})
|
};
|
||||||
)(Home)
|
}
|
||||||
|
|
||||||
|
export default connect(
|
||||||
|
mapStateToProps,
|
||||||
|
mapDispatchToProps
|
||||||
|
)(Home);
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
import React, {Component} from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
class NotFound extends Component {
|
export default class NotFound extends React.Component {
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div className="not-found">
|
<div>
|
||||||
<h1>404</h1>
|
<h1>404</h1>
|
||||||
<h3>Page not found</h3>
|
<h3>Page not found</h3>
|
||||||
</div>
|
</div>
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default NotFound
|
|
||||||
|
|||||||
17
src/index.js
17
src/index.js
@@ -1,6 +1,6 @@
|
|||||||
import registerServiceWorker from './registerServiceWorker'
|
import registerServiceWorker from './registerServiceWorker';
|
||||||
import React from 'react'
|
import React from 'react';
|
||||||
import ReactDOM from 'react-dom'
|
import ReactDOM from 'react-dom';
|
||||||
import {BrowserRouter} from 'react-router-dom'
|
import {BrowserRouter} from 'react-router-dom'
|
||||||
import {createStore, applyMiddleware} from 'redux'
|
import {createStore, applyMiddleware} from 'redux'
|
||||||
import {Provider} from 'react-redux'
|
import {Provider} from 'react-redux'
|
||||||
@@ -17,8 +17,8 @@ const loggerMiddleware = createLogger()
|
|||||||
const store = createStore(
|
const store = createStore(
|
||||||
reducer,
|
reducer,
|
||||||
applyMiddleware(
|
applyMiddleware(
|
||||||
thunkMiddleware,
|
thunkMiddleware, // lets us dispatch() functions
|
||||||
loggerMiddleware
|
loggerMiddleware // neat middleware that logs actions
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -27,8 +27,5 @@ ReactDOM.render(
|
|||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
<App />
|
<App />
|
||||||
</BrowserRouter>
|
</BrowserRouter>
|
||||||
</Provider>,
|
</Provider>, document.getElementById('root'));
|
||||||
document.getElementById('root')
|
registerServiceWorker();
|
||||||
)
|
|
||||||
|
|
||||||
registerServiceWorker()
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import * as types from '../constants/types'
|
import * as types from '../constants/types';
|
||||||
|
|
||||||
const INITIAL_STATE = {
|
const INITIAL_STATE = {
|
||||||
products: []
|
products: []
|
||||||
}
|
};
|
||||||
|
|
||||||
export default function (state = INITIAL_STATE, action) {
|
export default function (state = INITIAL_STATE, action) {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
@@ -11,7 +11,7 @@ export default function (state = INITIAL_STATE, action) {
|
|||||||
...state, products: action.payload.map(product =>
|
...state, products: action.payload.map(product =>
|
||||||
({...product, compare: false})
|
({...product, compare: false})
|
||||||
)
|
)
|
||||||
}
|
};
|
||||||
case types.COMPARE_PRODUCT:
|
case types.COMPARE_PRODUCT:
|
||||||
return {
|
return {
|
||||||
...state, products: state.products.map(product =>
|
...state, products: state.products.map(product =>
|
||||||
@@ -19,8 +19,8 @@ export default function (state = INITIAL_STATE, action) {
|
|||||||
({...product, compare: !product.compare}) :
|
({...product, compare: !product.compare}) :
|
||||||
product
|
product
|
||||||
)
|
)
|
||||||
}
|
};
|
||||||
default:
|
default:
|
||||||
return state
|
return state;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user