Skip to content

Commit

Permalink
[FEATURE] - simplify data fetching, organize structure
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelBenin committed Aug 9, 2017
1 parent 1918a4b commit 8ee228b
Show file tree
Hide file tree
Showing 24 changed files with 285 additions and 146 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// import { canUseDOM } from 'exenv';

export function repoDetailPageLoading(state = {}) {
return {
type: 'REPO_DETAIL_PAGE_LOADING',
isLoading: true,
state
};
}

export function repoDetailPageLoaded(state = {}) {
return {
type: 'REPO_DETAIL_PAGE_LOADED',
isLoading: false,
state
};
}

export function repoDetailPageLoadError(state = {}, error) {
return {
type: 'REPO_DETAIL_PAGE_ERROR',
isLoading: false,
state,
error
};
}
29 changes: 29 additions & 0 deletions src/redux/action_creators/search/search_action_creators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// https://api.github.com/searchs/michaelbenin
// import { canUseDOM } from 'exenv';

export function searchLoading(search = {}, state) {
return {
type: 'SEARCH_LOADING',
isLoading: true,
search,
state
};
}

export function searchLoaded(search, state) {
return {
type: 'SEARCH_LOADED',
isLoading: false,
search,
state
};
}

export function searchError(error, state) {
return {
type: 'SEARCH_ERROR',
isLoading: false,
error,
state
};
}
27 changes: 27 additions & 0 deletions src/redux/action_creators/search/search_page_action_creators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// https://api.github.com/searchs/michaelbenin
// import { canUseDOM } from 'exenv';

export function searchPageLoading(state = {}) {
return {
type: 'SEARCH_PAGE_LOADING',
isLoading: true,
state
};
}

export function searchPageLoaded(state = {}) {
return {
type: 'SEARCH_PAGE_LOADED',
isLoading: false,
state
};
}

export function searchPageError(error, state = {}) {
return {
type: 'SEARCH_PAGE_ERROR',
isLoading: false,
state,
error
};
}
27 changes: 0 additions & 27 deletions src/redux/action_creators/search_action_creators.js

This file was deleted.

24 changes: 0 additions & 24 deletions src/redux/actions/async_repo_detail_actions.js

This file was deleted.

26 changes: 0 additions & 26 deletions src/redux/actions/async_search_actions.js

This file was deleted.

17 changes: 17 additions & 0 deletions src/redux/actions/repo_detail/async_repo_detail_actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as repoDetailActions from '../../action_creators/repo_detail/repo_detail_action_creators';
import RepoDetailModel from '../../../models/repo_detail';
import notFoundActionCreator from '../../action_creators/not_found_status_action_creator';
import log from '../../../services/logger_service';

export default function fetchRepoDetail(params, dispatch, state) {
dispatch(repoDetailActions.repoDetailLoading());
return RepoDetailModel.fetch(params, state)
.then(function handleRepoDetailData(repoData) {
dispatch(repoDetailActions.repoDetailLoaded(repoData.data, state));
})
.catch(function handleUserError(err) {
log.error(err, 'Error in fetching repo.');
dispatch(notFoundActionCreator(500, 'ERROR_STATUS'));
dispatch(repoDetailActions.repoDetailLoadError(err, state));
});
}
19 changes: 19 additions & 0 deletions src/redux/actions/repo_detail/async_repo_detail_page_actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import P from 'bluebird';
import * as repoDetailPageActions from '../../action_creators/repo_detail/repo_detail_page_action_creators';
import notFoundActionCreator from '../../action_creators/not_found_status_action_creator';
import log from '../../../services/logger_service';

import repoDetailAsyncAction from './async_repo_detail_actions';

export default function fetchRepoDetail(params, dispatch, state) {
dispatch(repoDetailPageActions.repoDetailPageLoading());
return P.all([repoDetailAsyncAction(params, dispatch, state)])
.then(function handleRepoDetailData() {
dispatch(repoDetailPageActions.repoDetailPageLoaded());
})
.catch(function handleUserError(err) {
log.error(err, 'Error in fetching repo detail page.');
dispatch(notFoundActionCreator(500, 'ERROR_STATUS'));
dispatch(repoDetailPageActions.repoDetailPageLoadError(err, state));
});
}
19 changes: 19 additions & 0 deletions src/redux/actions/search/async_search_actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// https://api.github.com/searchs/michaelbenin
// import { canUseDOM } from 'exenv';
import * as searchActions from '../../action_creators/search/search_action_creators';
import notFoundActionCreator from '../../action_creators/not_found_status_action_creator';
import SearchModel from '../../../models/search';
import log from '../../../services/logger_service';

