Skip to content

Commit

Permalink
Deferred all data fetching to unblock ui on route change
Browse files Browse the repository at this point in the history
  • Loading branch information
Justkant committed Nov 1, 2015
1 parent 4fe6d53 commit 09cb09b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/containers/Market/Market.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class Market extends Component {
market: PropTypes.array
};

static fetchData(getState, dispatch) {
static fetchDataDeferred(getState, dispatch) {
if (!isMarketLoaded(getState())) {
return dispatch(loadMarket());
}
Expand All @@ -21,7 +21,7 @@ export default class Market extends Component {

return (
<div className={styles.container}>
{market.map((product) => {
{market && market.map((product) => {
return (<ProductVignette product={product} key={product.id}/>);
})}
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/containers/Product/Product.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default class Product extends Component {
params: PropTypes.object.isRequired
};

static fetchData(getState, dispatch, location, params) {
static fetchDataDeferred(getState, dispatch, location, params) {
if (!isProductLoaded(getState())) {
return dispatch(getById(params.id));
}
Expand Down
18 changes: 10 additions & 8 deletions src/containers/Products/Products.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class Products extends Component {
this.checkFile = this.checkFile.bind(this);
}

static fetchData(getState, dispatch) {
static fetchDataDeferred(getState, dispatch) {
if (!isAllLoaded(getState())) {
return dispatch(getAll());
}
Expand Down Expand Up @@ -105,13 +105,15 @@ export default class Products extends Component {
);

const productsList = [];
for (const product of products) {
productsList.push(
<div className={styles.element} key={product.id}>
<img src={'/api/' + product.imageUrl}/>
<h4>{product.title}</h4>
</div>
);
if (products) {
for (const product of products) {
productsList.push(
<div className={styles.element} key={product.id}>
<img src={'/api/' + product.imageUrl}/>
<h4>{product.title}</h4>
</div>
);
}
}

return (
Expand Down
4 changes: 2 additions & 2 deletions src/containers/Users/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class Users extends Component {
users: PropTypes.array
};

static fetchData(getState, dispatch) {
static fetchDataDeferred(getState, dispatch) {
if (!isUsersLoaded(getState())) {
return dispatch(getUsers());
}
Expand All @@ -20,7 +20,7 @@ export default class Users extends Component {

return (
<div className={styles.container}>
{users.map((user, index) => {
{users && users.map((user, index) => {
return (
<div className={styles.element} key={user.username + index}>
<img src="/default-user.png"/>
Expand Down
4 changes: 3 additions & 1 deletion src/redux/modules/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ export default function reducer(state = initialState, action = {}) {
case GETBYID:
return {
...state,
gettingById: true
gettingById: true,
product: null
};
case GETBYID_SUCCESS:
return {
Expand All @@ -91,6 +92,7 @@ export default function reducer(state = initialState, action = {}) {
...state,
gettingById: false,
gotById: false,
product: null,
error: action.error
};
default:
Expand Down

0 comments on commit 09cb09b

Please sign in to comment.