Skip to content

Commit cff06e5

Browse files
committed
part 15
1 parent 8bf447b commit cff06e5

File tree

10 files changed

+59
-24
lines changed

10 files changed

+59
-24
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
"dependencies": {
99
"react": "^15.5.4",
1010
"react-dom": "^15.5.4",
11-
"redux": "^3.6.0"
11+
"react-redux": "^5.0.5",
12+
"redux": "^3.6.0",
13+
"redux-logger": "^3.0.6"
1214
},
1315
"scripts": {
1416
"start": "react-scripts start",

src/actions/archive.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { STORY_ARCHIVE } from '../constants/actionTypes';
2+
3+
const doArchiveStory = id => ({
4+
type: STORY_ARCHIVE,
5+
id,
6+
});
7+
8+
export {
9+
doArchiveStory,
10+
};

src/actions/index.js

Whitespace-only changes.

src/components/App.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ class App extends Component {
77
render() {
88
return (
99
<div className="app">
10-
<Stories
11-
stories={this.props.stories}
12-
onArchive={this.props.onArchive}
13-
/>
10+
<Stories />
1411
</div>
1512
);
1613
}

src/components/Stories.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import React, { Component } from 'react';
2+
import { connect } from 'react-redux';
3+
import { getReadableStories } from '../selectors/story';
24
import './Stories.css';
35

46
import Story from './Story';
@@ -27,10 +29,7 @@ const COLUMNS = {
2729

2830
class Stories extends Component {
2931
render() {
30-
const {
31-
stories,
32-
onArchive,
33-
} = this.props;
32+
const { stories } = this.props;
3433

3534
return (
3635
<div className="stories">
@@ -41,7 +40,6 @@ class Stories extends Component {
4140
key={story.objectID}
4241
story={story}
4342
columns={COLUMNS}
44-
onArchive={onArchive}
4543
/>
4644
)}
4745
</div>
@@ -61,4 +59,10 @@ const StoriesHeader = ({ columns }) =>
6159
)}
6260
</div>
6361

64-
export default Stories;
62+
const mapStateToProps = state => ({
63+
stories: getReadableStories(state),
64+
});
65+
66+
export default connect(
67+
mapStateToProps
68+
)(Stories);

src/components/Story.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import React, { Component } from 'react';
2+
import { connect } from 'react-redux';
3+
import { doArchiveStory } from '../actions/archive';
24
import './Story.css';
35

46
import { ButtonInline } from './Buttons';
@@ -44,4 +46,11 @@ class Story extends Component {
4446
}
4547
}
4648

47-
export default Story;
49+
const mapDispatchToProps = dispatch => ({
50+
onArchive: id => dispatch(doArchiveStory(id)),
51+
});
52+
53+
export default connect(
54+
null,
55+
mapDispatchToProps
56+
)(Story);

src/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
3+
import { Provider } from 'react-redux';
34
import App from './components/App';
45
import store from './store';
5-
import { STORY_ARCHIVE } from './constants/actionTypes';
66
import registerServiceWorker from './registerServiceWorker';
77
import './index.css';
88

99
ReactDOM.render(
10-
<App
11-
stories={store.getState().storyState}
12-
onArchive={id => store.dispatch({ type: STORY_ARCHIVE, id })}
13-
/>,
10+
<Provider store={store}>
11+
<App />
12+
</Provider>,
1413
document.getElementById('root')
1514
);
15+
1616
registerServiceWorker();

src/reducers/archive.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { STORY_ARCHIVE } from '../constants/actionTypes';
22

33
const INITIAL_STATE = [];
44

5+
const applyArchiveStory = (state, action) =>
6+
[ ...state, action.id ];
7+
58
function archiveReducer(state = INITIAL_STATE, action) {
69
switch(action.type) {
710
case STORY_ARCHIVE : {
@@ -11,8 +14,4 @@ function archiveReducer(state = INITIAL_STATE, action) {
1114
}
1215
}
1316

14-
function applyArchiveStory(state, action) {
15-
return [ ...state, action.id ];
16-
}
17-
1817
export default archiveReducer;

src/selectors/story.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const isNotArchived = archivedIds => story =>
2+
archivedIds.indexOf(story.objectID) === -1;
3+
4+
const getReadableStories = ({ storyState, archiveState }) =>
5+
storyState.filter(isNotArchived(archiveState));
6+
7+
export {
8+
getReadableStories,
9+
};

src/store/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import { createStore } from 'redux';
1+
import { createStore, applyMiddleware } from 'redux';
2+
import { createLogger } from 'redux-logger';
23
import rootReducer from '../reducers';
34

5+
const logger = createLogger();
6+
47
const store = createStore(
5-
rootReducer
8+
rootReducer,
9+
undefined,
10+
applyMiddleware(logger)
611
);
712

8-
export default store;
13+
export default store;

0 commit comments

Comments
 (0)