Skip to content

Commit b2102a4

Browse files
committed
pretty print responds to state updates
1 parent 86e2dfd commit b2102a4

File tree

9 files changed

+104
-53
lines changed

9 files changed

+104
-53
lines changed

src/App.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,28 @@ export default function App() {
3939

4040
<p>
4141
<ButtonComponent
42-
instructions="Press me to increment counter 1"
42+
instructions={`Press me to increment counter 1 => ${state.counter1}`}
4343
onClick={handleClick1}
4444
/>
4545
</p>
4646

4747
<hr />
4848

4949
<p>
50-
<span role="img" aria-label="pulling my tongue">
51-
👀 👍
52-
</span>
5350
<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."
51+
instructions={`PRESS ME for counter 2 => ${state.counter2}`}
5552
onClick={handleClick2}
5653
/>
5754
</p>
55+
<span role="img" aria-label="pulling my tongue">
56+
👀 👍
57+
</span>
58+
<p>
59+
<strong>
60+
BTW: whenever I see that counter 1 increments, I increment counter
61+
2! This way, I'm always ahead of competition.
62+
</strong>
63+
</p>
5864

5965
<br />
6066

src/components/ButtonComponent.jsx

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import React from "react";
2+
import { useDataLayerValue } from "../context/DataLayer";
23

3-
const ButtonComponent = ({ instructions, onClick }) => (
4-
<button onClick={onClick}>{instructions} (Takes from 1 to 3 seconds)</button>
5-
);
4+
const ButtonComponent = ({ instructions, onClick }) => {
5+
const { dispatch } = useDataLayerValue();
6+
7+
function handleClick() {
8+
dispatch({ type: "IS_WORKING", payload: true });
9+
onClick();
10+
11+
let timer = setTimeout(() => {
12+
dispatch({ type: "IS_WORKING", payload: false });
13+
}, 1000);
14+
}
15+
16+
return <button onClick={handleClick}>{instructions}</button>;
17+
};
618
export default ButtonComponent;

src/components/HeaderComponent.jsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ function HeaderComponent() {
1212
state:
1313
</p>
1414

15-
<PrettyPrint toPrint={state} />
15+
<div>
16+
<PrettyPrint toPrint={state} />
17+
</div>
1618
</div>
1719
);
1820
}

src/components/Loader.jsx

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

33
function Loader({ show }) {
4-
console.log(show);
54
return (
65
<>
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>
6+
<div className={`loader ${show ? "In" : "Out"}`}>
7+
<div className="loader__overlay" />
8+
<div className="loader__messageBox">
9+
<div className="loader__image">
10+
<img src="/images/loader.gif" alt="busy" />
1411
</div>
1512
</div>
16-
) : null}
13+
</div>
1714
</>
1815
);
1916
}

src/components/PrettyPrint.jsx

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
1-
import React from "react";
2-
const PrettyPrint = ({ toPrint }) => (
3-
<pre>{JSON.stringify(toPrint, null, 2)}</pre>
4-
);
1+
import React, { useEffect, useLayoutEffect, useState } from "react";
2+
const PrettyPrint = ({ toPrint }) => {
3+
const [In, setIn] = useState("---");
4+
5+
useEffect(() => {
6+
let timer = null;
7+
8+
setIn("Out");
9+
10+
timer = setTimeout(() => {
11+
setIn("In");
12+
}, 100);
13+
14+
return () => {
15+
clearTimeout(timer);
16+
};
17+
}, [toPrint.counter1, toPrint.counter2]);
18+
19+
return (
20+
<div className="prettyPrint">
21+
<pre title="I animate when counters increment!" className={In}>
22+
{JSON.stringify(toPrint, null, 2)}
23+
</pre>
24+
</div>
25+
);
26+
};
527
export default PrettyPrint;

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/sagas/rootSagas.js

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

33
import { incrementCounter1Start } from "./counter1/counter1.sagas";
44
import { counter1Sagas } from "./counter2/counter2.sagas";
5-
import { uiSagas } from "./ui/ui.sagas";
65

76
export default function* rootSaga() {
87
yield all([
98
//
109
call(incrementCounter1Start),
1110
call(counter1Sagas),
12-
call(uiSagas),
1311
]);
1412
}

src/sagas/ui/ui.sagas.js

-25
This file was deleted.

src/styles.css

+42-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ App > div > div {
3838

3939
.App > div::before,
4040
.App > div > div::before {
41-
content: "Depth: 1";
41+
content: "Nesting Level: 1";
4242
position: absolute;
4343
top: 0;
4444
left: 0;
@@ -47,15 +47,21 @@ App > div > div {
4747
}
4848

4949
.App > div > div::before {
50-
content: "Depth: 2";
50+
content: "Nesting Level: 2";
5151
}
5252

5353
.loader {
5454
position: relative;
55+
opacity: 0;
56+
pointer-events: none;
57+
}
58+
59+
.loader.In {
60+
opacity: 1;
5561
}
5662

5763
.loader__overlay {
58-
background-color: rgba(0, 0, 0, 0.5);
64+
background-color: rgba(0, 0, 0, 0.3);
5965
position: fixed;
6066
height: 100vh;
6167
width: 100vw;
@@ -78,4 +84,37 @@ App > div > div {
7884
.loader__image {
7985
padding: 1rem;
8086
background-color: white;
87+
transform: scaleY(0);
88+
transition: transform 300ms cubic-bezier(0.075, 0.82, 0.165, 1);
89+
}
90+
91+
.In .loader__image {
92+
transform: scaleY(1);
93+
}
94+
95+
.prettyPrint {
96+
border: 1px solid teal;
97+
width: 80%;
98+
margin: 0 auto;
99+
}
100+
101+
.prettyPrint pre {
102+
margin: 0;
103+
background-color: teal;
104+
padding: 1rem;
105+
font-weight: 800;
106+
cursor: help;
107+
}
108+
109+
.prettyPrint pre.In {
110+
animation: animate 700ms cubic-bezier(0.175, 0.885, 0.32, 1.275);
111+
}
112+
113+
@keyframes animate {
114+
from {
115+
transform: rotate3d(0);
116+
}
117+
to {
118+
transform: perspective(600px) rotateX(360deg) scale(1.3);
119+
}
81120
}

0 commit comments

Comments
 (0)