Skip to content

Commit

Permalink
add actionCreators, middleware, thunk
Browse files Browse the repository at this point in the history
  • Loading branch information
SergiiNikolayev committed Jul 3, 2018
1 parent 0ba6a21 commit 5e1e2b9
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 26 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"react-dom": "^16.4.1",
"react-redux": "^5.0.7",
"react-scripts": "1.1.4",
"redux": "^4.0.0"
"redux": "^4.0.0",
"redux-thunk": "^2.3.0"
},
"scripts": {
"start": "react-scripts start",
Expand Down
18 changes: 5 additions & 13 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import Input from './components/Input'
import Output from './components/Output'
import {getFilteredItems} from './store/selects'
import './index.css'
import {VisibilityFilters} from "./store/actions";
import {VisibilityFilters} from './store/actions';
import * as actionCreators from './store/actions';

class App extends Component {
constructor(props) {
Expand Down Expand Up @@ -65,22 +66,13 @@ export default connect(
}),
mapDispatchToProps => ({
onItemAdd: (item) => {
mapDispatchToProps({
type: 'ADD', payload: {
id: Date.now(),
name: item
}
})
mapDispatchToProps(actionCreators.add({id: Date.now(), name: item}));
},
onItemDone: (id) => {
mapDispatchToProps({
type: 'MARK_DONE', id
})
mapDispatchToProps(actionCreators.markDone(id));
},
changeFilter: (filter) => {
mapDispatchToProps({
type: 'CHANGE_FILTER', filter
})
mapDispatchToProps(actionCreators.changeFilter(filter))
}
})
)(App);
20 changes: 16 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from 'react-redux';
import {createStore} from 'redux';
import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk'

import './index.css';
import App from './App';
import rootReducer from './store/rootReducer'

const store = createStore(rootReducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
const devTools = window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__();

const logger = store => {
return next => {
return action => {
console.log('[Middleware] Dispatching', action);
const result = next(action);
console.log('[Middleware] next state', store.getState());
return result;
}
}
};

const store = createStore(rootReducer, devTools, applyMiddleware(logger, thunk));

store.subscribe(() => {
console.log('[subscribe]', store.getState());
Expand Down
16 changes: 10 additions & 6 deletions src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,25 @@ export const VisibilityFilters = {

//генераторы действий, action creators

export const add = () => {
export const add = ({id, name}) => {
return {
type: ADD
type: ADD,
id: id,
name: name
}
};

export const markDone = () => {
export const markDone = (getId) => {
return {
type: MARK_DONE
type: MARK_DONE,
id: getId
}
};

export const changeFilter = () => {
export const changeFilter = (filter) => {
return {
type: CHANGE_FILTER
type: CHANGE_FILTER,
filter: filter
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/store/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const reducerTodo = (state = initialState, action) => {
{
isDone: false,
/* currentFilter: VisibilityFilters.SORT_BLACK,*/
name: action.payload.name,
id: action.payload.id
name: action.name,
id: action.id
}
])
};
Expand Down

0 comments on commit 5e1e2b9

Please sign in to comment.