Skip to content

Commit

Permalink
add input control
Browse files Browse the repository at this point in the history
  • Loading branch information
Chenyi LEE committed Dec 3, 2016
1 parent 1fe56e6 commit dd0c760
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/routes/Todo/components/Todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export class Todo extends React.Component {
{
props.isAuth ? <TodoView data={props.todos} onChange={props.handleToggleTodo}/> : <TodoAuth doAuth={props.doAuth}/>
}
<form>
<input type="text" value={props.inputTodo} onChange={props.handleInputText} />
<input type="button" value="追加" onClick={props.addTodo}/>
</form>
</div>
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/routes/Todo/containers/TodoContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
didMount,
doAuth,
handleToggleTodo,
handleInputText,
addTodo,
} from '../modules/todo'

import Todo from '../components/Todo'
Expand All @@ -11,11 +13,14 @@ const mapDispatchToProps = {
didMount,
doAuth,
handleToggleTodo,
handleInputText,
addTodo,
}

const mapStateToProps = (state) => ({
isAuth: state.todo.isAuth,
todos: state.todo.todos,
inputTodo: state.todo.inputTodo,
})

export default connect(mapStateToProps, mapDispatchToProps)(Todo)
26 changes: 26 additions & 0 deletions src/routes/Todo/modules/todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ export function handleToggleTodo(i) {
}
}

export function handleInputText(e) {
return (dispatch, getState) => {
dispatch({
type: "TASK_INPUTED",
payload: e.target.value,
})
}
}

export function addTodo(e) {
e.preventDefault()
return (dispatch, getState) => {
console.log(getState())
firebase.database().ref("todo").push({
title: getState().todo.inputTodo,
is_done: false,
})
}
}

// ------------------------------------
// Action Handlers
// ------------------------------------
Expand Down Expand Up @@ -87,6 +107,11 @@ const ACTION_HANDLERS = {
todos: todos
})
},
["TASK_INPUTED"]: (state, action) => {
return Object.assign({}, state, {
inputTodo: action.payload
})
},
}

// ------------------------------------
Expand All @@ -95,6 +120,7 @@ const ACTION_HANDLERS = {
const initialState = {
isAuth: false,
todos: [],
inputTodo: "",
}
export default function todoReducer (state = initialState, action) {
const handler = ACTION_HANDLERS[action.type]
Expand Down

0 comments on commit dd0c760

Please sign in to comment.