Skip to content

Commit

Permalink
moved getting orders for dashboard to App component
Browse files Browse the repository at this point in the history
this way the orders are downloaded only once at the start of the application instead of every time when the user navigates to `/`
  • Loading branch information
tarnas14 committed Mar 27, 2016
1 parent 803581e commit 9ee5fb8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
22 changes: 22 additions & 0 deletions src/components/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import OrderDetails from './orderDetails';
import {orders, activeOrder} from '../store/orders';
import localization from '../store/localization';
import changeLocale from '../store/localizationActions';
import {hydrateOrders} from '../store/ordersActions';

// Add the reducer to your store on the `routing` key
const store = createStore(
Expand All @@ -31,9 +32,30 @@ const App = connect(state => ({resources: state.localization.resources.app}))(
React.createClass({
propTypes: {
children: React.PropTypes.object,
dispatch: React.PropTypes.func.isRequired,
resources: React.PropTypes.object.isRequired
},

componentDidMount () {
fetch('/api/orders')
.then(response => response.json())
.then(orders => {
const ordersToday = orders.map(orderWithStringDates => {
return {
...orderWithStringDates,
deadline: new Date(orderWithStringDates.deadline)
}
}).filter(order => {
const today = new Date();
return today.getFullYear() === order.deadline.getFullYear() &&
today.getMonth() === order.deadline.getMonth() &&
today.getDate() === order.deadline.getDate();
});

this.props.dispatch(hydrateOrders(ordersToday));
});
},

render () {
return (
<Grid>
Expand Down
24 changes: 1 addition & 23 deletions src/components/dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,18 @@ import {Row, Col, Alert, Button} from 'react-bootstrap';
import {Link} from 'react-router';
import OrderTile from './orderTile';
import {connect} from 'react-redux';
import {hydrateOrders} from '../store/ordersActions';

const Dashboard = React.createClass({
propTypes: {
dispatch: React.PropTypes.func.isRequired,
orders: React.PropTypes.array.isRequired,
resources: React.PropTypes.object.isRequired
},

componentDidMount () {
fetch('/api/orders')
.then(response => response.json())
.then(orders => {
const ordersToday = orders.map(orderWithStringDates => {
return {
...orderWithStringDates,
deadline: new Date(orderWithStringDates.deadline)
}
}).filter(order => {
const today = new Date();
return today.getFullYear() === order.deadline.getFullYear() &&
today.getMonth() === order.deadline.getMonth() &&
today.getDate() === order.deadline.getDate();
});

this.props.dispatch(hydrateOrders(ordersToday));
});
},

_renderOrderTiles () {
return this.props.orders.map(order => <OrderTile key={order.id} {...order} />);
},

_getTileContainerStyles() {
_getTileContainerStyles () {
return {
marginTop: '1em'
};
Expand Down

0 comments on commit 9ee5fb8

Please sign in to comment.