import { fromJS } from 'immutable' ; import { Action , Actor , IMap } from 'plume2' ; import { Command } from '../command' ; let uuid = 0 ; export default class TodoActor extends Actor { defaultState ( ) { return { todo : [ ] } ; } @ Action ( Command . SUMBIT_TEXT ) add ( state , text ) { return state . update ( 'todo' , todo => todo . push ( fromJS ( { id : ++ uuid , text , done : false } ) ) ) ; } @ Action ( Command . DESTROY ) destroy ( state , index ) { return state . deleteIn ( [ 'todo' , index ] ) ; } @ Action ( Command . TOGGLE ) toggle ( state , index ) { return state . updateIn ( [ 'todo' , index , 'done' ] , done => ! done ) ; } @ Action ( Command . TOGGLE_ALL ) toggleAll ( state , checked ) { return state . update ( 'todo' , todo => todo . map ( item => item . set ( 'done' , checked ) ) ) ; } @ Action ( Command . CLEAN_COMPLETED ) clear ( state ) { return state . update ( 'todo' , todo => todo . filter ( item => ! item . get ( 'done' ) ) ) ; } @ Action ( Command . INIT ) init ( state , { todo } ) { return state . set ( 'todo' , fromJS ( todo ) ) ; } }