Skip to content

Commit d2fdbae

Browse files
authored
Merge pull request #2 from boostup/new-version
New version
2 parents 716d8d4 + 86e2dfd commit d2fdbae

File tree

12 files changed

+172
-40
lines changed

12 files changed

+172
-40
lines changed

public/images/loader.gif

23.6 KB
Loading

src/App.js

+42-30
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ import React from "react";
33
import ButtonComponent from "./components/ButtonComponent";
44
import FooterComponent from "./components/FooterComponent";
55
import HeaderComponent from "./components/HeaderComponent";
6+
import Loader from "./components/Loader";
67

78
import { useDataLayerValue } from "./context/DataLayer";
89

910
import "./styles.css";
1011

1112
export default function App() {
12-
const {
13-
// state,//so es-lint does not brag!
14-
put,
15-
} = useDataLayerValue();
13+
const { state, put } = useDataLayerValue();
1614

1715
function handleClick1() {
1816
put({ type: "INCREMENT_COUNTER_1_START" });
@@ -23,32 +21,46 @@ export default function App() {
2321
}
2422

2523
return (
26-
<div className="App">
27-
<p>
28-
Imagine this is an actual website, where components need to display some
29-
state, or let's be crazy: even set some state !
30-
</p>
31-
32-
<HeaderComponent />
33-
34-
<p>
35-
We, buttons below, live on the ground floor, level 0, in the App.js file
36-
directly but, we're cool!
37-
</p>
38-
39-
<p className="App__groundFloor">
40-
<ButtonComponent
41-
instructions="Press me to increment counter 1"
42-
onClick={handleClick1}
43-
/>
24+
<>
25+
<div className="App">
26+
<p>
27+
Imagine this is an actual website, where components need to display
28+
some state, or let's be crazy: even set some state !
29+
</p>
30+
31+
<HeaderComponent />
32+
33+
<br />
34+
35+
<p>
36+
We, buttons below, live on the ground floor, level 0, in the App.js
37+
file directly but, we're cool!
38+
</p>
39+
40+
<p>
41+
<ButtonComponent
42+
instructions="Press me to increment counter 1"
43+
onClick={handleClick1}
44+
/>
45+
</p>
46+
4447
<hr />
45-
<ButtonComponent
46-
instructions="No no, press me to increment counter 2, he's much better ;)"
47-
onClick={handleClick2}
48-
/>
49-
</p>
50-
51-
<FooterComponent />
52-
</div>
48+
49+
<p>
50+
<span role="img" aria-label="pulling my tongue">
51+
👀 👍
52+
</span>
53+
<ButtonComponent
54+
instructions="PRESS ME!! BTW: whenever I see that counter 1 increments, I increment counter 2! This way, I'm always ahead of competition."
55+
onClick={handleClick2}
56+
/>
57+
</p>
58+
59+
<br />
60+
61+
<FooterComponent />
62+
</div>
63+
<Loader show={state.ui.isWorking} />
64+
</>
5365
);
5466
}

src/components/ButtonComponent.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react";
22

33
const ButtonComponent = ({ instructions, onClick }) => (
4-
<button onClick={onClick}>{instructions} (Asynchronous)</button>
4+
<button onClick={onClick}>{instructions} (Takes from 1 to 3 seconds)</button>
55
);
66
export default ButtonComponent;

src/components/Loader.jsx

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from "react";
2+
3+
function Loader({ show }) {
4+
console.log(show);
5+
return (
6+
<>
7+
{show ? (
8+
<div className="loader">
9+
<div className="loader__overlay" />
10+
<div className="loader__messageBox">
11+
<div className="loader__image">
12+
<img src="/images/loader.gif" alt="busy" />
13+
</div>
14+
</div>
15+
</div>
16+
) : null}
17+
</>
18+
);
19+
}
20+
21+
export default Loader;

src/reducers/counter1/counter1.reducers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export const counter1initialState = 0;
22

33
export function counter1Reducers(state, action) {
4-
console.log((state, action));
4+
// console.log(state, action);
55
switch (action.type) {
66
case "INCREMENT_COUNTER_1":
77
return state + 1;

src/reducers/counter2/counter2.reducers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export const counter2initialState = 0;
22

33
export function counter2Reducers(state, action) {
4-
console.log((state, action));
4+
console.log(state, action);
55
switch (action.type) {
66
case "INCREMENT_COUNTER_2":
77
return state + 1;

src/reducers/rootReducers.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
counter2initialState,
77
counter2Reducers,
88
} from "./counter2/counter2.reducers";
9+
import { uiInitialState, uiReducers } from "./ui/ui.reducers";
910

1011
function combineReducers(slices) {
1112
return function (prevState, action) {
@@ -21,11 +22,13 @@ function combineReducers(slices) {
2122
export const allInitialStates = {
2223
counter1: counter1initialState,
2324
counter2: counter2initialState,
25+
ui: uiInitialState,
2426
};
2527

2628
const allReducers = combineReducers({
2729
counter1: counter1Reducers,
2830
counter2: counter2Reducers,
31+
ui: uiReducers,
2932
});
3033

3134
export default allReducers;

src/reducers/ui/ui.reducers.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export const uiInitialState = {
2+
isWorking: false,
3+
};
4+
5+
export function uiReducers(state, action) {
6+
console.log(state, action);
7+
switch (action.type) {
8+
case "IS_WORKING":
9+
return {
10+
...state,
11+
isWorking: action.payload,
12+
};
13+
default:
14+
return state;
15+
}
16+
}

src/sagas/counter2/counter2.sagas.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { select, call, delay, put, takeLatest } from "redux-saga/effects";
1+
import { select, call, delay, put, takeLatest, all } from "redux-saga/effects";
22

33
function* incrementAsync() {
44
const previousState = yield select();
@@ -7,13 +7,30 @@ function* incrementAsync() {
77
"I AM STARTING TO INCREMENT FROM",
88
previousState.counter2
99
);
10-
yield delay(1000);
10+
yield delay(2000);
1111
yield put({ type: "INCREMENT_COUNTER_2" });
1212
const state = yield select();
1313
yield call(console.log, "I AM FINISHED INCREMENTING TO", state.counter2);
1414
}
1515

16+
function* listenToCounter1IncrementsAsync() {
17+
yield delay(1000);
18+
yield put({ type: "INCREMENT_COUNTER_2" });
19+
}
20+
1621
//increment saga
1722
export function* incrementCounter2Start() {
1823
yield takeLatest("INCREMENT_COUNTER_2_START", incrementAsync);
1924
}
25+
26+
export function* listenToCounter1Increments() {
27+
yield takeLatest("INCREMENT_COUNTER_1", listenToCounter1IncrementsAsync);
28+
}
29+
30+
export function* counter1Sagas() {
31+
yield all([
32+
//
33+
call(incrementCounter2Start),
34+
call(listenToCounter1Increments),
35+
]);
36+
}

src/sagas/rootSagas.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { all, call } from "redux-saga/effects";
22

33
import { incrementCounter1Start } from "./counter1/counter1.sagas";
4-
import { incrementCounter2Start } from "./counter2/counter2.sagas";
4+
import { counter1Sagas } from "./counter2/counter2.sagas";
5+
import { uiSagas } from "./ui/ui.sagas";
56

67
export default function* rootSaga() {
7-
yield all([call(incrementCounter1Start), call(incrementCounter2Start)]);
8+
yield all([
9+
//
10+
call(incrementCounter1Start),
11+
call(counter1Sagas),
12+
call(uiSagas),
13+
]);
814
}

src/sagas/ui/ui.sagas.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { call, put, takeLatest, all } from "redux-saga/effects";
2+
3+
export function* isWorkingAsync() {
4+
yield put({ type: "IS_WORKING", payload: true });
5+
}
6+
7+
export function* isFinishedAsync() {
8+
yield put({ type: "IS_WORKING", payload: false });
9+
}
10+
11+
export function* counter2StartedIncrementing() {
12+
yield takeLatest("INCREMENT_COUNTER_2_START", isWorkingAsync);
13+
}
14+
15+
export function* counter2FinishedIncrementing() {
16+
yield takeLatest("INCREMENT_COUNTER_2", isFinishedAsync);
17+
}
18+
19+
export function* uiSagas() {
20+
yield all([
21+
//
22+
call(counter2StartedIncrementing),
23+
call(counter2FinishedIncrementing),
24+
]);
25+
}

src/styles.css

+35-3
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,19 @@ App > div > div {
2626
padding: 1rem;
2727
}
2828

29+
.App > div,
30+
.App > div > div {
31+
padding-top: 3rem;
32+
}
33+
2934
.App > div > div {
3035
width: 80%;
3136
border: 5px solid teal;
3237
}
3338

3439
.App > div::before,
3540
.App > div > div::before {
36-
content: "Depth: Level 1";
41+
content: "Depth: 1";
3742
position: absolute;
3843
top: 0;
3944
left: 0;
@@ -42,8 +47,35 @@ App > div > div {
4247
}
4348

4449
.App > div > div::before {
45-
content: "Depth: Level 2";
50+
content: "Depth: 2";
51+
}
52+
53+
.loader {
54+
position: relative;
4655
}
4756

48-
.App__groundFloor {
57+
.loader__overlay {
58+
background-color: rgba(0, 0, 0, 0.5);
59+
position: fixed;
60+
height: 100vh;
61+
width: 100vw;
62+
top: 0;
63+
left: 0;
64+
}
65+
66+
.loader__messageBox {
67+
position: fixed;
68+
height: 100vh;
69+
width: 100vw;
70+
font-size: 5rem;
71+
top: 0;
72+
left: 0;
73+
display: flex;
74+
align-items: center;
75+
justify-content: center;
76+
}
77+
78+
.loader__image {
79+
padding: 1rem;
80+
background-color: white;
4981
}

0 commit comments

Comments
 (0)