Skip to content

Commit 5295bfb

Browse files
reducers and actions added
1 parent 88f5234 commit 5295bfb

File tree

8 files changed

+154
-5
lines changed

8 files changed

+154
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// ./react-redux-client/src/actions/appActions.js
2+
3+
const apiUrl = "/api/";
4+
5+
export const toggleAddBook = () => {
6+
return {
7+
type: 'TOGGLE_ADD_TODO'
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// ./react-redux-client/src/actions/todoActions.js
2+
3+
const apiUrl = "http://localhost:3001/api/";
4+
5+
export const toggleAddBook = () => {
6+
return {
7+
type: 'TOGGLE_ADD_TODO'
8+
}
9+
}
10+
11+
export const addTodo = (todo) => {
12+
13+
}
14+
15+
export const deleteTodo = (todo) => {
16+
17+
}
18+
19+
export const editTodo = (todo) => {
20+
21+
}
22+
23+
//Async action
24+
export const fetchTodos = () => {
25+
// Returns a dispatcher function
26+
// that dispatches an action at later time
27+
return (dispatch) => {
28+
29+
dispatch(fetchTodosRequest());
30+
// Returns a promise
31+
return fetch(apiUrl)
32+
.then(response => res.json())
33+
.then(data =>
34+
// dispatch another action
35+
// to consume data
36+
dispatch(fetchTodosSuccess(data.books,data.message))
37+
)
38+
.then(error => {
39+
throw(error);
40+
})
41+
}
42+
}
43+
44+
export const fetchTodosRequest = () => {
45+
return {
46+
type:'FETCH_TODOS_REQUEST'
47+
}
48+
}
49+
50+
51+
//Sync action
52+
export const fetchTodosSuccess = (todos,message) => {
53+
return {
54+
type: 'FETCH_TODOS_SUCCESS',
55+
todos: todos,
56+
message: message,
57+
receivedAt: Date.now
58+
}
59+
};

react-redux-client/src/components/Todo.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default class Todo extends React.Component {
55

66

77
render(){
8-
const todo = this.props.mappedTodo;
8+
const todo = this.props.mappedTodoState.todo;
99
return(
1010
<div className="todoDetail">
1111
<h2>Todo Detail</h2>

react-redux-client/src/components/Todos.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ export default class Todos extends React.Component {
77
super(props);
88
}
99

10+
componentWillMount(){
11+
this.props.fetchTodos();
12+
}
13+
1014
showEditModal(bookToEdit){
1115
//this.props.mappedshowEditModal(todoToEdit);
1216
}
@@ -24,7 +28,7 @@ export default class Todos extends React.Component {
2428
}
2529

2630
render(){
27-
const todos = this.props.mappedTodos;
31+
const todos = this.props.mappedTodoState.todos;
2832
return(
2933
<div className="col-md-12">
3034
<h3>Books</h3>

react-redux-client/src/containers/Todo.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Todo from '../components/Todo';
77
const mapStateToProps = (state) => {
88
return {
99
//you can now say this.props.mappedAppSate
10-
mappedTodo: state.todo
10+
mappedTodoState: state.todoState
1111
}
1212
}
1313

react-redux-client/src/containers/Todos.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@ import Todos from '../components/Todos';
77
const mapStateToProps = (state) => {
88
return {
99
//you can now say this.props.mappedAppSate
10-
mappedTodos: state.todos
10+
mappedTodoState: state.todoState
1111
}
1212
}
1313

1414
// map actions to props
1515
const mapDispatchToProps = (dispatch) => {
1616
return {
1717
//you can now say this.props.mappedAppActions
18+
fetchTodos: () => dispatch(todoActions.fetchTodos()),
1819
mappedAddTodo: todo => dispatch(todoActions.addTodo(todo)),
1920
mappedDeleteTodo: todoToDelete => dispatch(todoActions.deleteTodo(todoToDelete)),
20-
mappedEditTodo: todoToEdit => dispatch(todoActions.deleteTodo(todoToEdit))
21+
mappedEditTodo: todoToEdit => dispatch(todoActions.editTodo(todoToEdit))
2122
}
2223
}
2324

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// ./react-redux-client/src/reducers/index.js
2+
import { routerReducer as routing } from 'react-router-redux';
3+
import { combineReducers } from 'redux';
4+
import appReducer from './appReducer';
5+
import todoReducer from './todoReducer';
6+
7+
export default combineReducers({
8+
appState:appReducer,
9+
todoState:todoReducer,
10+
routing
11+
// More reducers if there are
12+
// can go here
13+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// ./react-redux-client/src/reducers/todoReducer.js
2+
const INITIAL_STATE = {
3+
todos:[],
4+
todo:null,
5+
isFetching: false,
6+
error: null,
7+
successMsg:null,
8+
showDeleteModal: false,
9+
todoToDelete: null,
10+
showEditModal: false,
11+
todoToEdit: null,
12+
}
13+
14+
export const todoReducer = (currentState = INITIAL_STATE, action) => {
15+
switch (action.type) {
16+
case 'FETCH_TODOS_REQUEST':
17+
return {
18+
...currentState,
19+
todos:[],
20+
todo:null,
21+
isFetching: true,
22+
error: null,
23+
successMsg:null,
24+
showDeleteModal: false,
25+
todoToDelete: null,
26+
showEditModal: false,
27+
todoToEdit: null,
28+
}
29+
30+
case 'FETCH_TODOS_SUCCESS':
31+
return {
32+
...currentState,
33+
todos:action.todos,
34+
todo:null,
35+
isFetching: false,
36+
error: null,
37+
successMsg:action.message,
38+
showDeleteModal: false,
39+
todoToDelete: null,
40+
showEditModal: false,
41+
todoToEdit: null,
42+
}
43+
44+
case 'FETCH_TODOS_FAILED':
45+
return {
46+
...currentState,
47+
todos:[],
48+
todo:null,
49+
isFetching: false,
50+
error: action.message,
51+
successMsg:null,
52+
showDeleteModal: false,
53+
todoToDelete: null,
54+
showEditModal: false,
55+
todoToEdit: null,
56+
}
57+
58+
59+
default:
60+
return currentState;
61+
62+
}
63+
}

0 commit comments

Comments
 (0)