import React, { Component } from 'react'; import './App.css'; import { connect, Provider } from 'react-redux'; import { createStore, combineReducers } from 'redux'; function todoReducer(state, action){ if (typeof state === 'undefined'){ return {items: JSON.parse(localStorage.todo || "[]")}; } if (action.type === 'NEW_TODO'){ console.log(state); return {items: [...state.items, {title: action.title, text: action.text, timestamp: (new Date()).getTime()}]} } if (action.type === 'DELETE_TODO'){ let items = []; for (let i=0;i { let state = store.getState(); localStorage.todo = JSON.stringify(state.todo.items); }) class ToDoItem extends Component { constructor(props){ super(props); this.delete = this.delete.bind(this); } delete() { store.dispatch({type: "DELETE_TODO", timestamp: this.props.item.timestamp}) } render() { return (

{ this.props.item.title }

{ this.props.item.text }

{ (new Date(this.props.item.timestamp)).toLocaleString() } {this.props.item.timestamp}
); } } class ToDoForm extends Component { constructor (props) { super(props); this.save = this.save.bind(this); this.state = {valid: true}; } validator(){ return this.title.value && this.text.value; } save() { let valid = this.validator(); this.setState({valid}) if (valid){ store.dispatch({type: 'NEW_TODO', title: this.title.value, text: this.text.value}) this.title.value = ''; this.text.value = ''; } } render (){ let style = { backgroundColor: this.state.valid ? '' : 'red' } return (
this.title = c}/> this.text = c} />
); } } class ToDoList extends Component { render (){ return (

TODO LIST

{ this.props.items.map( (item, index) => ) }
); } } const mapStateToProps = function(store){ return { items: store.todo.items }; } ToDoList = connect(mapStateToProps)(ToDoList); class App extends Component { render() { return (
); } } export default App;