Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e6cb57d

Browse files
create undoable counter
Signed-off-by: Amit Amrutiya <amitamrutiya2210@gmail.com>
1 parent a9a3351 commit e6cb57d

File tree

7 files changed

+241
-0
lines changed

7 files changed

+241
-0
lines changed

‎React/undoable-counter/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Undoable Counter
2+
3+
![image](https://github.com/user-attachments/assets/342c93d9-b2ab-404b-8ec8-28bb144aa3b2)

‎React/undoable-counter/package.json‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "faq-comp",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@testing-library/jest-dom": "^5.17.0",
7+
"@testing-library/react": "^13.4.0",
8+
"@testing-library/user-event": "^13.5.0",
9+
"react": "^18.2.0",
10+
"react-dom": "^18.2.0",
11+
"react-scripts": "5.0.1",
12+
"web-vitals": "^2.1.4"
13+
},
14+
"scripts": {
15+
"start": "react-scripts start",
16+
"build": "react-scripts build",
17+
"test": "react-scripts test",
18+
"eject": "react-scripts eject"
19+
},
20+
"eslintConfig": {
21+
"extends": [
22+
"react-app",
23+
"react-app/jest"
24+
]
25+
},
26+
"browserslist": {
27+
"production": [
28+
">0.2%",
29+
"not dead",
30+
"not op_mini all"
31+
],
32+
"development": [
33+
"last 1 chrome version",
34+
"last 1 firefox version",
35+
"last 1 safari version"
36+
]
37+
}
38+
}
3.78 KB
Binary file not shown.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta
9+
name="description"
10+
content="Web site created using create-react-app"
11+
/>
12+
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13+
<!--
14+
manifest.json provides metadata used when your web app is installed on a
15+
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16+
-->
17+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18+
<!--
19+
Notice the use of %PUBLIC_URL% in the tags above.
20+
It will be replaced with the URL of the `public` folder during the build.
21+
Only files inside the `public` folder can be referenced from the HTML.
22+
23+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24+
work correctly both with client-side routing and a non-root public URL.
25+
Learn how to configure a non-root public URL by running `npm run build`.
26+
-->
27+
<title>React App</title>
28+
</head>
29+
<body>
30+
<noscript>You need to enable JavaScript to run this app.</noscript>
31+
<div id="root"></div>
32+
<!--
33+
This HTML file is a template.
34+
If you open it directly in the browser, you will see an empty page.
35+
36+
You can add webfonts, meta tags, or analytics to this file.
37+
The build step will place the bundled scripts into the <body> tag.
38+
39+
To begin the development, run `npm start` or `yarn start`.
40+
To create a production bundle, use `npm run build` or `yarn build`.
41+
-->
42+
</body>
43+
</html>

‎React/undoable-counter/src/App.js‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { useState } from "react";
2+
3+
function App() {
4+
const [value, setValue] = useState(0);
5+
const [redoList, setRedoList] = useState([]);
6+
const [history, setHistory] = useState([]);
7+
const [undoCount, setUndoCount] = useState(0);
8+
9+
const maintainHistory = (key, prev, curr) => {
10+
console.log(key, prev, curr);
11+
const obj = {
12+
action: key,
13+
prev,
14+
curr,
15+
};
16+
const copyHistory = [...history];
17+
copyHistory.unshift(obj); //
18+
setHistory(copyHistory);
19+
};
20+
21+
const handleClick = (key) => {
22+
const val = parseInt(key);
23+
console.log(key);
24+
maintainHistory(key, value, val + value);
25+
setValue((existingValue) => existingValue + val);
26+
};
27+
28+
const handleUndo = () => {
29+
//stack LIFO
30+
//histor [ +10, +1] redolist [-1]
31+
if (history.length) {
32+
if (undoCount + 1 > 5) {
33+
alert("You cant undo beyond limit=5");
34+
return;
35+
}
36+
setUndoCount((prev) => prev + 1);
37+
const copyHist = [...history];
38+
const firstItem = copyHist.shift();
39+
setHistory(copyHist);
40+
41+
setValue(firstItem.prev);
42+
43+
const copyRedoList = [...redoList];
44+
copyRedoList.push(firstItem);
45+
setRedoList(copyRedoList);
46+
}
47+
};
48+
49+
console.log(redoList);
50+
51+
const handleRedo = () => {
52+
if (redoList.length) {
53+
const copyRedoList = [...redoList];
54+
const poppedValue = copyRedoList.pop();
55+
setRedoList(copyRedoList);
56+
const { action, prev, curr } = poppedValue;
57+
setValue(curr);
58+
maintainHistory(action, prev, curr);
59+
// [+100,+10,+1]
60+
}
61+
};
62+
63+
return (
64+
<div className="App">
65+
<h1>Undoable Counter</h1>
66+
<div className="action-btn">
67+
<button onClick={handleUndo}>Undo</button>
68+
<button onClick={handleRedo}>Redo</button>
69+
</div>
70+
71+
<div className="user-actions">
72+
{[-100, -10, -1].map((btn) => {
73+
return <button onClick={() => handleClick(btn)}>{btn}</button>;
74+
})}
75+
<div style={{ fontSize: 40 }}>{value}</div>
76+
{["+1", "+10", "+100"].map((btn) => {
77+
return <button onClick={() => handleClick(btn)}>{btn}</button>;
78+
})}
79+
</div>
80+
81+
<div className="history">
82+
{history.map((item) => {
83+
return (
84+
<div className="row">
85+
<div>{item.action}</div>
86+
<div>{`[ ${item.prev} -> ${item.curr} ]`}</div>
87+
</div>
88+
);
89+
})}
90+
</div>
91+
</div>
92+
);
93+
}
94+
95+
export default App;

‎React/undoable-counter/src/index.css‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
body {
2+
margin: 0;
3+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4+
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5+
sans-serif;
6+
-webkit-font-smoothing: antialiased;
7+
-moz-osx-font-smoothing: grayscale;
8+
}
9+
10+
code {
11+
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12+
monospace;
13+
}
14+
15+
.App {
16+
text-align: center;
17+
}
18+
19+
.user-actions{
20+
display: flex;
21+
justify-content: center;
22+
align-items: center;
23+
}
24+
25+
button{
26+
width: 100px;
27+
height: 40px;
28+
border: none;
29+
outline: none;
30+
font-size: 20px;
31+
border-radius: 5px;
32+
color: #fff;
33+
background-color: purple;
34+
margin: 10px;
35+
font-weight: bold;
36+
cursor: pointer;
37+
}
38+
.history{
39+
width: 300px;
40+
height: 300px;
41+
margin: 100px auto;
42+
border: 1px solid black;
43+
overflow-y: auto;
44+
}
45+
.row{
46+
display: flex;
47+
justify-content: space-around;
48+
align-items: center;
49+
padding: 5px;
50+
font-size: 20px;
51+
}

‎React/undoable-counter/src/index.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom/client';
3+
import './index.css';
4+
import App from './App';
5+
6+
const root = ReactDOM.createRoot(document.getElementById('root'));
7+
root.render(
8+
<React.StrictMode>
9+
<App />
10+
</React.StrictMode>
11+
);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /