forked from huangtengfei/redux-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49c3426
commit aa4183b
Showing
5 changed files
with
80 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,47 @@ | ||
|
||
import React, { Component } from 'react' | ||
import ReactDOM from 'react-dom' | ||
import { connect } from 'react-redux' | ||
|
||
import AddTodo from './AddTodo.jsx' | ||
import TodoList from './TodoList.jsx' | ||
import TodoFilter from './TodoFilter.jsx' | ||
|
||
class App extends Component { | ||
|
||
constructor() { | ||
super() | ||
this.onAddClick = this.onAddClick.bind(this) | ||
this.onToggleClick = this.onToggleClick.bind(this) | ||
this.onFilterChange = this.onFilterChange.bind(this) | ||
|
||
this.state = {}; | ||
this.state.todos = [{ | ||
text: 'learn about react', | ||
completed: true | ||
},{ | ||
text: 'learn redux', | ||
completed: false | ||
}] | ||
this.state.filter = 'SHOW_ALL' | ||
|
||
this.filteredTodos = this.state.todos | ||
} | ||
import { addTodo, toggleTodo, setFilter, Filters } from './actions.js' | ||
|
||
onAddClick(input) { | ||
let newTodo = { | ||
text: input, | ||
completed: false | ||
} | ||
let todos = this.state.todos | ||
let newTodos = todos.concat([newTodo]) | ||
this.setState({todos: newTodos}) | ||
this.filterTodos(newTodos, this.state.filter) | ||
} | ||
|
||
onToggleClick(index) { | ||
let todos = this.state.todos | ||
let newTodos = [...todos] | ||
newTodos[index].completed = !newTodos[index].completed | ||
this.setState({todos: newTodos}) | ||
} | ||
|
||
onFilterChange(filter) { | ||
this.setState({filter: filter}) | ||
this.filterTodos(this.state.todos, filter) | ||
} | ||
|
||
filterTodos(todos, filter) { | ||
this.filteredTodos = todos; | ||
if(filter == 'SHOW_COMPLETED') { | ||
this.filteredTodos = todos.filter((todo) => { | ||
return todo.completed | ||
}) | ||
}else if(filter == 'SHOW_ACTIVE') { | ||
this.filteredTodos = todos.filter((todo) => { | ||
return !todo.completed | ||
}) | ||
} | ||
} | ||
class App extends Component { | ||
|
||
render() { | ||
const { dispatch, filteredTodos, filter } = this.props | ||
return ( | ||
<div> | ||
<AddTodo onAddClick={this.onAddClick} /> | ||
<TodoList todos={this.filteredTodos} onToggleClick={this.onToggleClick} /> | ||
<TodoFilter filter={this.state.filter} onFilterChange={this.onFilterChange} /> | ||
<AddTodo onAddClick={(text) => dispatch(addTodo(text))} /> | ||
<TodoList todos={filteredTodos} onToggleClick={(index) => dispatch(toggleTodo(index))} /> | ||
<TodoFilter filter={filter} onFilterChange={(filter) => dispatch(setFilter(filter))} /> | ||
</div> | ||
) | ||
} | ||
|
||
} | ||
|
||
ReactDOM.render(<App />, document.getElementById('todo')) | ||
function filterTodos(todos, filter) { | ||
switch(filter) { | ||
case Filters.SHOW_ALL: | ||
return todos; | ||
case Filters.SHOW_COMPLETED: | ||
return todos.filter(todo => todo.completed) | ||
case Filters.SHOW_ACTIVE: | ||
return todos.filter(todo => !todo.completed) | ||
} | ||
} | ||
|
||
function select(state) { | ||
console.log(state) | ||
return { | ||
filteredTodos: filterTodos(state.todos, state.filter), | ||
filter: state.filter | ||
} | ||
} | ||
|
||
export default connect(select)(App) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,17 @@ | ||
|
||
import React from 'react' | ||
import { render } from 'react-dom' | ||
import { createStore } from 'redux' | ||
import { Provider } from 'react-redux' | ||
|
||
import todoApp from './reducers.js' | ||
|
||
import { addTodo, toggleTodo, setFilter, Filters } from './actions.js' | ||
import App from './App.jsx' | ||
|
||
let store = createStore(todoApp) | ||
|
||
let unsubscribe = store.subscribe(() => { | ||
console.log(store.getState()) | ||
}) | ||
|
||
store.dispatch(addTodo('build a react app')) | ||
store.dispatch(addTodo('build a redux app')) | ||
store.dispatch(toggleTodo(1)) | ||
store.dispatch(setFilter(Filters.SHOW_ACTIVE)) | ||
|
||
unsubscribe(); | ||
store.dispatch(addTodo('build a todo app')) | ||
render( | ||
<Provider store={store}> | ||
<App /> | ||
</Provider>, | ||
document.getElementById('todo') | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
|
||
// import Counter from './Counter/index.js' | ||
|
||
// import Todo from './Todo/index.js' | ||
import Todo from './Todo/index.js' | ||
|
||
import App from './Todo/App.jsx' | ||
// import App from './Todo/App.jsx' | ||
|
||
import './Todo/index.less' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters