Skip to content

Commit 7099a27

Browse files
committed
counter 2 increments whenever counter 1 increments ;)
1 parent 0a15c08 commit 7099a27

File tree

6 files changed

+44
-13
lines changed

6 files changed

+44
-13
lines changed

src/App.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,34 @@ export default function App() {
3131

3232
<HeaderComponent />
3333

34+
<br />
35+
3436
<p>
3537
We, buttons below, live on the ground floor, level 0, in the App.js file
3638
directly but, we're cool!
3739
</p>
3840

39-
<p className="App__groundFloor">
41+
<p>
4042
<ButtonComponent
4143
instructions="Press me to increment counter 1"
4244
onClick={handleClick1}
4345
/>
44-
<hr />
46+
</p>
47+
48+
<hr />
49+
50+
<p>
51+
<span role="img" aria-label="pulling my tongue">
52+
👀 👍
53+
</span>
4554
<ButtonComponent
46-
instructions="No no, press me to increment counter 2, he's much better ;)"
55+
instructions="Whenever I see that counter 1 increments, I increment counter 2! This way, I'm always ahead of competition. But you can still press me though"
4756
onClick={handleClick2}
4857
/>
4958
</p>
5059

60+
<br />
61+
5162
<FooterComponent />
5263
</div>
5364
);

src/reducers/counter1/counter1.reducers.js

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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/sagas/counter2/counter2.sagas.js

Lines changed: 19 additions & 1 deletion
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();
@@ -13,7 +13,25 @@ function* incrementAsync() {
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+
yield put({ type: "INCREMENT_COUNTER_UPON_LISTENNING" });
20+
}
21+
1622
//increment saga
1723
export function* incrementCounter2Start() {
1824
yield takeLatest("INCREMENT_COUNTER_2_START", incrementAsync);
1925
}
26+
27+
export function* listenToCounter1Increments() {
28+
yield takeLatest("INCREMENT_COUNTER_1", listenToCounter1IncrementsAsync);
29+
}
30+
31+
export function* counter1Sagas() {
32+
yield all([
33+
//
34+
call(incrementCounter2Start),
35+
call(listenToCounter1Increments),
36+
]);
37+
}

src/sagas/rootSagas.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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";
55

66
export default function* rootSaga() {
7-
yield all([call(incrementCounter1Start), call(incrementCounter2Start)]);
7+
yield all([call(incrementCounter1Start), call(counter1Sagas)]);
88
}

src/styles.css

Lines changed: 7 additions & 5 deletions
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,5 @@ App > div > div {
4247
}
4348

4449
.App > div > div::before {
45-
content: "Depth: Level 2";
46-
}
47-
48-
.App__groundFloor {
50+
content: "Depth: 2";
4951
}

0 commit comments

Comments
 (0)