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 1
1
import {
2
2
STORIES_ADD ,
3
3
STORIES_FETCH ,
4
+ STORIES_FETCH_ERROR ,
4
5
} from '../constants/actionTypes' ;
5
6
6
7
const doAddStories = stories => ( {
@@ -13,7 +14,13 @@ const doFetchStories = query => ({
13
14
query,
14
15
} ) ;
15
16
17
+ const doFetchErrorStories = error => ( {
18
+ type : STORIES_FETCH_ERROR ,
19
+ error,
20
+ } ) ;
21
+
16
22
export {
17
23
doAddStories ,
18
24
doFetchStories ,
25
+ doFetchErrorStories ,
19
26
} ;
Original file line number Diff line number Diff line change 1
1
import React from 'react' ;
2
2
import { connect } from 'react-redux' ;
3
- import { getReadableStories } from '../selectors/story' ;
3
+ import {
4
+ getReadableStories ,
5
+ getFetchError ,
6
+ } from '../selectors/story' ;
4
7
import './Stories.css' ;
5
8
6
9
import Story from './Story' ;
@@ -27,10 +30,12 @@ const COLUMNS = {
27
30
} ,
28
31
} ;
29
32
30
- const Stories = ( { stories } ) =>
33
+ const Stories = ( { stories, error } ) =>
31
34
< div className = "stories" >
32
35
< StoriesHeader columns = { COLUMNS } />
33
36
37
+ { error && < p className = "error" > Something went wrong ...</ p > }
38
+
34
39
{ ( stories || [ ] ) . map ( story =>
35
40
< Story
36
41
key = { story . objectID }
@@ -54,6 +59,7 @@ const StoriesHeader = ({ columns }) =>
54
59
55
60
const mapStateToProps = state => ( {
56
61
stories : getReadableStories ( state ) ,
62
+ error : getFetchError ( state ) ,
57
63
} ) ;
58
64
59
65
export default connect (
Original file line number Diff line number Diff line change 1
1
export const STORY_ARCHIVE = 'STORY_ARCHIVE' ;
2
2
export const STORIES_FETCH = 'STORIES_FETCH' ;
3
+ export const STORIES_FETCH_ERROR = 'STORIES_FETCH_ERROR' ;
3
4
export 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' ;
2
5
3
- const INITIAL_STATE = [ ] ;
6
+ const INITIAL_STATE = {
7
+ stories : [ ] ,
8
+ error : null ,
9
+ } ;
4
10
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
+ } ) ;
7
20
8
21
function storyReducer ( state = INITIAL_STATE , action ) {
9
22
switch ( action . type ) {
10
23
case STORIES_ADD : {
11
24
return applyAddStories ( state , action ) ;
12
25
}
26
+ case STORIES_FETCH_ERROR : {
27
+ return applyFetchErrorStories ( state , action ) ;
28
+ }
13
29
default : return state ;
14
30
}
15
31
}
Original file line number Diff line number Diff line change 1
1
import { call , put } from 'redux-saga/effects' ;
2
- import { doAddStories } from '../actions/story' ;
2
+ import { doAddStories , doFetchErrorStories } from '../actions/story' ;
3
3
import { fetchStories } from '../api/story' ;
4
4
5
5
function * handleFetchStories ( action ) {
6
6
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
+ }
9
14
}
10
15
11
16
export {
Original file line number Diff line number Diff line change @@ -2,8 +2,12 @@ const isNotArchived = archivedIds => story =>
2
2
archivedIds . indexOf ( story . objectID ) === - 1 ;
3
3
4
4
const getReadableStories = ( { storyState, archiveState } ) =>
5
- storyState . filter ( isNotArchived ( archiveState ) ) ;
5
+ storyState . stories . filter ( isNotArchived ( archiveState ) ) ;
6
+
7
+ const getFetchError = ( { storyState } ) =>
8
+ storyState . error ;
6
9
7
10
export {
8
11
getReadableStories ,
12
+ getFetchError ,
9
13
} ;
You can’t perform that action at this time.
0 commit comments