export default function fetchSearchAction(query, dispatch, state) {
dispatch(searchActions.searchLoading());
return SearchModel.fetch(query, state)
.then(function handleSearchModelData(searchData) {
dispatch(searchActions.searchLoaded(searchData.data, state));
})
.catch(function handleSearchError(err) {
log.error(err, 'Error in fetching repo.');
dispatch(notFoundActionCreator(500, 'ERROR_STATUS'));
dispatch(searchActions.searchError(err, state));
});
}
20 changes: 20 additions & 0 deletions src/redux/actions/search/async_search_page_actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// https://api.github.com/searchs/michaelbenin
// import { canUseDOM } from 'exenv';
import P from 'bluebird';
import * as searchPageActions from '../../action_creators/search/search_page_action_creators';
import notFoundActionCreator from '../../action_creators/not_found_status_action_creator';
import log from '../../../services/logger_service';
import searchAsyncAction from './async_search_actions';

export default function fetchRepoDetail(params, dispatch, state) {
dispatch(searchPageActions.searchPageLoading());
return P.all([searchAsyncAction(params, dispatch, state)])
.then(function handleRepoDetailData() {
dispatch(searchPageActions.searchPageLoaded());
})
.catch(function handleUserError(err) {
log.error(err, 'Error in search page.');
dispatch(notFoundActionCreator(500, 'ERROR_STATUS'));
dispatch(searchPageActions.searchPageLoadError(err, state));
});
}
33 changes: 21 additions & 12 deletions src/redux/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import { combineReducers } from 'redux';
import { routerReducer } from 'react-router-redux';
import { routerReducer as routing } from 'react-router-redux';

import configReducer from './config_reducer';
import metaReducer from './meta_reducer';
import statusReducer from './status_reducer';
import repoDetailReducer from './repo_detail_reducer';
import searchResultsReducer from './search_results_reducer';
import config from './config_reducer';
import meta from './meta_reducer';
import status from './status_reducer';

import repoDetail from './repo_detail/repo_detail_reducer';
import repoDetailPage from './repo_detail/repo_detail_page_reducer';

import searchPage from './search/search_page_reducer';
import search from './search/search_results_reducer';

export default combineReducers({
routing: routerReducer,
config: configReducer,
meta: metaReducer,
status: statusReducer,
repoDetail: repoDetailReducer,
search: searchResultsReducer
routing,

config,
meta,
status,

repoDetailPage,
repoDetail,

searchPage,
search
});
29 changes: 29 additions & 0 deletions src/redux/reducers/repo_detail/repo_detail_page_reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// https://github.com/reactjs/redux/issues/99
// import { canUseDOM } from 'exenv';

export default function(state = {}, action) {
const typeMap = {
REPO_DETAIL_PAGE_LOADING() {
return {
isLoading: true
};
},
REPO_DETAIL_PAGE_LOADED() {
return {
isLoading: false
};
},
REPO_DETAIL_PAGE_ERROR() {
return {
isLoading: false,
error: action.error
};
}
};

if (typeMap[action.type]) {
return typeMap[action.type]();
}

return state;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ export default function(state = {}, action) {
const typeMap = {
REPO_DETAIL_LOADING() {
return {
isLoading: true,
repo: action.repo
isLoading: true
};
},
REPO_DETAIL_LOADED() {
Expand All @@ -18,8 +17,7 @@ export default function(state = {}, action) {
REPO_DETAIL_ERROR() {
return {
isLoading: false,
error: true,
errorMessage: action.repo.errorMessage
error: action.error
};
}
};
Expand Down
29 changes: 29 additions & 0 deletions src/redux/reducers/search/search_page_reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// https://github.com/reactjs/redux/issues/99
// import { canUseDOM } from 'exenv';

export default function(state = {}, action) {
const typeMap = {
SEARCH_LOADING() {
return {
isLoading: true
};
},
SEARCH_LOADED() {
return {
isLoading: false
};
},
SEARCH_ERROR() {
return {
isLoading: false,
error: action.error
};
}
};

if (typeMap[action.type]) {
return typeMap[action.type]();
}

return state;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ export default function(state = {}, action) {
SEARCH_ERROR() {
return {
isLoading: false,
error: true,
errorMessage: action.search.errorMessage
error: action.error
};
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { connect } from 'react-redux';
import { withRouter } from 'react-router';
import get from 'lodash/get';

import loadData from './repo_detail_state_manager';
import loadData from './repo_detail_page_data_fetch';

// eslint-disable-next-line react/prefer-stateless-function
class RepoDetail extends Component {
componentWillMount() {
if (!this.props.state.config.initialPageLoad) {
Expand Down
Loading

0 comments on commit 8ee228b

Please sign in to comment.