Skip to content

Commit e70bdf9

Browse files
committed
actions and reducers
1 parent e71aa0b commit e70bdf9

File tree

3 files changed

+67
-20
lines changed

3 files changed

+67
-20
lines changed

src/actions/index.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,40 @@
1-
import axios from 'axios';
1+
// import axios from 'axios';
22

33
//Task List:
44
//1. Add a thunk action called fetchSmurfs that triggers a loading status display in our application, performs an axios call to retreive smurfs from our server, saves the result of that call to our state and shows an error if one is made.
55
//2. Add a standard action that allows us to add new smurf (including the name, nickname, position, summary)
6-
//3. Add a standard action that allows us to set the value of the error message slice of state.
6+
//3. Add a standard action that allows us to set the value of the error message slice of state.
7+
8+
9+
import axios from 'axios';
10+
11+
export const FETCH_START = 'FETCH_START';
12+
export const FETCH_SUCCESS = 'FETCH_SUCCESS';
13+
export const FETCH_FAIL = 'FETCH_FAIL';
14+
15+
export const ADD_SMURF = 'ADD_SMURF';
16+
export const SET_ERROR = 'SET_ERROR';
17+
18+
export const getSmurfs = () => (dispatch) =>{
19+
dispatch({type: FETCH_START});
20+
axios.get('http://localhost:3333/smurfs')
21+
.then((res =>{
22+
dispatch({type:FETCH_SUCCESS, payload:res.data})
23+
}))
24+
.catch(err =>{
25+
dispatch({type: FETCH_FAIL,payload:err})
26+
})}
27+
28+
export const addSmurf = ({name,position,nickname, description})=>{
29+
30+
return {
31+
type:ADD_SMURF,
32+
payload:
33+
{name,position,nickname,description}
34+
}}
35+
export const errorMessage = (message) =>{
36+
37+
return{
38+
type:SET_ERROR,
39+
payload: message
40+
}}

src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ const store = createStore( applyMiddleware( thunk, logger ), reducer );
4444

4545

4646

47-
4847
const rootElement = document.getElementById("root");
4948

5049
ReactDOM.render(

src/reducers/index.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
1+
//
2+
// export const initialState = {
3+
// }
4+
//
5+
// const reducer = ()=>{
6+
// }
7+
//
8+
// export default reducer;
9+
//
10+
// //Task List:
11+
// //1. Adds the following state values into the initialState:
12+
// // - an array of smurfs
13+
// // - a boolean indicating if the app is loading
14+
// // - a string indicating a possible error message
15+
//
16+
// //2. Add in the arguments needed to complete a standard reducer function.
17+
// //3. Add in a reducer case to accomidate the start of a smurf fetch.
18+
// //4. Add in a reducer case to accomidate the successful smurf api fetch.
19+
// //5. Add in a reducer cases to accomidate the failed smurf api fetch.
20+
// //6. Add in a reducer case to accomidate adding a smurf (including the name, nickname, position, summary and an internally generated id) into your smurf list.
21+
// //7. Add in a reducer case that adds in a value to the error message.
122

2-
export const initialState = {
3-
}
4-
5-
const reducer = ()=>{
6-
}
723

8-
export default reducer;
924

10-
//Task List:
11-
//1. Adds the following state values into the initialState:
12-
// - an array of smurfs
13-
// - a boolean indicating if the app is loading
14-
// - a string indicating a possible error message
25+
import FETCH_START from '../actions/index'
26+
import ADD_SMURF from '../actions/index'
27+
import FETCH_SUCCESS from '../actions/index'
28+
import SET_ERROR from '../actions/index'
29+
import FETCH_FAIL from '../actions/index'
1530

16-
//2. Add in the arguments needed to complete a standard reducer function.
17-
//3. Add in a reducer case to accomidate the start of a smurf fetch.
18-
//4. Add in a reducer case to accomidate the successful smurf api fetch.
19-
//5. Add in a reducer cases to accomidate the failed smurf api fetch.
20-
//6. Add in a reducer case to accomidate adding a smurf (including the name, nickname, position, summary and an internally generated id) into your smurf list.
21-
//7. Add in a reducer case that adds in a value to the error message.
31+
export const initialState = {
32+
smurfsArray: [],
33+
appLoading: false,
34+
error: ''
35+
}

0 commit comments

Comments
 (0)