Skip to content

Commit 16fb07d

Browse files
committed
added redux files
1 parent e8373b6 commit 16fb07d

File tree

6 files changed

+196
-26
lines changed

6 files changed

+196
-26
lines changed

4-redux/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "",
55
"main": "webpack.config.js",
66
"dependencies": {
7+
"axios": "^0.12.0",
78
"babel-loader": "^6.2.0",
89
"babel-plugin-add-module-exports": "^0.1.2",
910
"babel-plugin-react-html-attrs": "^2.0.0",
@@ -15,6 +16,9 @@
1516
"react": "^0.14.6",
1617
"react-dom": "^0.14.6",
1718
"redux": "^3.5.2",
19+
"redux-logger": "^2.6.1",
20+
"redux-promise-middleware": "^3.2.0",
21+
"redux-thunk": "^2.1.0",
1822
"webpack": "^1.12.9",
1923
"webpack-dev-server": "^1.14.1"
2024
},

4-redux/src/js/1-basic-setup.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { createStore } from "redux";
2+
3+
const reducer = (initialState=0, action) => {
4+
if (action.type === "INC") {
5+
return initialState + 1;
6+
} else if (action.type === "DEC") {
7+
return initialState - 1;
8+
}
9+
return initialState;
10+
}
11+
12+
const store = createStore(reducer, 1)
13+
14+
store.subscribe(() => {
15+
console.log("store changed", store.getState());
16+
})
17+
18+
store.dispatch({type: "INC"})
19+
store.dispatch({type: "INC"})
20+
store.dispatch({type: "INC"})
21+
store.dispatch({type: "DEC"})
22+
store.dispatch({type: "DEC"})
23+
store.dispatch({type: "DEC"})

4-redux/src/js/2-multiple-reducers.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { combineReducers, createStore } from "redux";
2+
3+
// I would live in a separate file
4+
const userReducer = (state={}, action) => {
5+
switch(action.type) {
6+
case "SET_NAME": {
7+
return {...state, name: action.payload};
8+
break;
9+
}
10+
case "SET_AGE": {
11+
return {...state, age: action.payload};
12+
break;
13+
}
14+
}
15+
return state;
16+
}
17+
18+
// I would live in a separate file
19+
const tweetsReducer = (state=[], action) => {
20+
switch(action.type) {
21+
case "ADD_TWEET": {
22+
return state.concat({
23+
id: Date.now(), //fake an ID by using a timestamp
24+
text: action.payload,
25+
});
26+
break;
27+
}
28+
}
29+
return state;
30+
}
31+
32+
const reducers = combineReducers({
33+
user: userReducer,
34+
tweets: tweetsReducer
35+
})
36+
37+
const store = createStore(reducers)
38+
39+
store.subscribe(() => {
40+
console.log("store changed", store.getState());
41+
})
42+
43+
store.dispatch({type: "SET_NAME", payload: "Will"})
44+
store.dispatch({type: "SET_AGE", payload: 35})
45+
store.dispatch({type: "SET_AGE", payload: 34})
46+
store.dispatch({type: "ADD_TWEET", payload: "OMG LIKE LOL"})
47+
store.dispatch({type: "ADD_TWEET", payload: "I am so like seriously like totally like right now"})

4-redux/src/js/3-middleware.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { applyMiddleware, createStore } from "redux";
2+
3+
const reducer = (initialState=0, action) => {
4+
if (action.type === "INC") {
5+
return initialState + 1;
6+
} else if (action.type === "DEC") {
7+
return initialState - 1;
8+
} else if (action.type === "MULT") {
9+
throw new Error("AHHHH!!");
10+
}
11+
return initialState;
12+
}
13+
14+
const logger = (store) => (next) => (action) => {
15+
console.log("Logged", action);
16+
return next(action);
17+
};
18+
19+
20+
const errorHandler = (store) => (next) => (action) => {
21+
try {
22+
return next(action);
23+
} catch(e) {
24+
console.log("ERROR!", e);
25+
}
26+
};
27+
28+
const middleware = applyMiddleware(
29+
logger,
30+
errorHandler
31+
)
32+
const store = createStore(reducer, middleware)
33+
34+
store.subscribe(() => {
35+
console.log("store changed", store.getState());
36+
})
37+
38+
store.dispatch({type: "INC"})
39+
store.dispatch({type: "INC"})
40+
store.dispatch({type: "INC"})
41+
store.dispatch({type: "DEC"})
42+
store.dispatch({type: "DEC"})
43+
store.dispatch({type: "DEC"})
44+
store.dispatch({type: "MULT"})
45+
store.dispatch({type: "DEC"})

