File tree Expand file tree Collapse file tree 6 files changed +49
-10
lines changed Expand file tree Collapse file tree 6 files changed +49
-10
lines changed Original file line number Diff line number Diff line change 11import {
22 STORIES_ADD ,
33 STORIES_FETCH ,
4+ STORIES_FETCH_ERROR ,
45} from '../constants/actionTypes' ;
56
67const doAddStories = stories => ( {
@@ -13,7 +14,13 @@ const doFetchStories = query => ({
1314 query,
1415} ) ;
1516
17+ const doFetchErrorStories = error => ( {
18+ type : STORIES_FETCH_ERROR ,
19+ error,
20+ } ) ;
21+
1622export {
1723 doAddStories ,
1824 doFetchStories ,
25+ doFetchErrorStories ,
1926} ;
Original file line number Diff line number Diff line change 11import React from 'react' ;
22import { connect } from 'react-redux' ;
3- import { getReadableStories } from '../selectors/story' ;
3+ import {
4+ getReadableStories ,
5+ getFetchError ,
6+ } from '../selectors/story' ;
47import './Stories.css' ;
58
69import Story from './Story' ;
@@ -27,10 +30,12 @@ const COLUMNS = {
2730 } ,
2831} ;
2932
30- const Stories = ( { stories } ) =>
33+ const Stories = ( { stories, error } ) =>
3134 < div className = "stories" >
3235 < StoriesHeader columns = { COLUMNS } />
3336
37+ { error && < p className = "error" > Something went wrong ...</ p > }
38+
3439 { ( stories || [ ] ) . map ( story =>
3540 < Story
3641 key = { story . objectID }
@@ -54,6 +59,7 @@ const StoriesHeader = ({ columns }) =>
5459
5560const mapStateToProps = state => ( {
5661 stories : getReadableStories ( state ) ,
62+ error : getFetchError ( state ) ,
5763} ) ;
5864
5965export default connect (
Original file line number Diff line number Diff line change 11export const STORY_ARCHIVE = 'STORY_ARCHIVE' ;
22export const STORIES_FETCH = 'STORIES_FETCH' ;
3+ export const STORIES_FETCH_ERROR = 'STORIES_FETCH_ERROR' ;
34export const STORIES_ADD = 'STORIES_ADD' ;
Original file line number Diff line number Diff line change 1- import { STORIES_ADD } from '../constants/actionTypes' ;
1+ import {
2+ STORIES_ADD ,
3+ STORIES_FETCH_ERROR ,
4+ } from '../constants/actionTypes' ;
25
3- const INITIAL_STATE = [ ] ;
6+ const INITIAL_STATE = {
7+ stories : [ ] ,
8+ error : null ,
9+ } ;
410
5- const applyAddStories = ( state , action ) =>
6- action . stories ;
11+ const applyAddStories = ( state , action ) => ( {
12+ stories : action . stories ,
13+ error : null ,
14+ } ) ;
15+
16+ const applyFetchErrorStories = ( state , action ) => ( {
17+ stories : [ ] ,
18+ error : action . error ,
19+ } ) ;
720
821function storyReducer ( state = INITIAL_STATE , action ) {
922 switch ( action . type ) {
1023 case STORIES_ADD : {
1124 return applyAddStories ( state , action ) ;
1225 }
26+ case STORIES_FETCH_ERROR : {
27+ return applyFetchErrorStories ( state , action ) ;
28+ }
1329 default : return state ;
1430 }
1531}
Original file line number Diff line number Diff line change 11import { call , put } from 'redux-saga/effects' ;
2- import { doAddStories } from '../actions/story' ;
2+ import { doAddStories , doFetchErrorStories } from '../actions/story' ;
33import { fetchStories } from '../api/story' ;
44
55function * handleFetchStories ( action ) {
66 const { query } = action ;
7- const result = yield call ( fetchStories , query ) ;
8- yield put ( doAddStories ( result . hits ) ) ;
7+
8+ try {
9+ const result = yield call ( fetchStories , query ) ;
10+ yield put ( doAddStories ( result . hits ) ) ;
11+ } catch ( error ) {
12+ yield put ( doFetchErrorStories ( error ) ) ;
13+ }
914}
1015
1116export {
Original file line number Diff line number Diff line change @@ -2,8 +2,12 @@ const isNotArchived = archivedIds => story =>
22 archivedIds . indexOf ( story . objectID ) === - 1 ;
33
44const getReadableStories = ( { storyState, archiveState } ) =>
5- storyState . filter ( isNotArchived ( archiveState ) ) ;
5+ storyState . stories . filter ( isNotArchived ( archiveState ) ) ;
6+
7+ const getFetchError = ( { storyState } ) =>
8+ storyState . error ;
69
710export {
811 getReadableStories ,
12+ getFetchError ,
913} ;
You can’t perform that action at this time.
0 commit comments