Skip to content

Commit

Permalink
add change filter
Browse files Browse the repository at this point in the history
  • Loading branch information
SergiiNikolayev committed Jun 27, 2018
1 parent f6a4149 commit 4a96bd8
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 46 deletions.
21 changes: 13 additions & 8 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class App extends Component {
this.props.onItemDone(id);
};

sortDoneHandler = ( filter ) => {
/* sortDoneHandler = ( filter ) => {
console.log(filter);
this.props.onItemSortDone( filter );
};
Expand All @@ -39,7 +39,7 @@ class App extends Component {
sortAllHandler = (filter) => {
console.log(filter);
this.props.onItemSortAll(filter)
};
};*/

render() {
return (
Expand All @@ -52,9 +52,9 @@ class App extends Component {
/>
<Controls
userClick={this.itemAddHandler}
userClickFilterOne={e => this.sortDoneHandler(VisibilityFilters.SORT_RED)}
userClickFilterTwo={e => this.sortUnDoneHandler(VisibilityFilters.SORT_BLACK)}
userClickFilterThree={e => this.sortAllHandler(VisibilityFilters.SORT_ALL)}
userClickFilterOne={e => this.props.changeFilter(VisibilityFilters.SORT_RED)}
userClickFilterTwo={e => this.props.changeFilter(VisibilityFilters.SORT_BLACK)}
userClickFilterThree={e => this.props.changeFilter(VisibilityFilters.SORT_ALL)}
/>
<ol>
{
Expand All @@ -75,8 +75,8 @@ class App extends Component {

export default connect(
state => ({
todoItems: state.items,
//todoItems: getFilteredItems({ ...state }) //store
//odoItems: state.items,
todoItems: getFilteredItems(state) //store
}),
dispatch => ({
onItemAdd: (item) => {
Expand All @@ -92,7 +92,7 @@ export default connect(
type: 'MARK_DONE', id
})
},
onItemSortDone: ( filter ) => {
/* onItemSortDone: ( filter ) => {
dispatch({
type: filter
})
Expand All @@ -106,6 +106,11 @@ export default connect(
dispatch({
type: filter
})
},*/
changeFilter: (filter) => {
dispatch({
type: 'CHANGE_FILTER', filter
})
}
})
)(App);
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { createStore } from 'redux';

import './index.css';
import App from './App';
import myReducers from './store/myReducers'
import rootReducer from './store/rootReducer'
/*import reducerFilter from './store/reducerFilter'
import reducerTodo from './store/reducer'*/


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

Expand Down
2 changes: 1 addition & 1 deletion src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const MARK_DONE = 'MARK_DONE';
export const SORT_BLACKX = 'SORT_BLACKX';
export const SORT_ALLX = 'SORT_ALLX';*/

export const SET_VISIBILITY_FILTER = 'SET_VISIBILITY_FILTER';
export const CHANGE_FILTERS = 'CHANGE_FILTERS';

/*
* другие константы
Expand Down
19 changes: 19 additions & 0 deletions src/store/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {CHANGE_FILTERS, VisibilityFilters} from './actions';

const initialState = {
currentFilter: VisibilityFilters.SORT_ALL
}

const reducerFilter = (state = initialState, action) => {
switch (action.type) {
case CHANGE_FILTERS:
return {
...state,
currentFilter: action.filter
};
default:
return state;
}
};

export default reducerFilter;
8 changes: 4 additions & 4 deletions src/store/reducer.js → src/store/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as actionTypes from './actions';
import {VisibilityFilters} from './actions';

const initialState = {
items: [],
list: [],
};

const testState = {
Expand Down Expand Up @@ -39,10 +39,10 @@ const reducerTodo = (state = initialState, action) => {
case actionTypes.ADD:
return {
...state,
items: state.items.concat([
list: state.list.concat([
{
isDone: false,
currentFilter: VisibilityFilters.SORT_BLACK,
/*currentFilter: VisibilityFilters.SORT_BLACK,*/
name: action.payload.name,
id: action.payload.id
}
Expand All @@ -51,7 +51,7 @@ const reducerTodo = (state = initialState, action) => {

case actionTypes.MARK_DONE:
return {
items: state.items.map( element =>
list: state.list.map( element =>
(element.id === action.id )
? {...element, isDone: !element.isDone}
: { ...element})
Expand Down
8 changes: 0 additions & 8 deletions src/store/myReducers.js

This file was deleted.

16 changes: 0 additions & 16 deletions src/store/reducerFilter.js

This file was deleted.

8 changes: 8 additions & 0 deletions src/store/rootReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { combineReducers } from 'redux';
import items from './items';
import filter from './filter'

export default combineReducers({
items,
filter
})
12 changes: 5 additions & 7 deletions src/store/selects.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ export const getFilteredItems = (store) => {
return items.filter(item => item);
}*/

const currentFilter = store.currentFilter;// достаем текущий фильтр
const items = store.items;
const currentFilter = store.filter.currentFilter;// достаем текущий фильтр
const items = store.items.list;

// в зависимости от того какой фильтр, производим соответсвующие действия
if (currentFilter === VisibilityFilters.SORT_RED) {
if (currentFilter === 'SORT_RED') {
return items.filter(item => items.isDone);
} else if (currentFilter === VisibilityFilters.SORT_BLACK) {
} else if (currentFilter === 'SORT_BLACK') {
return items.filter(item => !items.isDone);
} else if (currentFilter === VisibilityFilters.SORT_ALL) {
return items.filter(item => item);
} else {
return items.filter(item => item);
return items;
}

};

0 comments on commit 4a96bd8

Please sign in to comment.