4-redux/src/js/4-async-middleware.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { applyMiddleware, createStore } from "redux";
2+
import axios from "axios";
3+
import logger from "redux-logger";
4+
import thunk from "redux-thunk";
5+
import promise from "redux-promise-middleware";
6+
7+
const initialState = {
8+
fetching: false,
9+
fetched: false,
10+
users: [],
11+
error: null,
12+
};
13+
14+
const reducer = (state=initialState, action) => {
15+
switch (action.type) {
16+
case "FETCH_USERS_PENDING": {
17+
return {...state, fetching: true}
18+
break;
19+
}
20+
case "FETCH_USERS_REJECTED": {
21+
return {...state, fetching: false, error: action.payload}
22+
break;
23+
}
24+
case "FETCH_USERS_ FULFILLED": {
25+
return {
26+
...state,
27+
fetching: false,
28+
fetched: true,
29+
users: action.payload,
30+
}
31+
break;
32+
}
33+
}
34+
return state
35+
}
36+
37+
const middleware = applyMiddleware(promise(), thunk, logger())
38+
const store = createStore(reducer, middleware)
39+
40+
store.dispatch({
41+
type: "FETCH_USERS",
42+
payload: axios.get("http://rest.learncode.academy/api/wstern/users")
43+
})

4-redux/src/js/client.js

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
1-
import { combineReducers, createStore } from "redux";
1+
import { applyMiddleware, createStore } from "redux";
2+
import axios from "axios";
3+
import logger from "redux-logger";
4+
import thunk from "redux-thunk";
5+
import promise from "redux-promise-middleware";
26

3-
const userReducer = (state={}, action) => {
4-
switch(action.type) {
5-
case "CHANGE_NAME": {
6-
state = {...state, name: action.payload}
7+
const initialState = {
8+
fetching: false,
9+
fetched: false,
10+
users: [],
11+
error: null,
12+
};
13+
14+
const reducer = (state=initialState, action) => {
15+
switch (action.type) {
16+
case "FETCH_USERS_PENDING": {
17+
return {...state, fetching: true}
18+
break;
19+
}
20+
case "FETCH_USERS_REJECTED": {
21+
return {...state, fetching: false, error: action.payload}
722
break;
823
}
9-
case "CHANGE_AGE": {
10-
state = {...state, age: action.payload}
24+
case "FETCH_USERS_FULFILLED": {
25+
return {
26+
...state,
27+
fetching: false,
28+
fetched: true,
29+
users: action.payload,
30+
}
1131
break;
1232
}
1333
}
14-
return state;
15-
};
34+
return state
35+
}
1636

17-
const tweetsReducer = (state=[], action) => {
18-
return state;
19-
};
37+
const middleware = applyMiddleware(promise(), thunk, logger())
38+
const store = createStore(reducer, middleware)
2039

21-
const reducers = combineReducers({
22-
user: userReducer,
23-
tweets: tweetsReducer,
40+
store.dispatch({
41+
type: "FETCH_USERS",
42+
payload: axios.get("http://rest.learncode.academy/api/wstern/users")
2443
})
25-
26-
const store = createStore(reducers);
27-
28-
store.subscribe(() => {
29-
console.log("store changed", store.getState())
30-
})
31-
32-
store.dispatch({type: "CHANGE_NAME", payload: "Will"})
33-
store.dispatch({type: "CHANGE_AGE", payload: 35})
34-
store.dispatch({type: "CHANGE_AGE", payload: 36})
35-

0 commit comments

Comments
 (0)