-
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
Shek Hang Ng
authored and
Shek Hang Ng
committed
Nov 6, 2021
1 parent
24d506f
commit 9b2787f
Showing
11 changed files
with
109 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
let nextTodoId = 0 | ||
export const addTodo = text => ({ | ||
type: 'ADD_TODO', | ||
id: nextTodoId++, | ||
text | ||
}) | ||
|
||
export const setVisibilityFilter = filter => ({ | ||
type: 'SET_VISIBILITY_FILTER', | ||
filter | ||
}) | ||
|
||
export const toggleTodo = id => ({ | ||
type: 'TOGGLE_TODO', | ||
id | ||
}) | ||
|
||
export const VisibilityFilters = { | ||
ALL: "ALL", | ||
ACTIVE: "ACTIVE", | ||
COMPLETED: "COMPLETED" | ||
} |
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
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,13 +1,14 @@ | ||
import React from 'react' | ||
import {Status} from '../utils/constant' | ||
import {VisibilityFilters, setVisibilityFilter} from '../actions' | ||
import {connect} from 'react-redux' | ||
|
||
const TodoFilter = ({onFilter}) => ( | ||
const TodoFilter = ({dispatch}) => ( | ||
<div> | ||
Show {' '} | ||
<button onClick={()=>onFilter(Status.ALL)}>All</button>{' '} | ||
<button onClick={()=>onFilter(Status.ACTIVE)}>Active</button>{' '} | ||
<button onClick={()=>onFilter(Status.COMPLETED)}>Completed</button> | ||
<button onClick={()=>dispatch(setVisibilityFilter(VisibilityFilters.ALL))}>All</button>{' '} | ||
<button onClick={()=>dispatch(setVisibilityFilter(VisibilityFilters.ACTIVE))}>Active</button>{' '} | ||
<button onClick={()=>dispatch(setVisibilityFilter(VisibilityFilters.COMPLETED))}>Completed</button> | ||
</div> | ||
) | ||
|
||
export default TodoFilter | ||
export default connect()(TodoFilter) |
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,24 +1,31 @@ | ||
import React from 'react' | ||
import Todo from './Todo' | ||
import {Status} from '../utils/constant' | ||
import {VisibilityFilters} from '../actions' | ||
import { connect } from 'react-redux' | ||
|
||
const getFilterTodos = (todos, filter) => { | ||
switch(filter) { | ||
case Status.ALL: | ||
case VisibilityFilters.ALL: | ||
return todos; | ||
case Status.ACTIVE: | ||
case VisibilityFilters.ACTIVE: | ||
return todos.filter(val => !val.completed) | ||
case Status.COMPLETED: | ||
case VisibilityFilters.COMPLETED: | ||
return todos.filter(val => val.completed) | ||
default: | ||
throw new Error('Unknown filter: ' + filter) | ||
} | ||
} | ||
|
||
const TodoList = ({todos, filter, onComplete}) => ( | ||
const mapStateToProps = state => ({ | ||
todos: getFilterTodos(state.todos, state.visibilityFilter) | ||
}) | ||
|
||
const TodoList = ({todos}) => ( | ||
<ul> | ||
{ getFilterTodos(todos, filter).map(todo => <Todo key={todo.id} onComplete={onComplete} {...todo} />) } | ||
{ todos.map(todo => <Todo key={todo.id} {...todo} />) } | ||
</ul> | ||
) | ||
|
||
export default TodoList | ||
export default connect( | ||
mapStateToProps | ||
)(TodoList) |
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,8 +1,15 @@ | ||
import React from 'react' | ||
import ReactDOM from 'react-dom' | ||
import App from './app' | ||
import {createStore} from 'redux' | ||
import {Provider} from 'react-redux' | ||
import rootReducer from './reducers' | ||
|
||
const store = createStore(rootReducer) | ||
|
||
ReactDOM.render( | ||
<App/>, | ||
<Provider store={store}> | ||
<App/> | ||
</Provider>, | ||
document.getElementById('root') | ||
) |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { combineReducers } from 'redux' | ||
import todos from './todos' | ||
import visibilityFilter from './visibilityFilter' | ||
|
||
export default combineReducers({ | ||
todos, | ||
visibilityFilter | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const todos = (state = [{id:0, text:'task1', completed:false}], action) => { | ||
switch (action.type) { | ||
case 'ADD_TODO': | ||
return [ | ||
...state, | ||
{ | ||
id: action.id, | ||
text: action.text, | ||
completed: false | ||
} | ||
] | ||
case 'TOGGLE_TODO': | ||
return state.map(todo => | ||
(todo.id === action.id) ? | ||
{...todo, completed: !todo.completed} : | ||
todo | ||
) | ||
default: | ||
return state | ||
} | ||
} | ||
|
||
export default todos |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { VisibilityFilters } from '../actions' | ||
|
||
const visibilityFilter = (state = VisibilityFilters.ALL, action) => { | ||
switch (action.type) { | ||
case 'SET_VISIBILITY_FILTER': | ||
return action.filter | ||
default: | ||
return state | ||
} | ||
} | ||
|
||
export default visibilityFilter |
This file was deleted.
Oops, something went wrong.