import { List } from 'immutable' ; import * as TodoService from "../TodoService" ; import { TodoModel } from "../TodoModel" ; export interface ITodoAction { type : string ; todoModel : TodoModel ; modelId ? : string ; key ? : string ; value : any ; } export function todos ( state = List < TodoModel > ( ) , action ) { function indexOf ( id ) { return state . findIndex ( ( i ) => i . getModelId ( ) === id ) ; } switch ( action . type ) { case TodoService . ADD_TODO : return state . push ( action . todoModel ) ; case TodoService . REMOVE_TODO : return List < TodoModel > ( state . filter ( ( i ) => i . getKey ( 'modelId' ) !== action . modelId ) ) ; case TodoService . CLEAR_TODOS : return List < TodoModel > ( ) ; case TodoService . EDIT_TODO : return state . update ( indexOf ( action . modelId ) , ( todoModel ) => { return todoModel . setKey < TodoModel > ( TodoModel , action . key , action . value ) ; } ) ; default : return state ; } }