2019-01-10 21:51:14 +03:00
|
|
|
import {
|
|
|
|
ADD_TODO,
|
|
|
|
DELETE_TODO,
|
|
|
|
EDIT_TODO,
|
|
|
|
MARK_TODO,
|
|
|
|
MARK_ALL,
|
2020-08-08 23:26:39 +03:00
|
|
|
CLEAR_MARKED,
|
2019-01-10 21:51:14 +03:00
|
|
|
} from '../constants/ActionTypes';
|
2018-12-22 17:20:04 +03:00
|
|
|
|
2019-01-10 21:51:14 +03:00
|
|
|
const initialState = [
|
|
|
|
{
|
|
|
|
text: 'Use Redux',
|
|
|
|
marked: false,
|
2020-08-08 23:26:39 +03:00
|
|
|
id: 0,
|
|
|
|
},
|
2019-01-10 21:51:14 +03:00
|
|
|
];
|
2018-12-22 17:20:04 +03:00
|
|
|
|
|
|
|
export default function todos(state = initialState, action) {
|
|
|
|
switch (action.type) {
|
|
|
|
case ADD_TODO:
|
2019-01-10 21:51:14 +03:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
id: state.length === 0 ? 0 : state[0].id + 1,
|
|
|
|
marked: false,
|
2020-08-08 23:26:39 +03:00
|
|
|
text: action.text,
|
2019-01-10 21:51:14 +03:00
|
|
|
},
|
2020-08-08 23:26:39 +03:00
|
|
|
...state,
|
2019-01-10 21:51:14 +03:00
|
|
|
];
|
2018-12-22 17:20:04 +03:00
|
|
|
|
|
|
|
case DELETE_TODO:
|
2020-08-08 23:26:39 +03:00
|
|
|
return state.filter((todo) => todo.id !== action.id);
|
2018-12-22 17:20:04 +03:00
|
|
|
|
|
|
|
case EDIT_TODO:
|
2020-08-08 23:26:39 +03:00
|
|
|
return state.map((todo) =>
|
2019-01-10 21:51:14 +03:00
|
|
|
todo.id === action.id ? { ...todo, text: action.text } : todo
|
|
|
|
);
|
2018-12-22 17:20:04 +03:00
|
|
|
|
|
|
|
case MARK_TODO:
|
2020-08-08 23:26:39 +03:00
|
|
|
return state.map((todo) =>
|
2019-01-10 21:51:14 +03:00
|
|
|
todo.id === action.id ? { ...todo, marked: !todo.marked } : todo
|
|
|
|
);
|
2018-12-22 17:20:04 +03:00
|
|
|
|
|
|
|
case MARK_ALL: {
|
2020-08-08 23:26:39 +03:00
|
|
|
const areAllMarked = state.every((todo) => todo.marked);
|
|
|
|
return state.map((todo) => ({
|
2018-12-22 17:20:04 +03:00
|
|
|
...todo,
|
2020-08-08 23:26:39 +03:00
|
|
|
marked: !areAllMarked,
|
2018-12-22 17:20:04 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
case CLEAR_MARKED:
|
2020-08-08 23:26:39 +03:00
|
|
|
return state.filter((todo) => todo.marked === false);
|
2018-12-22 17:20:04 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|