import { List } from 'immutable' ; import { TodoItem } from './todoitem' ; export interface ITodoAction { type : string ; text ? : string ; itemId ? : string ; completed ? : boolean ; } export function reducer ( state = List < TodoItem > ( ) , action ) { function indexOf ( uuid ) { return state . findIndex ( ( i ) => i . uuid === action . itemId ) ; } switch ( action . type ) { case 'ADD' : return state . push ( new TodoItem ( ) . setText ( action . text ) ) ; case 'REMOVE' : return List < TodoItem > ( state . filter ( ( i ) => i . uuid !== action . itemId ) ) ; case 'UPDATE_ITEM_TEXT' : return state . update ( indexOf ( action . itemId ) , ( i ) => i . setText ( action . text ) ) ; case 'UPDATE_ITEM_COMPLETION' : return state . update ( indexOf ( action . itemId ) , ( i ) => i . setCompleted ( action . completed ) ) ; default : return state ; } }