Skip to content

Commit dc3f4f5

Browse files
committed
create todo
1 parent 3523734 commit dc3f4f5

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"react-dom": "^15.5.4",
1212
"react-redux": "^5.0.5",
1313
"redux": "^3.6.0",
14-
"redux-logger": "^3.0.6"
14+
"redux-logger": "^3.0.6",
15+
"uuid": "^3.0.1"
1516
},
1617
"scripts": {
1718
"start": "react-scripts start",

src/index.js

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { applyMiddleware, combineReducers, createStore } from 'redux';
44
import { Provider, connect } from 'react-redux';
55
import { createLogger } from 'redux-logger';
66
import { schema, normalize } from 'normalizr';
7+
import uuid from 'uuid/v4';
78
import './index.css';
89

910
// schemas
@@ -19,8 +20,8 @@ const FILTER_SET = 'FILTER_SET';
1920
// reducers
2021

2122
const todos = [
22-
{ id: '0', name: 'learn redux' },
23-
{ id: '1', name: 'learn mobx' },
23+
{ id: uuid(), name: 'learn redux' },
24+
{ id: uuid(), name: 'learn mobx' },
2425
];
2526

2627
const normalizedTodos = normalize(todos, [todoSchema]);
@@ -121,7 +122,51 @@ const store = createStore(
121122
// components
122123

123124
function TodoApp() {
124-
return <ConnectedTodoList />;
125+
return (
126+
<div>
127+
<ConnectedTodoCreate />
128+
<ConnectedTodoList />
129+
</div>
130+
);
131+
}
132+
133+
class TodoCreate extends React.Component {
134+
constructor(props) {
135+
super(props);
136+
137+
this.state = {
138+
value: '',
139+
};
140+
141+
this.onCreateTodo = this.onCreateTodo.bind(this);
142+
this.onChangeName = this.onChangeName.bind(this);
143+
}
144+
145+
onChangeName(event) {
146+
this.setState({ value: event.target.value });
147+
}
148+
149+
onCreateTodo(event) {
150+
this.props.onAddTodo(this.state.value);
151+
this.setState({ value: '' });
152+
event.preventDefault();
153+
}
154+
155+
render() {
156+
return (
157+
<div>
158+
<form onSubmit={this.onCreateTodo}>
159+
<input
160+
type="text"
161+
placeholder="Add Todo..."
162+
value={this.state.value}
163+
onChange={this.onChangeName}
164+
/>
165+
<button type="submit">Add</button>
166+
</form>
167+
</div>
168+
);
169+
}
125170
}
126171

127172
function TodoList({ todosAsIds }) {
@@ -170,8 +215,15 @@ function mapDispatchToPropsItem(dispatch) {
170215
};
171216
}
172217

218+
function mapDispatchToPropsCreate(dispatch) {
219+
return {
220+
onAddTodo: name => dispatch(doAddTodo(uuid(), name)),
221+
};
222+
}
223+
173224
const ConnectedTodoList = connect(mapStateToPropsList)(TodoList);
174225
const ConnectedTodoItem = connect(mapStateToPropsItem, mapDispatchToPropsItem)(TodoItem);
226+
const ConnectedTodoCreate = connect(null, mapDispatchToPropsCreate)(TodoCreate);
175227

176228
ReactDOM.render(
177229
<Provider store={store}>

0 commit comments

Comments
 (0)