Skip to content

Commit a1f6a88

Browse files
committed
part 16
1 parent b6a6e59 commit a1f6a88

File tree

6 files changed

+49
-10
lines changed

6 files changed

+49
-10
lines changed

src/actions/story.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
STORIES_ADD,
33
STORIES_FETCH,
4+
STORIES_FETCH_ERROR,
45
} from '../constants/actionTypes';
56

67
const 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+
1622
export {
1723
doAddStories,
1824
doFetchStories,
25+
doFetchErrorStories,
1926
};

src/components/Stories.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import React from 'react';
22
import { connect } from 'react-redux';
3-
import { getReadableStories } from '../selectors/story';
3+
import {
4+
getReadableStories,
5+
getFetchError,
6+
} from '../selectors/story';
47
import './Stories.css';
58

69
import 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

5560
const mapStateToProps = state => ({
5661
stories: getReadableStories(state),
62+
error: getFetchError(state),
5763
});
5864

5965
export default connect(

src/constants/actionTypes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const STORY_ARCHIVE = 'STORY_ARCHIVE';
22
export const STORIES_FETCH = 'STORIES_FETCH';
3+
export const STORIES_FETCH_ERROR = 'STORIES_FETCH_ERROR';
34
export const STORIES_ADD = 'STORIES_ADD';

src/reducers/story.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
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

821
function 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
}

src/sagas/story.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { call, put } from 'redux-saga/effects';
2-
import { doAddStories } from '../actions/story';
2+
import { doAddStories, doFetchErrorStories } from '../actions/story';
33
import { fetchStories } from '../api/story';
44

55
function* 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

1116
export {

src/selectors/story.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ const isNotArchived = archivedIds => story =>
22
archivedIds.indexOf(story.objectID) === -1;
33

44
const getReadableStories = ({ storyState, archiveState }) =>
5-
storyState.filter(isNotArchived(archiveState));
5+
storyState.stories.filter(isNotArchived(archiveState));
6+
7+
const getFetchError = ({ storyState }) =>
8+
storyState.error;
69

710
export {
811
getReadableStories,
12+
getFetchError,
913
};

0 commit comments

Comments
 (0)