import immer from 'immer' ; import { Modeler } from 'redux-aggregate' ; import { TodoModel , TodoST } from './todo' ; export interface TodosST { name : string ; input : string | null ; items : TodoST [ ] ; now : string ; bgColor : string ; } export const TodosModel = injects => ( { name : '' , input : null , items : [ ] , now : '' , bgColor : '#fff' , ... injects } ) ; function getInputValue ( state ) { if ( state . input === null ) return '' ; return `${ state . input }` ; } export const TodosQR = { getInputValue } ; const addTodo = ( state ) : TodosST => immer ( state , _state => { const value = TodosQR . getInputValue ( _state ) ; if ( value === '' ) return ; _state . items . push ( TodoModel ( { value , date : new Date ( ) } ) ) ; _state . input = '' ; } ) ; const setInputValue = ( state , value ) : TodosST => immer ( state , _state => { _state . input = value ; } ) ; const setItemDone = ( state , { id , done } ) : TodosST => immer ( state , _state => { const item = _state . items . find ( item => item . id === id ) ; if ( item === undefined ) return ; item . done = done ; } ) ; export const TodosMT = { addTodo , setInputValue , setItemDone } ; const Timer = { tick ( state , timeLabel ) { return { ... state , now : timeLabel } ; } } ; export const TodosSB = { Timer } ;