mirror of
				https://github.com/reduxjs/redux-devtools.git
				synced 2025-11-04 01:47:25 +03:00 
			
		
		
		
	
		
			
				
	
	
		
			51 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			51 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
import { ADD_TODO, DELETE_TODO, EDIT_TODO, MARK_TODO, MARK_ALL, CLEAR_MARKED } from '../constants/ActionTypes';
 | 
						|
 | 
						|
const initialState = [{
 | 
						|
  text: 'Use Redux',
 | 
						|
  marked: false,
 | 
						|
  id: 0
 | 
						|
}];
 | 
						|
 | 
						|
export default function todos(state = initialState, action) {
 | 
						|
  switch (action.type) {
 | 
						|
  case ADD_TODO:
 | 
						|
    return [{
 | 
						|
      id: (state.length === 0) ? 0 : state[0].id + 1,
 | 
						|
      marked: false,
 | 
						|
      text: action.text
 | 
						|
    }, ...state];
 | 
						|
 | 
						|
  case DELETE_TODO:
 | 
						|
    return state.filter(todo =>
 | 
						|
      todo.id !== action.id
 | 
						|
    );
 | 
						|
 | 
						|
  case EDIT_TODO:
 | 
						|
    return state.map(todo =>
 | 
						|
      todo.id === action.id ?
 | 
						|
        { ...todo, text: action.text } :
 | 
						|
        todo
 | 
						|
    );
 | 
						|
 | 
						|
  case MARK_TODO:
 | 
						|
    return state.map(todo =>
 | 
						|
      todo.id === action.id ?
 | 
						|
        { ...todo, marked: !todo.marked } :
 | 
						|
        todo
 | 
						|
    );
 | 
						|
 | 
						|
  case MARK_ALL:
 | 
						|
    const areAllMarked = state.every(todo => todo.marked);
 | 
						|
    return state.map(todo => ({
 | 
						|
      ...todo,
 | 
						|
      marked: !areAllMarked
 | 
						|
    }));
 | 
						|
 | 
						|
  case CLEAR_MARKED:
 | 
						|
    return state.filter(todo => todo.marked === false);
 | 
						|
 | 
						|
  default:
 | 
						|
    return state;
 | 
						|
  }
 | 
						|
}
 |