Skip to content

Instantly share code, notes, and snippets.

@laere
Last active February 15, 2016 02:33
Show Gist options
  • Save laere/7177445c0162624dec93 to your computer and use it in GitHub Desktop.
Save laere/7177445c0162624dec93 to your computer and use it in GitHub Desktop.

Revisions

  1. laere revised this gist Feb 15, 2016. 3 changed files with 23 additions and 10 deletions.
    13 changes: 7 additions & 6 deletions 1Reducer.jsx
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,28 @@

    import dotProp from 'dot-prop-immutable';
    //save string values to vars
    const ADD_TODO = 'ADD_TODO';

    let nextId = 0;

    //the action is whats performed to alter state
    //addItem is an action creator and nees to return an action
    export const AddTodo = (text) => {
    return {
    type: ADD_TODO,
    id: nextId++,
    text
    };
    }

    //initial state of items and text
    const initialState = {
    items: [],
    text: ''
    }

    //A pure function that takes the current/prev state
    //and an action, and returns the next state
    //Reducer controls the state, and is where state lives.
    export const ToDoState = (state = initialState, action) => {
    switch(action.type) {
    case ADD_TODO:
    state = dotProp.set(state, 'items', items => [...items, { text: action.text, id: action.id }]);
    state = dotProp.set(state, 'items', items => [...items, {text: action.text, id: action.id }])
    console.log(state, action);
    return state;
    default:
    5 changes: 2 additions & 3 deletions 3App.jsx
    Original file line number Diff line number Diff line change
    @@ -13,8 +13,7 @@ class App extends Component {
    }

    handleSubmit(e) {
    e.preventDefault();
    this.props.AddTodo(this.refs.inputfield.value);
    this.props.AddTodo();
    }

    render() {
    @@ -23,7 +22,7 @@ class App extends Component {
    <h1>To Do App</h1>
    <List
    items={this.props.items}
    handleOnClick={this.handleSubmit}
    handleSubmit={this.handleSubmit}
    />
    </div>
    )
    15 changes: 14 additions & 1 deletion 4List.jsx
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,19 @@ import ListItem from '../components/ListItem';

    export default class List extends Component {

    constructor(props) {
    super(props);

    this.handleOnClick = this.handleOnClick.bind(this);
    }

    handleOnClick(e) {
    e.preventDefault();
    let inputValue = this.refs.inputfield.value;
    this.props.handleSubmit(inputValue);
    console.log(inputValue);
    }

    renderTodos() {
    return this.props.items.map((item) => {
    return (
    @@ -14,7 +27,7 @@ export default class List extends Component {
    render() {
    return (
    <div>
    <form onSubmit={this.props.handleSubmit}>
    <form onSubmit={this.handleOnClick}>
    <input type="text" ref="inputfield"/>
    <input type="submit" value="Add" />
    </form>
  2. laere revised this gist Feb 15, 2016. 2 changed files with 5 additions and 11 deletions.
    7 changes: 2 additions & 5 deletions 1Reducer.jsx
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,7 @@
    const ADD_TODO = 'ADD_TODO';

    let nextId = 0;
    //the action is whats performed to alter state
    //addItem is an action creator and nees to return an action

    export const AddTodo = (text) => {
    return {
    type: ADD_TODO,
    @@ -18,9 +17,7 @@ const initialState = {
    items: [],
    text: ''
    }
    //A pure function that takes the current/prev state
    //and an action, and returns the next state
    //Reducer controls the state, and is where state lives.

    export const ToDoState = (state = initialState, action) => {
    switch(action.type) {
    case ADD_TODO:
    9 changes: 3 additions & 6 deletions 3App.jsx
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,20 @@
    import React, { Component } from 'react';
    //import actions
    import { AddTodo } from '../reducers/reducer_todos';
    // import { RemoveToDo } from '../reducers/reducer_todos';
    //connect to store
    import { connect } from 'react-redux';
    import { bindActionCreators } from 'redux';
    //import List component
    import List from '../containers/List';

    class App extends Component {

    constructor(props) {
    super(props);

    this.handleSubmit = this.handleSubmit.bind(this);
    }

    handleSubmit(e) {
    e.preventDefault();
    this.props.AddTodo(this.refs.inputfield.value);
    this.refs.inputfield.value = ''
    }

    render() {
    @@ -26,7 +23,7 @@ class App extends Component {
    <h1>To Do App</h1>
    <List
    items={this.props.items}
    handleSubmit={this.props.handleSubmit}
    handleOnClick={this.handleSubmit}
    />
    </div>
    )
  3. laere revised this gist Feb 15, 2016. No changes.
  4. laere revised this gist Feb 15, 2016. 4 changed files with 16 additions and 37 deletions.
    20 changes: 7 additions & 13 deletions 1Reducer.jsx
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,10 @@

    //save string values to vars
    const ADD_TODO = 'ADD_TODO';
    const UPDATE_TEXT = 'UPDATE_TEXT';

    let nextId = 0;

    //the action is whats performed to alter state
    //addItem is an action creator and nees to return an action
    export const AddTodo = (text) => {
    return {
    type: ADD_TODO,
    @@ -13,25 +13,19 @@ export const AddTodo = (text) => {
    };
    }

    export const UpdateText = (text) => {
    return {
    type: UPDATE_TEXT,
    text
    };
    }

    //initial state of items and text
    const initialState = {
    items: [],
    text: ''
    }

    //A pure function that takes the current/prev state
    //and an action, and returns the next state
    //Reducer controls the state, and is where state lives.
    export const ToDoState = (state = initialState, action) => {
    switch(action.type) {
    case ADD_TODO:
    state = dotProp.set(state, 'items', items => [...items, { text: action.text, id: action.id }]);
    return state;
    case UPDATE_TEXT:
    state.UpdateText = action.text;
    console.log(state, action);
    return state;
    default:
    return state;
    25 changes: 5 additions & 20 deletions 3App.jsx
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,6 @@
    import React, { Component } from 'react';
    //import actions
    import { UpdateText } from '../reducers/reducer_todos';
    import { AddTodo } from '../reducers/reducer_todos';


    // import { RemoveToDo } from '../reducers/reducer_todos';
    //connect to store
    import { connect } from 'react-redux';
    @@ -17,20 +14,10 @@ class App extends Component {
    super(props);
    }

    handleOnChange(e) {
    this.props.UpdateText(e.target.value);
    console.log(e.target.value);
    }

    handleOnClick(e) {
    handleSubmit(e) {
    e.preventDefault();
    this.props.AddTodo(this.getInputText());
    console.log('test click');
    }

    getInputText() {
    return this.refs.inputfield.value;
    console.log(this.refs.inputfield.value);
    this.props.AddTodo(this.refs.inputfield.value);
    this.refs.inputfield.value = ''
    }

    render() {
    @@ -39,8 +26,7 @@ class App extends Component {
    <h1>To Do App</h1>
    <List
    items={this.props.items}
    handleOnClick={this.props.handleOnClick}
    handleOnChange={this.props.handleOnChange}
    handleSubmit={this.props.handleSubmit}
    />
    </div>
    )
    @@ -50,8 +36,7 @@ class App extends Component {
    let mapDispatchToProps = (dispatch) => {
    return bindActionCreators(
    {
    AddTodo: AddTodo,
    UpdateText: UpdateText
    AddTodo: AddTodo
    }, dispatch);
    }

    6 changes: 3 additions & 3 deletions 4List.jsx
    Original file line number Diff line number Diff line change
    @@ -14,9 +14,9 @@ export default class List extends Component {
    render() {
    return (
    <div>
    <form>
    <input onChange={this.props.handleOnChange} ref="inputfield"/>
    <button onClick={this.props.handleOnClick} >Add</button>
    <form onSubmit={this.props.handleSubmit}>
    <input type="text" ref="inputfield"/>
    <input type="submit" value="Add" />
    </form>
    <ul>
    {this.renderTodos()}
    2 changes: 1 addition & 1 deletion 5ListItem.jsx
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ export default class ListItem extends Component {
    render() {
    return (
    <li>
    <h4>{this.props.text}</h4>
    {this.props.text}
    </li>
    );
    }
  5. laere revised this gist Feb 14, 2016. 2 changed files with 4 additions and 5 deletions.
    2 changes: 1 addition & 1 deletion 1Reducer.jsx
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ const UPDATE_TEXT = 'UPDATE_TEXT';

    let nextId = 0;

    export const AddToDo = (text) => {
    export const AddTodo = (text) => {
    return {
    type: ADD_TODO,
    id: nextId++,
    7 changes: 3 additions & 4 deletions 3App.jsx
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,8 @@
    import React, { Component } from 'react';
    //import actions
    import { UpdateText } from '../reducers/reducer_todos';
    import { AddToDo } from '../reducers/reducer_todos';
    console.log(AddToDo);
    console.log(UpdateText);
    import { AddTodo } from '../reducers/reducer_todos';


    // import { RemoveToDo } from '../reducers/reducer_todos';
    //connect to store
    @@ -51,7 +50,7 @@ class App extends Component {
    let mapDispatchToProps = (dispatch) => {
    return bindActionCreators(
    {
    AddToDo: AddToDo,
    AddTodo: AddTodo,
    UpdateText: UpdateText
    }, dispatch);
    }
  6. laere revised this gist Feb 14, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion 1Reducer.jsx
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,6 @@
    //save string values to vars
    const ADD_TODO = 'ADD_TODO';
    const UPDATE_TEXT = 'UPDATE_TEXT';
    const REMOVE_TODO = 'REMOVE_TODO';

    let nextId = 0;

  7. laere revised this gist Feb 14, 2016. 5 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  8. laere created this gist Feb 14, 2016.
    66 changes: 66 additions & 0 deletions App.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    import React, { Component } from 'react';
    //import actions
    import { UpdateText } from '../reducers/reducer_todos';
    import { AddToDo } from '../reducers/reducer_todos';
    console.log(AddToDo);
    console.log(UpdateText);

    // import { RemoveToDo } from '../reducers/reducer_todos';
    //connect to store
    import { connect } from 'react-redux';
    import { bindActionCreators } from 'redux';
    //import List component
    import List from '../containers/List';

    class App extends Component {

    constructor(props) {
    super(props);
    }

    handleOnChange(e) {
    this.props.UpdateText(e.target.value);
    console.log(e.target.value);
    }

    handleOnClick(e) {
    e.preventDefault();
    this.props.AddTodo(this.getInputText());
    console.log('test click');
    }

    getInputText() {
    return this.refs.inputfield.value;
    console.log(this.refs.inputfield.value);
    }

    render() {
    return (
    <div>
    <h1>To Do App</h1>
    <List
    items={this.props.items}
    handleOnClick={this.props.handleOnClick}
    handleOnChange={this.props.handleOnChange}
    />
    </div>
    )
    }
    }

    let mapDispatchToProps = (dispatch) => {
    return bindActionCreators(
    {
    AddToDo: AddToDo,
    UpdateText: UpdateText
    }, dispatch);
    }

    let mapStateToProps = (state) => {
    return {
    items: state.items,
    text: state.text
    }
    }

    export default connect(mapStateToProps, mapDispatchToProps)(App);
    27 changes: 27 additions & 0 deletions List.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    import React, { Component } from 'react';
    import ListItem from '../components/ListItem';

    export default class List extends Component {

    renderTodos() {
    return this.props.items.map((item) => {
    return (
    <ListItem key={item.id} text={item.text} />
    );
    });
    }

    render() {
    return (
    <div>
    <form>
    <input onChange={this.props.handleOnChange} ref="inputfield"/>
    <button onClick={this.props.handleOnClick} >Add</button>
    </form>
    <ul>
    {this.renderTodos()}
    </ul>
    </div>
    );
    }
    }
    11 changes: 11 additions & 0 deletions ListItem.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    import React, { Component } from 'react';

    export default class ListItem extends Component {
    render() {
    return (
    <li>
    <h4>{this.props.text}</h4>
    </li>
    );
    }
    }
    40 changes: 40 additions & 0 deletions Reducer.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@

    //save string values to vars
    const ADD_TODO = 'ADD_TODO';
    const UPDATE_TEXT = 'UPDATE_TEXT';
    const REMOVE_TODO = 'REMOVE_TODO';

    let nextId = 0;

    export const AddToDo = (text) => {
    return {
    type: ADD_TODO,
    id: nextId++,
    text
    };
    }

    export const UpdateText = (text) => {
    return {
    type: UPDATE_TEXT,
    text
    };
    }

    const initialState = {
    items: [],
    text: ''
    }

    export const ToDoState = (state = initialState, action) => {
    switch(action.type) {
    case ADD_TODO:
    state = dotProp.set(state, 'items', items => [...items, { text: action.text, id: action.id }]);
    return state;
    case UPDATE_TEXT:
    state.UpdateText = action.text;
    return state;
    default:
    return state;
    }
    }
    18 changes: 18 additions & 0 deletions Root.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    import React from 'react';
    import ReactDOM from 'react-dom';
    import App from './components/app';
    import dotProp from 'dot-prop-immutable';

    import { Provider } from 'react-redux';
    import { createStore } from 'redux';
    import { ToDoState } from '../src/reducers/reducer_todos';

    let store = createStore(ToDoState);

    console.log(store);

    ReactDOM.render(
    <Provider store={store}>
    <App />
    </Provider>,
    document.querySelector('.container'));