Ensure data is only loaded if not already loaded (needed to keep client/server state consistent)

This commit is contained in:
SteveSandersonMS
2016-02-09 23:06:30 -08:00
parent 3a567823e8
commit a84688d65c
9 changed files with 69 additions and 58 deletions

View File

@@ -7,6 +7,7 @@ import { ActionCreator } from './';
export interface FeaturedAlbumsState {
albums: Album[];
isLoaded: boolean;
}
export interface Album {
@@ -37,11 +38,13 @@ class ReceiveFeaturedAlbums extends Action {
export const actionCreators = {
requestFeaturedAlbums: (): ActionCreator => (dispatch, getState) => {
fetch('/api/albums/mostPopular')
.then(results => results.json())
.then(albums => dispatch(new ReceiveFeaturedAlbums(albums)));
return dispatch(new RequestFeaturedAlbums());
if (!getState().featuredAlbums.isLoaded) {
fetch('/api/albums/mostPopular')
.then(results => results.json())
.then(albums => dispatch(new ReceiveFeaturedAlbums(albums)));
return dispatch(new RequestFeaturedAlbums());
}
}
};
@@ -51,8 +54,8 @@ export const actionCreators = {
export const reducer: Reducer<FeaturedAlbumsState> = (state, action) => {
if (isActionType(action, ReceiveFeaturedAlbums)) {
return { albums: action.albums };
return { albums: action.albums, isLoaded: true };
} else {
return state || { albums: [] };
return state || { albums: [], isLoaded: false };
}
};