Skip to content

Commit

Permalink
React Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramez-Ibrahim committed Jul 1, 2024
1 parent 211b862 commit ff3a130
Show file tree
Hide file tree
Showing 10 changed files with 300 additions and 0 deletions.
Binary file added public/favicon.ico
Binary file not shown.
43 changes: 43 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Binary file added public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
56 changes: 56 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React, { useRef, useEffect } from 'react';
import useCalculator from './Calculator';
import './styles.css'; // Ensure this path is correct based on your file structure

function App() {
const {
current,
previous,
clear,
deleteLast,
appendNumber,
chooseOperation,
compute,
} = useCalculator();

const previousOperandRef = useRef(null);
const currentOperandRef = useRef(null);

useEffect(() => {
if (previousOperandRef.current) {
previousOperandRef.current.innerText = previous;
}
if (currentOperandRef.current) {
currentOperandRef.current.innerText = current;
}
}, [current, previous]);

return (
<div className="calculator-grid">
<div className="output">
<div data-previous-operand ref={previousOperandRef} className="previous-operand"></div>
<div data-current-operand ref={currentOperandRef} className="current-operand"></div>
</div>
<button className="span-two" onClick={clear}>AC</button>
<button onClick={deleteLast}>DEL</button>
<button onClick={() => chooseOperation("÷")}>÷</button>
<button onClick={() => appendNumber("1")}>1</button>
<button onClick={() => appendNumber("2")}>2</button>
<button onClick={() => appendNumber("3")}>3</button>
<button onClick={() => chooseOperation("*")}>*</button>
<button onClick={() => appendNumber("4")}>4</button>
<button onClick={() => appendNumber("5")}>5</button>
<button onClick={() => appendNumber("6")}>6</button>
<button onClick={() => chooseOperation("+")}>+</button>
<button onClick={() => appendNumber("7")}>7</button>
<button onClick={() => appendNumber("8")}>8</button>
<button onClick={() => appendNumber("9")}>9</button>
<button onClick={() => chooseOperation("-")}>-</button>
<button onClick={() => appendNumber(".")}>.</button>
<button onClick={() => appendNumber("0")}>0</button>
<button className="span-two" onClick={compute}>=</button>
</div>
);
}

export default App;
96 changes: 96 additions & 0 deletions src/Calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Calculator.js
import { useState, useEffect } from 'react';

const useCalculator = () => {
const [currentOperand, setCurrentOperand] = useState("");
const [previousOperand, setPreviousOperand] = useState("");
const [operation, setOperation] = useState(null);

const clear = () => {
setCurrentOperand("");
setPreviousOperand("");
setOperation(null);
};

const deleteLast = () => {
setCurrentOperand(currentOperand.toString().slice(0, -1));
};

const appendNumber = (number) => {
if (number === "." && currentOperand.includes(".")) return;
setCurrentOperand(currentOperand + number);
};

const chooseOperation = (op) => {
if (currentOperand === "") return;
if (previousOperand !== "") {
compute();
}
setOperation(op);
setPreviousOperand(currentOperand);
setCurrentOperand("");
};

const compute = () => {
const prev = parseFloat(previousOperand);
const current = parseFloat(currentOperand);
if (isNaN(prev) || isNaN(current)) return;
let computation;
switch (operation) {
case "+":
computation = prev + current;
break;
case "-":
computation = prev - current;
break;
case "*":
computation = prev * current;
break;
case "÷":
computation = prev / current;
break;
default:
return;
}
setCurrentOperand(computation.toString());
setOperation(null);
setPreviousOperand("");
};

const getDisplayNumber = (number) => {
const stringNumber = number.toString();
const integerDigits = parseFloat(stringNumber.split('.')[0]);
const decimalDigits = stringNumber.split('.')[1];
let integerDisplay;
if (isNaN(integerDigits)) {
integerDisplay = "";
} else {
integerDisplay = integerDigits.toLocaleString('en', { maximumFractionDigits: 0 });
}
if (decimalDigits != null) {
return `${integerDisplay}.${decimalDigits}`;
} else {
return integerDisplay;
}
};

const updateDisplay = () => {
const current = getDisplayNumber(currentOperand);
const previous = operation ? `${getDisplayNumber(previousOperand)} ${operation}` : "";
return { current, previous };
};

const { current, previous } = updateDisplay();

return {
current,
previous,
clear,
deleteLast,
appendNumber,
chooseOperation,
compute,
};
};

export default useCalculator;
14 changes: 14 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
63 changes: 63 additions & 0 deletions src/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* Global styles */
*, *::before, *::after {
box-sizing: border-box; /* Ensures consistent box sizing for all elements */
font-family: Gotham Rounded, sans-serif; /* Sets the font family for all elements */
font-weight: normal; /* Sets normal font weight for all elements */
}

/* Body styles */
body {
padding: 0;
margin: 0;
background: linear-gradient(to right, #00AAFF, #00FF6C); /* Sets a linear gradient background color */
}

/* Calculator grid styles */
.calculator-grid {
display: grid; /* Sets the display type to grid */
justify-content: center; /* Centers the grid horizontally */
align-content: center; /* Centers the grid vertically */
min-height: 100vh; /* Sets the minimum height of the grid to the full viewport height */
grid-template-columns: repeat(4, 85px); /* Sets the grid columns to repeat 4 times with a width of 85px each */
grid-template-rows: minmax(120px, auto) repeat(5, 85px); /* Sets the grid rows with a minimum height of 120px for the output and 85px for the buttons */
}

.calculator-grid > button {
cursor: pointer; /* Sets a pointer cursor for the buttons */
font-size: 2rem; /* Sets the font size for the buttons */
border: 1px solid white; /* Sets a white border for the buttons */
outline: none; /* Removes the outline on focus */
background-color: rgba(255, 255, 255, .75); /* Sets the background color for the buttons */
}

.calculator-grid > button:hover {
background-color: rgba(255, 255, 255, .9); /* Sets the background color for the buttons on hover */
}

.span-two {
grid-column: span 2; /* Spans the button across 2 grid columns */
}

/* Output styles */
.output {
grid-column: 1 / -1; /* Sets the output element to span across all grid columns */
background-color: rgba(0, 0, 0, .75); /* Sets the background color for the output element */
display: flex; /* Sets the display type to flex */
align-items: flex-end; /* Aligns the content at the bottom of the output element */
justify-content: space-around; /* Distributes the content evenly along the horizontal axis */
flex-direction: column; /* Sets the flex direction to column */
padding: 10px; /* Adds padding to the output element */
word-wrap: break-word; /* Allows long words to break and wrap to the next line */
word-break: break-all; /* Breaks words at any character to fit within the container */
}

.output .previous-operand {
color: rgba(255, 255, 255, .75); /* Sets the color for the previous operand */
font-size: 1.5rem; /* Sets the font size for the previous operand */
}

.output .current-operand {
color: white; /* Sets the color for the current operand */
font-size: 2.5rem; /* Sets the font size for the current operand */
}

0 comments on commit ff3a130

Please sign in to comment.