File tree 8 files changed +154
-5
lines changed
8 files changed +154
-5
lines changed Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ export default class Todo extends React.Component {
5
5
6
6
7
7
render ( ) {
8
- const todo = this . props . mappedTodo ;
8
+ const todo = this . props . mappedTodoState . todo ;
9
9
return (
10
10
< div className = "todoDetail" >
11
11
< h2 > Todo Detail</ h2 >
Original file line number Diff line number Diff line change @@ -7,6 +7,10 @@ export default class Todos extends React.Component {
7
7
super ( props ) ;
8
8
}
9
9
10
+ componentWillMount ( ) {
11
+ this . props . fetchTodos ( ) ;
12
+ }
13
+
10
14
showEditModal ( bookToEdit ) {
11
15
//this.props.mappedshowEditModal(todoToEdit);
12
16
}
@@ -24,7 +28,7 @@ export default class Todos extends React.Component {
24
28
}
25
29
26
30
render ( ) {
27
- const todos = this . props . mappedTodos ;
31
+ const todos = this . props . mappedTodoState . todos ;
28
32
return (
29
33
< div className = "col-md-12" >
30
34
< h3 > Books</ h3 >
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ import Todo from '../components/Todo';
7
7
const mapStateToProps = ( state ) => {
8
8
return {
9
9
//you can now say this.props.mappedAppSate
10
- mappedTodo : state . todo
10
+ mappedTodoState : state . todoState
11
11
}
12
12
}
13
13
Original file line number Diff line number Diff line change @@ -7,17 +7,18 @@ import Todos from '../components/Todos';
7
7
const mapStateToProps = ( state ) => {
8
8
return {
9
9
//you can now say this.props.mappedAppSate
10
- mappedTodos : state . todos
10
+ mappedTodoState : state . todoState
11
11
}
12
12
}
13
13
14
14
// map actions to props
15
15
const mapDispatchToProps = ( dispatch ) => {
16
16
return {
17
17
//you can now say this.props.mappedAppActions
18
+ fetchTodos : ( ) => dispatch ( todoActions . fetchTodos ( ) ) ,
18
19
mappedAddTodo : todo => dispatch ( todoActions . addTodo ( todo ) ) ,
19
20
mappedDeleteTodo : todoToDelete => dispatch ( todoActions . deleteTodo ( todoToDelete ) ) ,
20
- mappedEditTodo : todoToEdit => dispatch ( todoActions . deleteTodo ( todoToEdit ) )
21
+ mappedEditTodo : todoToEdit => dispatch ( todoActions . editTodo ( todoToEdit ) )
21
22
}
22
23
}
23
24
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments