Skip to content

Commit e030569

Browse files
committed
new in-house thunk-like for async actions
1 parent c1b403c commit e030569

File tree

13 files changed

+108
-7
lines changed

13 files changed

+108
-7
lines changed
File renamed without changes.

src/components/HeaderComponent.jsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
import React from "react";
2+
23
import { useDataLayerValue } from "../context/DataLayer";
4+
import ButtonComponent from "./ButtonComponent";
35
import PrettyPrint from "./PrettyPrint";
46

57
function HeaderComponent() {
6-
const { state } = useDataLayerValue();
8+
const { state, thunk } = useDataLayerValue();
9+
10+
const getChuck = () => {
11+
thunk({
12+
type: "GET_CHUCK_START",
13+
payload: { someValue: "some value" },
14+
});
15+
};
716

817
return (
918
<div>
@@ -15,6 +24,14 @@ function HeaderComponent() {
1524
<div>
1625
<PrettyPrint toPrint={state} />
1726
</div>
27+
<p></p>
28+
<div>
29+
<ButtonComponent
30+
instructions="Get Chuck Norris (Thunk-like Async action)"
31+
onClick={getChuck}
32+
/>
33+
<p>{state.chuckNorris.data.value}</p>
34+
</div>
1835
</div>
1936
);
2037
}

src/context/DataLayer.jsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import React, { createContext, useContext, useReducer } from "react";
22
import { useReactSaga } from "use-react-saga";
3-
43
import allSagas from "../sagas/rootSagas";
54

5+
import { createThunkMiddleware } from "../middlewares/thunk";
6+
import allThunks from "../thunks/rootThunks";
7+
68
export const DataLayerContext = createContext();
79

810
export function DataLayer({ initialState, reducer, children }) {
911
const [state, dispatch] = useReducer(reducer, initialState);
1012
const put = useReactSaga({ state, dispatch, saga: allSagas });
13+
const thunk = createThunkMiddleware({ dispatch, thunks: allThunks });
1114

1215
return (
1316
<DataLayerContext.Provider
@@ -17,6 +20,8 @@ export function DataLayer({ initialState, reducer, children }) {
1720
dispatch,
1821
//Async actions => for using async functions (of type `generator`), which in turn dispatch an action object, in some unknown amount of time
1922
put,
23+
//
24+
thunk,
2025
}}
2126
>
2227
{children}

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import { DataLayer } from "./context/DataLayer";
55

66
import App from "./App";
77

8-
import allReducers, { allInitialStates } from "./reducers/rootReducers";
8+
import combinedReducers, {
9+
combinedInitialStates,
10+
} from "./reducers/rootReducers";
911

1012
const rootElement = document.getElementById("root");
1113
ReactDOM.render(
1214
<React.StrictMode>
13-
<DataLayer initialState={allInitialStates} reducer={allReducers}>
15+
<DataLayer initialState={combinedInitialStates} reducer={combinedReducers}>
1416
<App />
1517
</DataLayer>
1618
,

src/middlewares/demo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//A demo middleware
1+
// A Naive demo middleware
22
export function demo(fn) {
33
return function (...rest) {
44
console.log("Demo middleware", rest);

src/middlewares/logger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//Logger : A middleware
1+
//Logger : A Naive middleware
22
export function logger(fn) {
33
return function (...rest) {
44
console.group(fn.name);

src/middlewares/thunk.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function createThunkMiddleware({ dispatch, thunks }) {
2+
//
3+
//Naive Thunk-like middleware
4+
return async function interceptAction(thunkActionObj) {
5+
const actionObj = await thunks[thunkActionObj.type](
6+
{ payload: thunkActionObj.payload } || null
7+
);
8+
dispatch({ type: actionObj.type, payload: actionObj.payload });
9+
};
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export const chuckNorrisInitialState = {
2+
data: {},
3+
};
4+
5+
export function chuckNorrisReducers(state, action) {
6+
switch (action.type) {
7+
case "GOT_CHUCK":
8+
return {
9+
...state,
10+
data: action.payload,
11+
};
12+
default:
13+
return state;
14+
}
15+
}

src/reducers/rootReducers.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ import {
1414

1515
import { uiInitialState, uiReducers } from "./ui/ui.reducers";
1616

17-
export const allInitialStates = {
17+
import {
18+
chuckNorrisInitialState,
19+
chuckNorrisReducers,
20+
} from "./chuckNorris/chuckNorris.reducers";
21+
22+
export const combinedInitialStates = {
1823
counter1: counter1initialState,
1924
counter2: counter2initialState,
2025
ui: uiInitialState,
26+
chuckNorris: chuckNorrisInitialState,
2127
};
2228

2329
export default combineReducers(
@@ -27,6 +33,7 @@ export default combineReducers(
2733
counter1: counter1Reducers,
2834
counter2: counter2Reducers,
2935
ui: uiReducers,
36+
chuckNorris: chuckNorrisReducers,
3037
},
3138
[
3239
//Add middlewares here

src/styles.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ App > div > div {
104104
padding: 1rem;
105105
font-weight: 800;
106106
cursor: help;
107+
text-align: left;
107108
}
108109

109110
.prettyPrint pre.In {

0 commit comments

Comments
 (0)