mirror of
https://github.com/TrentSPalmer/fcc-challenges.git
synced 2024-12-22 06:02:49 -08:00
add calculator-react
This commit is contained in:
parent
09f76acd55
commit
36f827027c
23
calculator-react/.gitignore
vendored
Normal file
23
calculator-react/.gitignore
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
/.pnp
|
||||
.pnp.js
|
||||
|
||||
# testing
|
||||
/coverage
|
||||
|
||||
# production
|
||||
/build
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
80
calculator-react/README.md
Normal file
80
calculator-react/README.md
Normal file
@ -0,0 +1,80 @@
|
||||
### Calculator-React Project Links
|
||||
|
||||
* [GitHub Page View of this Repo](https://trentspalmer.github.io/fcc-challenges)
|
||||
* [GitHub Page View of this Project (drum-machine-react)](https://trentspalmer.github.io/fcc-challenges/calculator-react/build)
|
||||
|
||||
### FreeCodeCamp Links
|
||||
|
||||
* [FreeCodeCamp Instruction for This Project](https://www.freecodecamp.org/learn/front-end-libraries/front-end-libraries-projects/build-a-javascript-calculator)
|
||||
* [FreeCodeCamp Section](https://www.freecodecamp.org/learn/front-end-libraries/front-end-libraries-projects)
|
||||
|
||||
---
|
||||
|
||||
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
|
||||
|
||||
## Available Scripts
|
||||
|
||||
In the project directory, you can run:
|
||||
|
||||
### `npm start`
|
||||
|
||||
Runs the app in the development mode.<br />
|
||||
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
|
||||
|
||||
The page will reload if you make edits.<br />
|
||||
You will also see any lint errors in the console.
|
||||
|
||||
### `npm test`
|
||||
|
||||
Launches the test runner in the interactive watch mode.<br />
|
||||
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
|
||||
|
||||
### `npm run build`
|
||||
|
||||
Builds the app for production to the `build` folder.<br />
|
||||
It correctly bundles React in production mode and optimizes the build for the best performance.
|
||||
|
||||
The build is minified and the filenames include the hashes.<br />
|
||||
Your app is ready to be deployed!
|
||||
|
||||
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
|
||||
|
||||
### `npm run eject`
|
||||
|
||||
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
|
||||
|
||||
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
|
||||
|
||||
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
|
||||
|
||||
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
|
||||
|
||||
## Learn More
|
||||
|
||||
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
|
||||
|
||||
To learn React, check out the [React documentation](https://reactjs.org/).
|
||||
|
||||
### Code Splitting
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
|
||||
|
||||
### Analyzing the Bundle Size
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
|
||||
|
||||
### Making a Progressive Web App
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
|
||||
|
||||
### Advanced Configuration
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
|
||||
|
||||
### Deployment
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
|
||||
|
||||
### `npm run build` fails to minify
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
|
14062
calculator-react/package-lock.json
generated
Normal file
14062
calculator-react/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
40
calculator-react/package.json
Normal file
40
calculator-react/package.json
Normal file
@ -0,0 +1,40 @@
|
||||
{
|
||||
"name": "calculator-react",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"homepage": "https://trentspalmer.github.io/fcc-challenges/calculator-react/build",
|
||||
"dependencies": {
|
||||
"@fortawesome/fontawesome-svg-core": "^1.2.28",
|
||||
"@fortawesome/free-solid-svg-icons": "^5.13.0",
|
||||
"@fortawesome/react-fontawesome": "^0.1.10",
|
||||
"@testing-library/jest-dom": "^4.2.4",
|
||||
"@testing-library/react": "^9.5.0",
|
||||
"@testing-library/user-event": "^7.2.1",
|
||||
"react": "^16.13.1",
|
||||
"react-dom": "^16.13.1",
|
||||
"react-redux": "^7.2.0",
|
||||
"react-scripts": "3.4.1",
|
||||
"redux": "^4.0.5"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "react-scripts start",
|
||||
"build": "react-scripts build",
|
||||
"test": "react-scripts test",
|
||||
"eject": "react-scripts eject"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"extends": "react-app"
|
||||
},
|
||||
"browserslist": {
|
||||
"production": [
|
||||
">0.2%",
|
||||
"not dead",
|
||||
"not op_mini all"
|
||||
],
|
||||
"development": [
|
||||
"last 1 chrome version",
|
||||
"last 1 firefox version",
|
||||
"last 1 safari version"
|
||||
]
|
||||
}
|
||||
}
|
50
calculator-react/public/index.html
Normal file
50
calculator-react/public/index.html
Normal file
@ -0,0 +1,50 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<link rel="icon" href="#" />
|
||||
<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" />
|
||||
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Baloo+Bhai" />
|
||||
<!--
|
||||
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>
|
||||
Calculator React - Build a Calculator - Front End Libraries Projects
|
||||
</title>
|
||||
</head>
|
||||
<body>
|
||||
<noscript>You need to enable JavaScript to run this app.</noscript>
|
||||
<div id="root">
|
||||
<div>
|
||||
<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
|
||||
</div>
|
||||
</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>
|
25
calculator-react/public/manifest.json
Normal file
25
calculator-react/public/manifest.json
Normal file
@ -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
calculator-react/public/robots.txt
Normal file
3
calculator-react/public/robots.txt
Normal file
@ -0,0 +1,3 @@
|
||||
# https://www.robotstxt.org/robotstxt.html
|
||||
User-agent: *
|
||||
Disallow:
|
78
calculator-react/src/Add.js
Normal file
78
calculator-react/src/Add.js
Normal file
@ -0,0 +1,78 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||
import { faPlus } from "@fortawesome/free-solid-svg-icons";
|
||||
|
||||
import { colors,makeInputAndOperationsStrings } from './globals'
|
||||
import { inputStringAction } from "./actions/inputStringAction";
|
||||
import { operationStringAction } from "./actions/operationStringAction";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
inputStringAction: (inputString) => dispatch(inputStringAction(inputString)),
|
||||
operationStringAction: (operationString) => dispatch(operationStringAction(operationString)),
|
||||
});
|
||||
|
||||
class Add extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleMouseEvent = this.handleMouseEvent.bind(this);
|
||||
this.state = {
|
||||
backgroundColor: colors.fourthColor,
|
||||
};
|
||||
};
|
||||
|
||||
handleClick() {
|
||||
const [inputString,operationString] = makeInputAndOperationsStrings(this.props.inputString,this.props.operationString,'+');
|
||||
if (inputString !== this.props.inputString) {
|
||||
this.props.inputStringAction(inputString);
|
||||
}
|
||||
if (operationString !== this.props.operationString) {
|
||||
this.props.operationStringAction(operationString);
|
||||
}
|
||||
}
|
||||
|
||||
handleMouseEvent(e) {
|
||||
if (e.type === 'mousedown' || e.type === 'touchstart') {
|
||||
this.setState({backgroundColor: colors.firstColor});
|
||||
} else if (e.type === 'mouseup' || e.type === 'touchend') {
|
||||
this.setState({backgroundColor: colors.fourthColor});
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
gridColumnStart: 5,
|
||||
gridColumnEnd: 6,
|
||||
gridRowStart: 4,
|
||||
gridRowEnd: 5,
|
||||
borderRight: '3px solid black',
|
||||
display: 'flex',
|
||||
flexDirection: 'row',
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
backgroundColor: this.state.backgroundColor,
|
||||
};
|
||||
|
||||
if (height > width) {
|
||||
style['gridColumnStart'] = 4;
|
||||
style['gridColumnEnd'] = 5;
|
||||
style['borderRight'] = 'unset';
|
||||
style['borderBottom'] = '3px solid black';
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="add" style={style} className='clickable' onClick={this.handleClick}
|
||||
onTouchStart={this.handleMouseEvent} onTouchEnd={this.handleMouseEvent}
|
||||
onMouseDown={this.handleMouseEvent} onMouseUp={this.handleMouseEvent}>
|
||||
<FontAwesomeIcon icon={faPlus}/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(Add);
|
3
calculator-react/src/App.css
Normal file
3
calculator-react/src/App.css
Normal file
@ -0,0 +1,3 @@
|
||||
.App {
|
||||
text-align: center;
|
||||
}
|
94
calculator-react/src/App.js
Normal file
94
calculator-react/src/App.js
Normal file
@ -0,0 +1,94 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import Calculator from './Calculator';
|
||||
import { colors } from './globals'
|
||||
|
||||
import { innerWindowWidthAction } from "./actions/innerWindowWidthAction";
|
||||
import { innerWindowHeightAction } from "./actions/innerWindowHeightAction";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
innerWindowWidthAction: (windowInnerWidth) => dispatch(innerWindowWidthAction(windowInnerWidth)),
|
||||
innerWindowHeightAction: (windowInnerHeight) => dispatch(innerWindowHeightAction(windowInnerHeight)),
|
||||
});
|
||||
|
||||
class App extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.handleResize = this.handleResize.bind(this);
|
||||
};
|
||||
|
||||
handleResize() {
|
||||
this.props.innerWindowWidthAction(window.innerWidth);
|
||||
this.props.innerWindowHeightAction(window.innerHeight);
|
||||
};
|
||||
|
||||
componentDidMount() {
|
||||
window.addEventListener('resize',this.handleResize);
|
||||
};
|
||||
|
||||
componentWillUnmount() {
|
||||
window.removeEventListener('resize',this.handleResize);
|
||||
};
|
||||
|
||||
render() {
|
||||
const height = this.props.innerWindowHeight;
|
||||
const width = this.props.innerWindowWidth;
|
||||
const gitHubLabelStyle = {
|
||||
backgroundColor: colors.firstColor,
|
||||
border: 'none',
|
||||
width: 149,
|
||||
height: 0,
|
||||
position: 'absolute',
|
||||
right: 0,
|
||||
};
|
||||
|
||||
const attachmentStyle = {
|
||||
position: 'absolute',
|
||||
right: 0,
|
||||
height: 149,
|
||||
width: 149,
|
||||
};
|
||||
|
||||
const style = {
|
||||
textAlign: 'center',
|
||||
backgroundColor: colors.firstColor,
|
||||
height: '100vh',
|
||||
width: '100vw',
|
||||
display: 'flex',
|
||||
color: colors.textColor,
|
||||
overFlow: 'hidden',
|
||||
};
|
||||
|
||||
if (width > height && height < 400) {
|
||||
gitHubLabelStyle['width'] = 99;
|
||||
attachmentStyle['width'] = 99;
|
||||
attachmentStyle['height'] = 99;
|
||||
} else if (height > width) {
|
||||
gitHubLabelStyle['width'] = 99;
|
||||
attachmentStyle['width'] = 99;
|
||||
attachmentStyle['height'] = 99;
|
||||
}
|
||||
|
||||
return (
|
||||
<div style={style}>
|
||||
<a href="https://github.com/TrentSPalmer/fcc-challenges/tree/gh-pages/calculator-react"
|
||||
style={gitHubLabelStyle}
|
||||
target="_blank" rel="noopener noreferrer">
|
||||
<img
|
||||
src="https://github.blog/wp-content/uploads/2008/12/forkme_right_white_ffffff.png?resize=149%2C149"
|
||||
className="size-full"
|
||||
style={attachmentStyle}
|
||||
alt="Fork me on GitHub"
|
||||
data-recalc-dims="1">
|
||||
</img>
|
||||
</a>
|
||||
<Calculator />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(App);
|
9
calculator-react/src/App.test.js
Normal file
9
calculator-react/src/App.test.js
Normal file
@ -0,0 +1,9 @@
|
||||
import React from 'react';
|
||||
import { render } from '@testing-library/react';
|
||||
import App from './App';
|
||||
|
||||
test('renders learn react link', () => {
|
||||
const { getByText } = render(<App />);
|
||||
const linkElement = getByText(/learn react/i);
|
||||
expect(linkElement).toBeInTheDocument();
|
||||
});
|
79
calculator-react/src/Back.js
Normal file
79
calculator-react/src/Back.js
Normal file
@ -0,0 +1,79 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||
import { faBackspace } from "@fortawesome/free-solid-svg-icons";
|
||||
|
||||
import { colors,makeInputAndOperationsStrings } from './globals'
|
||||
import { inputStringAction } from "./actions/inputStringAction";
|
||||
import { operationStringAction } from "./actions/operationStringAction";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
inputStringAction: (inputString) => dispatch(inputStringAction(inputString)),
|
||||
operationStringAction: (operationString) => dispatch(operationStringAction(operationString)),
|
||||
});
|
||||
|
||||
class Back extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleMouseEvent = this.handleMouseEvent.bind(this);
|
||||
this.state = {
|
||||
backgroundColor: colors.fourthColor,
|
||||
};
|
||||
};
|
||||
|
||||
handleClick() {
|
||||
const [inputString,operationString] = makeInputAndOperationsStrings(this.props.inputString,this.props.operationString,'b');
|
||||
if (inputString !== this.props.inputString) {
|
||||
this.props.inputStringAction(inputString);
|
||||
}
|
||||
if (operationString !== this.props.operationString) {
|
||||
this.props.operationStringAction(operationString);
|
||||
}
|
||||
}
|
||||
|
||||
handleMouseEvent(e) {
|
||||
if (e.type === 'mousedown' || e.type === 'touchstart') {
|
||||
this.setState({backgroundColor: colors.firstColor});
|
||||
} else if (e.type === 'mouseup' || e.type === 'touchend') {
|
||||
this.setState({backgroundColor: colors.fourthColor});
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
gridColumnStart: 6,
|
||||
gridColumnEnd: 7,
|
||||
gridRowStart: 3,
|
||||
gridRowEnd: 4,
|
||||
borderBottom: '3px solid black',
|
||||
display: 'flex',
|
||||
flexDirection: 'row',
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
backgroundColor: this.state.backgroundColor,
|
||||
};
|
||||
|
||||
if (height > width) {
|
||||
style['gridRowStart'] = 2;
|
||||
style['gridRowEnd'] = 3;
|
||||
style['gridColumnStart'] = 2;
|
||||
style['gridColumnEnd'] = 3;
|
||||
style['borderRight'] = '3px solid black';
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="subtract" style={style} className='clickable' onClick={this.handleClick}
|
||||
onTouchStart={this.handleMouseEvent} onTouchEnd={this.handleMouseEvent}
|
||||
onMouseDown={this.handleMouseEvent} onMouseUp={this.handleMouseEvent}>
|
||||
<FontAwesomeIcon icon={faBackspace}/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(Back);
|
104
calculator-react/src/Calculator.js
Normal file
104
calculator-react/src/Calculator.js
Normal file
@ -0,0 +1,104 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import { colors,makeInputAndOperationsStrings } from './globals'
|
||||
import CalculatorGrid from './CalculatorGrid';
|
||||
import { inputStringAction } from "./actions/inputStringAction";
|
||||
import { operationStringAction } from "./actions/operationStringAction";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
inputStringAction: (inputString) => dispatch(inputStringAction(inputString)),
|
||||
operationStringAction: (operationString) => dispatch(operationStringAction(operationString)),
|
||||
});
|
||||
|
||||
class Calculator extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.handleKeyPress = this.handleKeyPress.bind(this);
|
||||
this.handleKey= this.handleKey.bind(this);
|
||||
};
|
||||
|
||||
handleKey(key) {
|
||||
const [inputString,operationString] = makeInputAndOperationsStrings(this.props.inputString,this.props.operationString,key);
|
||||
if (inputString !== this.props.inputString) {
|
||||
this.props.inputStringAction(inputString);
|
||||
}
|
||||
if (operationString !== this.props.operationString) {
|
||||
this.props.operationStringAction(operationString);
|
||||
}
|
||||
}
|
||||
|
||||
handleKeyPress(e) {
|
||||
if (e.keyCode === 8 || e.keyCode === 46) {
|
||||
this.handleKey('b');
|
||||
} else if (['0','1','2','3','4','5','6','7','8','9','.','*','+','-','='].includes(e.key)) {
|
||||
this.handleKey(e.key);
|
||||
} else if (e.key === '/') {
|
||||
e.preventDefault();
|
||||
this.handleKey(e.key);
|
||||
} else if (e.keyCode === 13) {
|
||||
this.handleKey('=');
|
||||
} else if (e.keyCode === 27) {
|
||||
this.props.inputStringAction('');
|
||||
this.props.operationStringAction('');
|
||||
}
|
||||
};
|
||||
|
||||
componentDidMount() {
|
||||
document.addEventListener("keydown", this.handleKeyPress, false);
|
||||
};
|
||||
|
||||
componentWillUnmount() {
|
||||
document.removeEventListener("keydown", this.handleKeyPress, false);
|
||||
};
|
||||
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const topMargin = Math.round(height * 0.1);
|
||||
const sideMargin = Math.round(width * 0.1);
|
||||
const style = {
|
||||
height: '80%',
|
||||
width: '80%',
|
||||
backgroundColor: colors.secondColor,
|
||||
borderRadius: 10,
|
||||
marginTop: topMargin,
|
||||
marginBottom: topMargin,
|
||||
marginLeft: sideMargin,
|
||||
marginRight: sideMargin,
|
||||
overFlow: 'hidden',
|
||||
border: '3px solid black',
|
||||
userSelect: 'none',
|
||||
MozUserSelect: 'none',
|
||||
WebkitUserSelect: 'none',
|
||||
zIndex: '1',
|
||||
fontFamily: 'Baloo Bhai',
|
||||
};
|
||||
|
||||
if (width > height && height < 400) {
|
||||
style['height'] = '96vh';
|
||||
style['width'] = '80vw';
|
||||
style['marginTop'] = '1vh';
|
||||
style['marginBottom'] = '3vh';
|
||||
style['marginLeft'] = '89px';
|
||||
style['marginRight'] = '89px';
|
||||
} else if (height > width) {
|
||||
style['height'] = '75vh';
|
||||
style['width'] = '100vw';
|
||||
style['marginTop'] = '12vh';
|
||||
style['marginBottom'] = 'unset';
|
||||
style['marginLeft'] = '0px';
|
||||
style['marginRight'] = '0px';
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="Calculator" style={style}>
|
||||
<CalculatorGrid />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(Calculator);
|
72
calculator-react/src/CalculatorGrid.js
Normal file
72
calculator-react/src/CalculatorGrid.js
Normal file
@ -0,0 +1,72 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import Seven from './Seven';
|
||||
import Eight from './Eight';
|
||||
import Nine from './Nine';
|
||||
import Four from './Four';
|
||||
import Five from './Five';
|
||||
import Six from './Six';
|
||||
import One from './One';
|
||||
import Two from './Two';
|
||||
import Three from './Three';
|
||||
import Zero from './Zero';
|
||||
import Decimal from './Decimal';
|
||||
import Clear from './Clear';
|
||||
import Divide from './Divide';
|
||||
import Multiply from './Multiply';
|
||||
import Subtract from './Subtract';
|
||||
import Add from './Add';
|
||||
import Equals from './Equals';
|
||||
import Display from './Display';
|
||||
import Back from './Back';
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
class CalculatorGrid extends React.Component {
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
display: 'grid',
|
||||
width: '100%',
|
||||
height: '100%',
|
||||
gridTemplateRows: 'repeat(4, 1fr)',
|
||||
gridTemplateColumns: 'repeat(6, 1fr)',
|
||||
fontSize: 64,
|
||||
};
|
||||
|
||||
if (width > height && height < 400) {
|
||||
style['fontSize'] = 48;
|
||||
} else if (height > width) {
|
||||
style['gridTemplateRows'] = 'repeat(6, 1fr)';
|
||||
style['gridTemplateColumns'] = 'repeat(4, 1fr)';
|
||||
style['fontSize'] = 48;
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="CalculatorGrid" style={style}>
|
||||
<Seven />
|
||||
<Eight />
|
||||
<Nine />
|
||||
<Four />
|
||||
<Five />
|
||||
<Six />
|
||||
<One />
|
||||
<Two />
|
||||
<Three />
|
||||
<Zero />
|
||||
<Decimal />
|
||||
<Clear />
|
||||
<Divide />
|
||||
<Multiply />
|
||||
<Subtract />
|
||||
<Add />
|
||||
<Equals />
|
||||
<Display />
|
||||
<Back />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps)(CalculatorGrid);
|
60
calculator-react/src/Clear.js
Normal file
60
calculator-react/src/Clear.js
Normal file
@ -0,0 +1,60 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import { colors } from './globals'
|
||||
import { inputStringAction } from "./actions/inputStringAction";
|
||||
import { operationStringAction } from "./actions/operationStringAction";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
inputStringAction: (inputString) => dispatch(inputStringAction(inputString)),
|
||||
operationStringAction: (operationString) => dispatch(operationStringAction(operationString)),
|
||||
});
|
||||
|
||||
class Clear extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleMouseEvent = this.handleMouseEvent.bind(this);
|
||||
this.state = {
|
||||
backgroundColor: colors.fifthColor,
|
||||
};
|
||||
};
|
||||
|
||||
handleClick() {
|
||||
this.props.inputStringAction('');
|
||||
this.props.operationStringAction('');
|
||||
}
|
||||
|
||||
handleMouseEvent(e) {
|
||||
if (e.type === 'mousedown' || e.type === 'touchstart') {
|
||||
this.setState({backgroundColor: colors.secondColor});
|
||||
} else if (e.type === 'mouseup' || e.type === 'touchend') {
|
||||
this.setState({backgroundColor: colors.fifthColor});
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const style = {
|
||||
gridColumnStart: 1,
|
||||
gridColumnEnd: 2,
|
||||
gridRowStart: 2,
|
||||
gridRowEnd: 3,
|
||||
borderRight: '3px solid black',
|
||||
borderBottom: '3px solid black',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'center',
|
||||
backgroundColor: this.state.backgroundColor,
|
||||
};
|
||||
|
||||
return (
|
||||
<div id="clear" style={style} className='clickable' onClick={this.handleClick}
|
||||
onTouchStart={this.handleMouseEvent} onTouchEnd={this.handleMouseEvent}
|
||||
onMouseDown={this.handleMouseEvent} onMouseUp={this.handleMouseEvent}>AC</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(Clear);
|
75
calculator-react/src/Decimal.js
Normal file
75
calculator-react/src/Decimal.js
Normal file
@ -0,0 +1,75 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import { colors,makeInputAndOperationsStrings } from './globals'
|
||||
import { inputStringAction } from "./actions/inputStringAction";
|
||||
import { operationStringAction } from "./actions/operationStringAction";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
inputStringAction: (inputString) => dispatch(inputStringAction(inputString)),
|
||||
operationStringAction: (operationString) => dispatch(operationStringAction(operationString)),
|
||||
});
|
||||
|
||||
class Decimal extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleMouseEvent = this.handleMouseEvent.bind(this);
|
||||
this.state = {
|
||||
backgroundColor: colors.secondColor,
|
||||
};
|
||||
};
|
||||
|
||||
handleClick() {
|
||||
const [inputString,operationString] = makeInputAndOperationsStrings(this.props.inputString,this.props.operationString,'.');
|
||||
if (inputString !== this.props.inputString) {
|
||||
this.props.inputStringAction(inputString);
|
||||
}
|
||||
if (operationString !== this.props.operationString) {
|
||||
this.props.operationStringAction(operationString);
|
||||
}
|
||||
}
|
||||
|
||||
handleMouseEvent(e) {
|
||||
if (e.type === 'mousedown' || e.type === 'touchstart') {
|
||||
this.setState({backgroundColor: colors.firstColor});
|
||||
} else if (e.type === 'mouseup' || e.type === 'touchend') {
|
||||
this.setState({backgroundColor: colors.secondColor});
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
gridColumnStart: 1,
|
||||
gridColumnEnd: 2,
|
||||
gridRowStart: 3,
|
||||
gridRowEnd: 4,
|
||||
borderRight: '3px solid black',
|
||||
borderBottom: '3px solid black',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'center',
|
||||
backgroundColor: this.state.backgroundColor,
|
||||
};
|
||||
|
||||
if (height > width) {
|
||||
style['gridRowStart'] = 6;
|
||||
style['gridRowEnd'] = 7;
|
||||
style['gridColumnStart'] = 3;
|
||||
style['gridColumnEnd'] = 4;
|
||||
style['borderBottom'] = 'unset';
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="decimal" style={style} className='clickable' onClick={this.handleClick}
|
||||
onTouchStart={this.handleMouseEvent} onTouchEnd={this.handleMouseEvent}
|
||||
onMouseDown={this.handleMouseEvent} onMouseUp={this.handleMouseEvent}>.</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(Decimal);
|
40
calculator-react/src/Display.js
Normal file
40
calculator-react/src/Display.js
Normal file
@ -0,0 +1,40 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import { colors } from './globals'
|
||||
import DisplayInput from './DisplayInput';
|
||||
import DisplayOperations from './DisplayOperations';
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
class Display extends React.Component {
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
gridColumnStart: 1,
|
||||
gridColumnEnd: 7,
|
||||
gridRowStart: 1,
|
||||
gridRowEnd: 2,
|
||||
borderBottom: '3px solid black',
|
||||
backgroundColor: colors.textColor,
|
||||
borderRadius: '7px 7px 0 0',
|
||||
color: colors.firstColor,
|
||||
textOverflow: 'clip',
|
||||
whiteSpace: 'nowrap',
|
||||
overflow: 'hidden',
|
||||
};
|
||||
|
||||
if (height > width) {
|
||||
style['gridColumnEnd'] = 5;
|
||||
}
|
||||
|
||||
return (
|
||||
<div style={style}>
|
||||
<DisplayOperations />
|
||||
<DisplayInput />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps)(Display);
|
34
calculator-react/src/DisplayInput.js
Normal file
34
calculator-react/src/DisplayInput.js
Normal file
@ -0,0 +1,34 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
class DisplayInput extends React.Component {
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'center',
|
||||
textAlign: 'right',
|
||||
height: '60%',
|
||||
fontSize: 64,
|
||||
lineHeight: '100%',
|
||||
};
|
||||
|
||||
if (width > height && height < 400) {
|
||||
style['fontSize'] = 36;
|
||||
} else if (height > width && height < 850) {
|
||||
style['fontSize'] = 36;
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="display" style={style}>{
|
||||
this.props.inputString.length === 0 ? 0 : this.props.inputString
|
||||
}</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps)(DisplayInput);
|
36
calculator-react/src/DisplayOperations.js
Normal file
36
calculator-react/src/DisplayOperations.js
Normal file
@ -0,0 +1,36 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
import { colors } from './globals'
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
class DisplayOperations extends React.Component {
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'center',
|
||||
textAlign: 'right',
|
||||
height: '40%',
|
||||
fontSize: 48,
|
||||
lineHeight: '100%',
|
||||
color: colors.secondtextColor,
|
||||
};
|
||||
|
||||
if (width > height && height < 400) {
|
||||
style['fontSize'] = 27;
|
||||
} else if (height > width && height < 850) {
|
||||
style['fontSize'] = 27;
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="displayOperations" style={style}>{
|
||||
this.props.operationString.length === 0 ? 0 : this.props.operationString
|
||||
}</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps)(DisplayOperations);
|
77
calculator-react/src/Divide.js
Normal file
77
calculator-react/src/Divide.js
Normal file
@ -0,0 +1,77 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||
import { faDivide } from "@fortawesome/free-solid-svg-icons";
|
||||
|
||||
import { colors,makeInputAndOperationsStrings } from './globals'
|
||||
import { inputStringAction } from "./actions/inputStringAction";
|
||||
import { operationStringAction } from "./actions/operationStringAction";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
inputStringAction: (inputString) => dispatch(inputStringAction(inputString)),
|
||||
operationStringAction: (operationString) => dispatch(operationStringAction(operationString)),
|
||||
});
|
||||
|
||||
class Divide extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleMouseEvent = this.handleMouseEvent.bind(this);
|
||||
this.state = {
|
||||
backgroundColor: colors.fourthColor,
|
||||
};
|
||||
};
|
||||
|
||||
handleClick() {
|
||||
const [inputString,operationString] = makeInputAndOperationsStrings(this.props.inputString,this.props.operationString,'/');
|
||||
if (inputString !== this.props.inputString) {
|
||||
this.props.inputStringAction(inputString);
|
||||
}
|
||||
if (operationString !== this.props.operationString) {
|
||||
this.props.operationStringAction(operationString);
|
||||
}
|
||||
}
|
||||
|
||||
handleMouseEvent(e) {
|
||||
if (e.type === 'mousedown' || e.type === 'touchstart') {
|
||||
this.setState({backgroundColor: colors.firstColor});
|
||||
} else if (e.type === 'mouseup' || e.type === 'touchend') {
|
||||
this.setState({backgroundColor: colors.fourthColor});
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
gridColumnStart: 5,
|
||||
gridColumnEnd: 6,
|
||||
gridRowStart: 2,
|
||||
gridRowEnd: 3,
|
||||
borderBottom: '3px solid black',
|
||||
borderRight: '3px solid black',
|
||||
display: 'flex',
|
||||
flexDirection: 'row',
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
backgroundColor: this.state.backgroundColor,
|
||||
};
|
||||
|
||||
if (height > width) {
|
||||
style['gridColumnStart'] = 3;
|
||||
style['gridColumnEnd'] = 4;
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="divide" style={style} className='clickable' onClick={this.handleClick}
|
||||
onTouchStart={this.handleMouseEvent} onTouchEnd={this.handleMouseEvent}
|
||||
onMouseDown={this.handleMouseEvent} onMouseUp={this.handleMouseEvent}>
|
||||
<FontAwesomeIcon icon={faDivide}/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(Divide);
|
74
calculator-react/src/Eight.js
Normal file
74
calculator-react/src/Eight.js
Normal file
@ -0,0 +1,74 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import { colors,makeInputAndOperationsStrings } from './globals'
|
||||
import { inputStringAction } from "./actions/inputStringAction";
|
||||
import { operationStringAction } from "./actions/operationStringAction";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
inputStringAction: (inputString) => dispatch(inputStringAction(inputString)),
|
||||
operationStringAction: (operationString) => dispatch(operationStringAction(operationString)),
|
||||
});
|
||||
|
||||
class Eight extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleMouseEvent = this.handleMouseEvent.bind(this);
|
||||
this.state = {
|
||||
backgroundColor: colors.secondColor,
|
||||
};
|
||||
};
|
||||
|
||||
handleClick() {
|
||||
const [inputString,operationString] = makeInputAndOperationsStrings(this.props.inputString,this.props.operationString,'8');
|
||||
if (inputString !== this.props.inputString) {
|
||||
this.props.inputStringAction(inputString);
|
||||
}
|
||||
if (operationString !== this.props.operationString) {
|
||||
this.props.operationStringAction(operationString);
|
||||
}
|
||||
}
|
||||
|
||||
handleMouseEvent(e) {
|
||||
if (e.type === 'mousedown' || e.type === 'touchstart') {
|
||||
this.setState({backgroundColor: colors.firstColor});
|
||||
} else if (e.type === 'mouseup' || e.type === 'touchend') {
|
||||
this.setState({backgroundColor: colors.secondColor});
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
gridColumnStart: 3,
|
||||
gridColumnEnd: 4,
|
||||
gridRowStart: 2,
|
||||
gridRowEnd: 3,
|
||||
borderBottom: '3px solid black',
|
||||
borderRight: '3px solid black',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'center',
|
||||
backgroundColor: this.state.backgroundColor,
|
||||
};
|
||||
|
||||
if (height > width) {
|
||||
style['gridRowStart'] = 3;
|
||||
style['gridRowEnd'] = 4;
|
||||
style['gridColumnStart'] = 2;
|
||||
style['gridColumnEnd'] = 3;
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="eight" style={style} className='clickable' onClick={this.handleClick}
|
||||
onTouchStart={this.handleMouseEvent} onTouchEnd={this.handleMouseEvent}
|
||||
onMouseDown={this.handleMouseEvent} onMouseUp={this.handleMouseEvent}>8</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(Eight);
|
77
calculator-react/src/Equals.js
Normal file
77
calculator-react/src/Equals.js
Normal file
@ -0,0 +1,77 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import { colors,makeInputAndOperationsStrings } from './globals'
|
||||
import { inputStringAction } from "./actions/inputStringAction";
|
||||
import { operationStringAction } from "./actions/operationStringAction";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
inputStringAction: (inputString) => dispatch(inputStringAction(inputString)),
|
||||
operationStringAction: (operationString) => dispatch(operationStringAction(operationString)),
|
||||
});
|
||||
|
||||
class Equals extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleMouseEvent = this.handleMouseEvent.bind(this);
|
||||
this.state = {
|
||||
backgroundColor: colors.thirdColor,
|
||||
};
|
||||
};
|
||||
|
||||
handleClick() {
|
||||
const [inputString,operationString] = makeInputAndOperationsStrings(this.props.inputString,this.props.operationString,'=');
|
||||
if (inputString !== this.props.inputString) {
|
||||
this.props.inputStringAction(inputString);
|
||||
}
|
||||
if (operationString !== this.props.operationString) {
|
||||
this.props.operationStringAction(operationString);
|
||||
}
|
||||
}
|
||||
|
||||
handleMouseEvent(e) {
|
||||
if (e.type === 'mousedown' || e.type === 'touchstart') {
|
||||
this.setState({backgroundColor: colors.firstColor});
|
||||
} else if (e.type === 'mouseup' || e.type === 'touchend') {
|
||||
this.setState({backgroundColor: colors.thirdColor});
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
gridColumnStart: 6,
|
||||
gridColumnEnd: 7,
|
||||
gridRowStart: 4,
|
||||
gridRowEnd: 5,
|
||||
display: 'flex',
|
||||
flexDirection: 'row',
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
backgroundColor: this.state.backgroundColor,
|
||||
borderRadius: '0 0 7px 0',
|
||||
fontSize: 88,
|
||||
};
|
||||
|
||||
if (width > height && height < 400) {
|
||||
style['fontSize'] = 56;
|
||||
} else if (height > width) {
|
||||
style['gridColumnStart'] = 4;
|
||||
style['gridColumnEnd'] = 5;
|
||||
style['gridRowStart'] = 5;
|
||||
style['gridRowEnd'] = 7;
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="equals" style={style} className='clickable' onClick={this.handleClick}
|
||||
onTouchStart={this.handleMouseEvent} onTouchEnd={this.handleMouseEvent}
|
||||
onMouseDown={this.handleMouseEvent} onMouseUp={this.handleMouseEvent}>=</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(Equals);
|
74
calculator-react/src/Five.js
Normal file
74
calculator-react/src/Five.js
Normal file
@ -0,0 +1,74 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import { colors,makeInputAndOperationsStrings } from './globals'
|
||||
import { inputStringAction } from "./actions/inputStringAction";
|
||||
import { operationStringAction } from "./actions/operationStringAction";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
inputStringAction: (inputString) => dispatch(inputStringAction(inputString)),
|
||||
operationStringAction: (operationString) => dispatch(operationStringAction(operationString)),
|
||||
});
|
||||
|
||||
class Five extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleMouseEvent = this.handleMouseEvent.bind(this);
|
||||
this.state = {
|
||||
backgroundColor: colors.secondColor,
|
||||
};
|
||||
};
|
||||
|
||||
handleClick() {
|
||||
const [inputString,operationString] = makeInputAndOperationsStrings(this.props.inputString,this.props.operationString,'5');
|
||||
if (inputString !== this.props.inputString) {
|
||||
this.props.inputStringAction(inputString);
|
||||
}
|
||||
if (operationString !== this.props.operationString) {
|
||||
this.props.operationStringAction(operationString);
|
||||
}
|
||||
}
|
||||
|
||||
handleMouseEvent(e) {
|
||||
if (e.type === 'mousedown' || e.type === 'touchstart') {
|
||||
this.setState({backgroundColor: colors.firstColor});
|
||||
} else if (e.type === 'mouseup' || e.type === 'touchend') {
|
||||
this.setState({backgroundColor: colors.secondColor});
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
gridColumnStart: 3,
|
||||
gridColumnEnd: 4,
|
||||
gridRowStart: 3,
|
||||
gridRowEnd: 4,
|
||||
borderBottom: '3px solid black',
|
||||
borderRight: '3px solid black',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'center',
|
||||
backgroundColor: this.state.backgroundColor,
|
||||
};
|
||||
|
||||
if (height > width) {
|
||||
style['gridRowStart'] = 4;
|
||||
style['gridRowEnd'] = 5;
|
||||
style['gridColumnStart'] = 2;
|
||||
style['gridColumnEnd'] = 3;
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="five" style={style} className='clickable' onClick={this.handleClick}
|
||||
onTouchStart={this.handleMouseEvent} onTouchEnd={this.handleMouseEvent}
|
||||
onMouseDown={this.handleMouseEvent} onMouseUp={this.handleMouseEvent}>5</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(Five);
|
74
calculator-react/src/Four.js
Normal file
74
calculator-react/src/Four.js
Normal file
@ -0,0 +1,74 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import { colors,makeInputAndOperationsStrings } from './globals'
|
||||
import { inputStringAction } from "./actions/inputStringAction";
|
||||
import { operationStringAction } from "./actions/operationStringAction";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
inputStringAction: (inputString) => dispatch(inputStringAction(inputString)),
|
||||
operationStringAction: (operationString) => dispatch(operationStringAction(operationString)),
|
||||
});
|
||||
|
||||
class Four extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleMouseEvent = this.handleMouseEvent.bind(this);
|
||||
this.state = {
|
||||
backgroundColor: colors.secondColor,
|
||||
};
|
||||
};
|
||||
|
||||
handleClick() {
|
||||
const [inputString,operationString] = makeInputAndOperationsStrings(this.props.inputString,this.props.operationString,'4');
|
||||
if (inputString !== this.props.inputString) {
|
||||
this.props.inputStringAction(inputString);
|
||||
}
|
||||
if (operationString !== this.props.operationString) {
|
||||
this.props.operationStringAction(operationString);
|
||||
}
|
||||
}
|
||||
|
||||
handleMouseEvent(e) {
|
||||
if (e.type === 'mousedown' || e.type === 'touchstart') {
|
||||
this.setState({backgroundColor: colors.firstColor});
|
||||
} else if (e.type === 'mouseup' || e.type === 'touchend') {
|
||||
this.setState({backgroundColor: colors.secondColor});
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
gridColumnStart: 2,
|
||||
gridColumnEnd: 3,
|
||||
gridRowStart: 3,
|
||||
gridRowEnd: 4,
|
||||
borderBottom: '3px solid black',
|
||||
borderRight: '3px solid black',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'center',
|
||||
backgroundColor: this.state.backgroundColor,
|
||||
};
|
||||
|
||||
if (height > width) {
|
||||
style['gridRowStart'] = 4;
|
||||
style['gridRowEnd'] = 5;
|
||||
style['gridColumnStart'] = 1;
|
||||
style['gridColumnEnd'] = 2;
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="four" style={style} className='clickable' onClick={this.handleClick}
|
||||
onTouchStart={this.handleMouseEvent} onTouchEnd={this.handleMouseEvent}
|
||||
onMouseDown={this.handleMouseEvent} onMouseUp={this.handleMouseEvent}>4</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(Four);
|
76
calculator-react/src/Multiply.js
Normal file
76
calculator-react/src/Multiply.js
Normal file
@ -0,0 +1,76 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||
import { faTimes } from "@fortawesome/free-solid-svg-icons";
|
||||
|
||||
import { colors,makeInputAndOperationsStrings } from './globals'
|
||||
import { inputStringAction } from "./actions/inputStringAction";
|
||||
import { operationStringAction } from "./actions/operationStringAction";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
inputStringAction: (inputString) => dispatch(inputStringAction(inputString)),
|
||||
operationStringAction: (operationString) => dispatch(operationStringAction(operationString)),
|
||||
});
|
||||
|
||||
class Multiply extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleMouseEvent = this.handleMouseEvent.bind(this);
|
||||
this.state = {
|
||||
backgroundColor: colors.fourthColor,
|
||||
};
|
||||
};
|
||||
|
||||
handleClick() {
|
||||
const [inputString,operationString] = makeInputAndOperationsStrings(this.props.inputString,this.props.operationString,'*');
|
||||
if (inputString !== this.props.inputString) {
|
||||
this.props.inputStringAction(inputString);
|
||||
}
|
||||
if (operationString !== this.props.operationString) {
|
||||
this.props.operationStringAction(operationString);
|
||||
}
|
||||
}
|
||||
|
||||
handleMouseEvent(e) {
|
||||
if (e.type === 'mousedown' || e.type === 'touchstart') {
|
||||
this.setState({backgroundColor: colors.firstColor});
|
||||
} else if (e.type === 'mouseup' || e.type === 'touchend') {
|
||||
this.setState({backgroundColor: colors.fourthColor});
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
gridColumnStart: 6,
|
||||
gridColumnEnd: 7,
|
||||
gridRowStart: 2,
|
||||
gridRowEnd: 3,
|
||||
borderBottom: '3px solid black',
|
||||
display: 'flex',
|
||||
flexDirection: 'row',
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
backgroundColor: this.state.backgroundColor,
|
||||
};
|
||||
|
||||
if (height > width) {
|
||||
style['gridColumnStart'] = 4;
|
||||
style['gridColumnEnd'] = 5;
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="multiply" style={style} className='clickable' onClick={this.handleClick}
|
||||
onTouchStart={this.handleMouseEvent} onTouchEnd={this.handleMouseEvent}
|
||||
onMouseDown={this.handleMouseEvent} onMouseUp={this.handleMouseEvent}>
|
||||
<FontAwesomeIcon icon={faTimes}/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(Multiply);
|
74
calculator-react/src/Nine.js
Normal file
74
calculator-react/src/Nine.js
Normal file
@ -0,0 +1,74 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import { colors,makeInputAndOperationsStrings } from './globals'
|
||||
import { inputStringAction } from "./actions/inputStringAction";
|
||||
import { operationStringAction } from "./actions/operationStringAction";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
inputStringAction: (inputString) => dispatch(inputStringAction(inputString)),
|
||||
operationStringAction: (operationString) => dispatch(operationStringAction(operationString)),
|
||||
});
|
||||
|
||||
class Nine extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleMouseEvent = this.handleMouseEvent.bind(this);
|
||||
this.state = {
|
||||
backgroundColor: colors.secondColor,
|
||||
};
|
||||
};
|
||||
|
||||
handleClick() {
|
||||
const [inputString,operationString] = makeInputAndOperationsStrings(this.props.inputString,this.props.operationString,'9');
|
||||
if (inputString !== this.props.inputString) {
|
||||
this.props.inputStringAction(inputString);
|
||||
}
|
||||
if (operationString !== this.props.operationString) {
|
||||
this.props.operationStringAction(operationString);
|
||||
}
|
||||
}
|
||||
|
||||
handleMouseEvent(e) {
|
||||
if (e.type === 'mousedown' || e.type === 'touchstart') {
|
||||
this.setState({backgroundColor: colors.firstColor});
|
||||
} else if (e.type === 'mouseup' || e.type === 'touchend') {
|
||||
this.setState({backgroundColor: colors.secondColor});
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
gridColumnStart: 4,
|
||||
gridColumnEnd: 5,
|
||||
gridRowStart: 2,
|
||||
gridRowEnd: 3,
|
||||
borderBottom: '3px solid black',
|
||||
borderRight: '3px solid black',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'center',
|
||||
backgroundColor: this.state.backgroundColor,
|
||||
};
|
||||
|
||||
if (height > width) {
|
||||
style['gridRowStart'] = 3;
|
||||
style['gridRowEnd'] = 4;
|
||||
style['gridColumnStart'] = 3;
|
||||
style['gridColumnEnd'] = 4;
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="nine" style={style} className='clickable' onClick={this.handleClick}
|
||||
onTouchStart={this.handleMouseEvent} onTouchEnd={this.handleMouseEvent}
|
||||
onMouseDown={this.handleMouseEvent} onMouseUp={this.handleMouseEvent}>9</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(Nine);
|
74
calculator-react/src/One.js
Normal file
74
calculator-react/src/One.js
Normal file
@ -0,0 +1,74 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import { colors,makeInputAndOperationsStrings } from './globals'
|
||||
import { inputStringAction } from "./actions/inputStringAction";
|
||||
import { operationStringAction } from "./actions/operationStringAction";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
inputStringAction: (inputString) => dispatch(inputStringAction(inputString)),
|
||||
operationStringAction: (operationString) => dispatch(operationStringAction(operationString)),
|
||||
});
|
||||
|
||||
class One extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleMouseEvent = this.handleMouseEvent.bind(this);
|
||||
this.state = {
|
||||
backgroundColor: colors.secondColor,
|
||||
};
|
||||
};
|
||||
|
||||
handleClick() {
|
||||
const [inputString,operationString] = makeInputAndOperationsStrings(this.props.inputString,this.props.operationString,'1');
|
||||
if (inputString !== this.props.inputString) {
|
||||
this.props.inputStringAction(inputString);
|
||||
}
|
||||
if (operationString !== this.props.operationString) {
|
||||
this.props.operationStringAction(operationString);
|
||||
}
|
||||
}
|
||||
|
||||
handleMouseEvent(e) {
|
||||
if (e.type === 'mousedown' || e.type === 'touchstart') {
|
||||
this.setState({backgroundColor: colors.firstColor});
|
||||
} else if (e.type === 'mouseup' || e.type === 'touchend') {
|
||||
this.setState({backgroundColor: colors.secondColor});
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
gridColumnStart: 2,
|
||||
gridColumnEnd: 3,
|
||||
gridRowStart: 4,
|
||||
gridRowEnd: 5,
|
||||
borderRight: '3px solid black',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'center',
|
||||
backgroundColor: this.state.backgroundColor,
|
||||
};
|
||||
|
||||
if (height > width) {
|
||||
style['gridRowStart'] = 5;
|
||||
style['gridRowEnd'] = 6;
|
||||
style['borderBottom'] = '3px solid black';
|
||||
style['gridColumnStart'] = 1;
|
||||
style['gridColumnEnd'] = 2;
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="one" style={style} className='clickable' onClick={this.handleClick}
|
||||
onTouchStart={this.handleMouseEvent} onTouchEnd={this.handleMouseEvent}
|
||||
onMouseDown={this.handleMouseEvent} onMouseUp={this.handleMouseEvent}>1</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(One);
|
34
calculator-react/src/Plus.js
Normal file
34
calculator-react/src/Plus.js
Normal file
@ -0,0 +1,34 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
class Minus extends React.Component {
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
gridColumnStart: 5,
|
||||
gridColumnEnd: 6,
|
||||
gridRowStart: 3,
|
||||
gridRowEnd: 4,
|
||||
borderBottom: '3px solid black',
|
||||
borderRight: '3px solid black',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'center',
|
||||
};
|
||||
|
||||
if (height > width) {
|
||||
style['gridColumnStart'] = 4;
|
||||
style['gridColumnEnd'] = 5;
|
||||
style['borderRight'] = 'unset';
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="minus" style={style}>-</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps)(Minus);
|
74
calculator-react/src/Seven.js
Normal file
74
calculator-react/src/Seven.js
Normal file
@ -0,0 +1,74 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import { colors,makeInputAndOperationsStrings } from './globals'
|
||||
import { inputStringAction } from "./actions/inputStringAction";
|
||||
import { operationStringAction } from "./actions/operationStringAction";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
inputStringAction: (inputString) => dispatch(inputStringAction(inputString)),
|
||||
operationStringAction: (operationString) => dispatch(operationStringAction(operationString)),
|
||||
});
|
||||
|
||||
class Seven extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleMouseEvent = this.handleMouseEvent.bind(this);
|
||||
this.state = {
|
||||
backgroundColor: colors.secondColor,
|
||||
};
|
||||
};
|
||||
|
||||
handleClick() {
|
||||
const [inputString,operationString] = makeInputAndOperationsStrings(this.props.inputString,this.props.operationString,'7');
|
||||
if (inputString !== this.props.inputString) {
|
||||
this.props.inputStringAction(inputString);
|
||||
}
|
||||
if (operationString !== this.props.operationString) {
|
||||
this.props.operationStringAction(operationString);
|
||||
}
|
||||
}
|
||||
|
||||
handleMouseEvent(e) {
|
||||
if (e.type === 'mousedown' || e.type === 'touchstart') {
|
||||
this.setState({backgroundColor: colors.firstColor});
|
||||
} else if (e.type === 'mouseup' || e.type === 'touchend') {
|
||||
this.setState({backgroundColor: colors.secondColor});
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
gridColumnStart: 2,
|
||||
gridColumnEnd: 3,
|
||||
gridRowStart: 2,
|
||||
gridRowEnd: 3,
|
||||
borderBottom: '3px solid black',
|
||||
borderRight: '3px solid black',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'center',
|
||||
backgroundColor: this.state.backgroundColor,
|
||||
};
|
||||
|
||||
if (height > width) {
|
||||
style['gridRowStart'] = 3;
|
||||
style['gridRowEnd'] = 4;
|
||||
style['gridColumnStart'] = 1;
|
||||
style['gridColumnEnd'] = 2;
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="seven" style={style} className='clickable' onClick={this.handleClick}
|
||||
onTouchStart={this.handleMouseEvent} onTouchEnd={this.handleMouseEvent}
|
||||
onMouseDown={this.handleMouseEvent} onMouseUp={this.handleMouseEvent}>7</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(Seven);
|
74
calculator-react/src/Six.js
Normal file
74
calculator-react/src/Six.js
Normal file
@ -0,0 +1,74 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import { colors,makeInputAndOperationsStrings } from './globals'
|
||||
import { inputStringAction } from "./actions/inputStringAction";
|
||||
import { operationStringAction } from "./actions/operationStringAction";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
inputStringAction: (inputString) => dispatch(inputStringAction(inputString)),
|
||||
operationStringAction: (operationString) => dispatch(operationStringAction(operationString)),
|
||||
});
|
||||
|
||||
class Six extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleMouseEvent = this.handleMouseEvent.bind(this);
|
||||
this.state = {
|
||||
backgroundColor: colors.secondColor,
|
||||
};
|
||||
};
|
||||
|
||||
handleClick() {
|
||||
const [inputString,operationString] = makeInputAndOperationsStrings(this.props.inputString,this.props.operationString,'6');
|
||||
if (inputString !== this.props.inputString) {
|
||||
this.props.inputStringAction(inputString);
|
||||
}
|
||||
if (operationString !== this.props.operationString) {
|
||||
this.props.operationStringAction(operationString);
|
||||
}
|
||||
}
|
||||
|
||||
handleMouseEvent(e) {
|
||||
if (e.type === 'mousedown' || e.type === 'touchstart') {
|
||||
this.setState({backgroundColor: colors.firstColor});
|
||||
} else if (e.type === 'mouseup' || e.type === 'touchend') {
|
||||
this.setState({backgroundColor: colors.secondColor});
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
gridColumnStart: 4,
|
||||
gridColumnEnd: 5,
|
||||
gridRowStart: 3,
|
||||
gridRowEnd: 4,
|
||||
borderBottom: '3px solid black',
|
||||
borderRight: '3px solid black',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'center',
|
||||
backgroundColor: this.state.backgroundColor,
|
||||
};
|
||||
|
||||
if (height > width) {
|
||||
style['gridRowStart'] = 4;
|
||||
style['gridRowEnd'] = 5;
|
||||
style['gridColumnStart'] = 3;
|
||||
style['gridColumnEnd'] = 4;
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="six" style={style} className='clickable' onClick={this.handleClick}
|
||||
onTouchStart={this.handleMouseEvent} onTouchEnd={this.handleMouseEvent}
|
||||
onMouseDown={this.handleMouseEvent} onMouseUp={this.handleMouseEvent}>6</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(Six);
|
78
calculator-react/src/Subtract.js
Normal file
78
calculator-react/src/Subtract.js
Normal file
@ -0,0 +1,78 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||
import { faMinus } from "@fortawesome/free-solid-svg-icons";
|
||||
|
||||
import { colors,makeInputAndOperationsStrings } from './globals'
|
||||
import { inputStringAction } from "./actions/inputStringAction";
|
||||
import { operationStringAction } from "./actions/operationStringAction";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
inputStringAction: (inputString) => dispatch(inputStringAction(inputString)),
|
||||
operationStringAction: (operationString) => dispatch(operationStringAction(operationString)),
|
||||
});
|
||||
|
||||
class Subtract extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleMouseEvent = this.handleMouseEvent.bind(this);
|
||||
this.state = {
|
||||
backgroundColor: colors.fourthColor,
|
||||
};
|
||||
};
|
||||
|
||||
handleClick() {
|
||||
const [inputString,operationString] = makeInputAndOperationsStrings(this.props.inputString,this.props.operationString,'-');
|
||||
if (inputString !== this.props.inputString) {
|
||||
this.props.inputStringAction(inputString);
|
||||
}
|
||||
if (operationString !== this.props.operationString) {
|
||||
this.props.operationStringAction(operationString);
|
||||
}
|
||||
}
|
||||
|
||||
handleMouseEvent(e) {
|
||||
if (e.type === 'mousedown' || e.type === 'touchstart') {
|
||||
this.setState({backgroundColor: colors.firstColor});
|
||||
} else if (e.type === 'mouseup' || e.type === 'touchend') {
|
||||
this.setState({backgroundColor: colors.fourthColor});
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
gridColumnStart: 5,
|
||||
gridColumnEnd: 6,
|
||||
gridRowStart: 3,
|
||||
gridRowEnd: 4,
|
||||
borderBottom: '3px solid black',
|
||||
borderRight: '3px solid black',
|
||||
display: 'flex',
|
||||
flexDirection: 'row',
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
backgroundColor: this.state.backgroundColor,
|
||||
};
|
||||
|
||||
if (height > width) {
|
||||
style['gridColumnStart'] = 4;
|
||||
style['gridColumnEnd'] = 5;
|
||||
style['borderRight'] = 'unset';
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="subtract" style={style} className='clickable' onClick={this.handleClick}
|
||||
onTouchStart={this.handleMouseEvent} onTouchEnd={this.handleMouseEvent}
|
||||
onMouseDown={this.handleMouseEvent} onMouseUp={this.handleMouseEvent}>
|
||||
<FontAwesomeIcon icon={faMinus}/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(Subtract);
|
74
calculator-react/src/Three.js
Normal file
74
calculator-react/src/Three.js
Normal file
@ -0,0 +1,74 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import { colors,makeInputAndOperationsStrings } from './globals'
|
||||
import { inputStringAction } from "./actions/inputStringAction";
|
||||
import { operationStringAction } from "./actions/operationStringAction";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
inputStringAction: (inputString) => dispatch(inputStringAction(inputString)),
|
||||
operationStringAction: (operationString) => dispatch(operationStringAction(operationString)),
|
||||
});
|
||||
|
||||
class Three extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleMouseEvent = this.handleMouseEvent.bind(this);
|
||||
this.state = {
|
||||
backgroundColor: colors.secondColor,
|
||||
};
|
||||
};
|
||||
|
||||
handleClick() {
|
||||
const [inputString,operationString] = makeInputAndOperationsStrings(this.props.inputString,this.props.operationString,'3');
|
||||
if (inputString !== this.props.inputString) {
|
||||
this.props.inputStringAction(inputString);
|
||||
}
|
||||
if (operationString !== this.props.operationString) {
|
||||
this.props.operationStringAction(operationString);
|
||||
}
|
||||
}
|
||||
|
||||
handleMouseEvent(e) {
|
||||
if (e.type === 'mousedown' || e.type === 'touchstart') {
|
||||
this.setState({backgroundColor: colors.firstColor});
|
||||
} else if (e.type === 'mouseup' || e.type === 'touchend') {
|
||||
this.setState({backgroundColor: colors.secondColor});
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
gridColumnStart: 4,
|
||||
gridColumnEnd: 5,
|
||||
gridRowStart: 4,
|
||||
gridRowEnd: 5,
|
||||
borderRight: '3px solid black',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'center',
|
||||
backgroundColor: this.state.backgroundColor,
|
||||
};
|
||||
|
||||
if (height > width) {
|
||||
style['gridRowStart'] = 5;
|
||||
style['gridRowEnd'] = 6;
|
||||
style['borderBottom'] = '3px solid black';
|
||||
style['gridColumnStart'] = 3;
|
||||
style['gridColumnEnd'] = 4;
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="three" style={style} className='clickable' onClick={this.handleClick}
|
||||
onTouchStart={this.handleMouseEvent} onTouchEnd={this.handleMouseEvent}
|
||||
onMouseDown={this.handleMouseEvent} onMouseUp={this.handleMouseEvent}>3</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(Three);
|
74
calculator-react/src/Two.js
Normal file
74
calculator-react/src/Two.js
Normal file
@ -0,0 +1,74 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import { colors,makeInputAndOperationsStrings } from './globals'
|
||||
import { inputStringAction } from "./actions/inputStringAction";
|
||||
import { operationStringAction } from "./actions/operationStringAction";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
inputStringAction: (inputString) => dispatch(inputStringAction(inputString)),
|
||||
operationStringAction: (operationString) => dispatch(operationStringAction(operationString)),
|
||||
});
|
||||
|
||||
class Two extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleMouseEvent = this.handleMouseEvent.bind(this);
|
||||
this.state = {
|
||||
backgroundColor: colors.secondColor,
|
||||
};
|
||||
};
|
||||
|
||||
handleClick() {
|
||||
const [inputString,operationString] = makeInputAndOperationsStrings(this.props.inputString,this.props.operationString,'2');
|
||||
if (inputString !== this.props.inputString) {
|
||||
this.props.inputStringAction(inputString);
|
||||
}
|
||||
if (operationString !== this.props.operationString) {
|
||||
this.props.operationStringAction(operationString);
|
||||
}
|
||||
}
|
||||
|
||||
handleMouseEvent(e) {
|
||||
if (e.type === 'mousedown' || e.type === 'touchstart') {
|
||||
this.setState({backgroundColor: colors.firstColor});
|
||||
} else if (e.type === 'mouseup' || e.type === 'touchend') {
|
||||
this.setState({backgroundColor: colors.secondColor});
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
gridColumnStart: 3,
|
||||
gridColumnEnd: 4,
|
||||
gridRowStart: 4,
|
||||
gridRowEnd: 5,
|
||||
borderRight: '3px solid black',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'center',
|
||||
backgroundColor: this.state.backgroundColor,
|
||||
};
|
||||
|
||||
if (height > width) {
|
||||
style['gridRowStart'] = 5;
|
||||
style['gridRowEnd'] = 6;
|
||||
style['borderBottom'] = '3px solid black';
|
||||
style['gridColumnStart'] = 2;
|
||||
style['gridColumnEnd'] = 3;
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="two" style={style} className='clickable' onClick={this.handleClick}
|
||||
onTouchStart={this.handleMouseEvent} onTouchEnd={this.handleMouseEvent}
|
||||
onMouseDown={this.handleMouseEvent} onMouseUp={this.handleMouseEvent}>2</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(Two);
|
74
calculator-react/src/Zero.js
Normal file
74
calculator-react/src/Zero.js
Normal file
@ -0,0 +1,74 @@
|
||||
import React from 'react';
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import { colors,makeInputAndOperationsStrings } from './globals'
|
||||
import { inputStringAction } from "./actions/inputStringAction";
|
||||
import { operationStringAction } from "./actions/operationStringAction";
|
||||
|
||||
const mapStateToProps = (state) => ({ ...state });
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
inputStringAction: (inputString) => dispatch(inputStringAction(inputString)),
|
||||
operationStringAction: (operationString) => dispatch(operationStringAction(operationString)),
|
||||
});
|
||||
|
||||
class Zero extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleMouseEvent = this.handleMouseEvent.bind(this);
|
||||
this.state = {
|
||||
backgroundColor: colors.secondColor,
|
||||
};
|
||||
};
|
||||
|
||||
handleClick() {
|
||||
const [inputString,operationString] = makeInputAndOperationsStrings(this.props.inputString,this.props.operationString,'0');
|
||||
if (inputString !== this.props.inputString) {
|
||||
this.props.inputStringAction(inputString);
|
||||
}
|
||||
if (operationString !== this.props.operationString) {
|
||||
this.props.operationStringAction(operationString);
|
||||
}
|
||||
}
|
||||
|
||||
handleMouseEvent(e) {
|
||||
if (e.type === 'mousedown' || e.type === 'touchstart') {
|
||||
this.setState({backgroundColor: colors.firstColor});
|
||||
} else if (e.type === 'mouseup' || e.type === 'touchend') {
|
||||
this.setState({backgroundColor: colors.secondColor});
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const width = this.props.innerWindowWidth;
|
||||
const height = this.props.innerWindowHeight;
|
||||
const style = {
|
||||
gridColumnStart: 1,
|
||||
gridColumnEnd: 2,
|
||||
gridRowStart: 4,
|
||||
gridRowEnd: 5,
|
||||
borderRight: '3px solid black',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'center',
|
||||
borderRadius: '0 0 0 7px',
|
||||
backgroundColor: this.state.backgroundColor,
|
||||
};
|
||||
|
||||
if (height > width) {
|
||||
style['gridRowStart'] = 6;
|
||||
style['gridRowEnd'] = 7;
|
||||
style['gridColumnStart'] = 1;
|
||||
style['gridColumnEnd'] = 3;
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="zero" style={style} className='clickable' onClick={this.handleClick}
|
||||
onTouchStart={this.handleMouseEvent} onTouchEnd={this.handleMouseEvent}
|
||||
onMouseDown={this.handleMouseEvent} onMouseUp={this.handleMouseEvent}>0</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(Zero);
|
8
calculator-react/src/actions/innerWindowHeightAction.js
Normal file
8
calculator-react/src/actions/innerWindowHeightAction.js
Normal file
@ -0,0 +1,8 @@
|
||||
export const SETWINDOWINNERHEIGHT = "SETWINDOWINNERHEIGHT";
|
||||
|
||||
export const innerWindowHeightAction = (windowInnerHeight) => {
|
||||
return {
|
||||
type: SETWINDOWINNERHEIGHT,
|
||||
windowInnerHeight: windowInnerHeight,
|
||||
};
|
||||
};
|
8
calculator-react/src/actions/innerWindowWidthAction.js
Normal file
8
calculator-react/src/actions/innerWindowWidthAction.js
Normal file
@ -0,0 +1,8 @@
|
||||
export const SETWINDOWINNERWIDTH = "SETWINDOWINNERWIDTH";
|
||||
|
||||
export const innerWindowWidthAction = (windowInnerWidth) => {
|
||||
return {
|
||||
type: SETWINDOWINNERWIDTH,
|
||||
windowInnerWidth: windowInnerWidth,
|
||||
};
|
||||
};
|
8
calculator-react/src/actions/inputStringAction.js
Normal file
8
calculator-react/src/actions/inputStringAction.js
Normal file
@ -0,0 +1,8 @@
|
||||
export const SETINPUTSTRING = "SETINPUTSTRING";
|
||||
|
||||
export const inputStringAction = (inputString) => {
|
||||
return {
|
||||
type: SETINPUTSTRING,
|
||||
inputString: inputString,
|
||||
};
|
||||
};
|
8
calculator-react/src/actions/operationStringAction.js
Normal file
8
calculator-react/src/actions/operationStringAction.js
Normal file
@ -0,0 +1,8 @@
|
||||
export const SETOPERATIONSTRING = "SETOPERATIONSTRING";
|
||||
|
||||
export const operationStringAction = (operationString) => {
|
||||
return {
|
||||
type: SETOPERATIONSTRING,
|
||||
operationString: operationString,
|
||||
};
|
||||
};
|
253
calculator-react/src/globals.js
Normal file
253
calculator-react/src/globals.js
Normal file
@ -0,0 +1,253 @@
|
||||
export const colors = {
|
||||
firstColor: '#00293C',
|
||||
secondColor: '#1e656d',
|
||||
thirdColor: '#6d261e',
|
||||
fourthColor: '#656d1e',
|
||||
fifthColor: '#261e6d',
|
||||
textColor: '#f1f3ce',
|
||||
secondtextColor: '#f62a00',
|
||||
};
|
||||
|
||||
const operators = ['*','/','-','+'];
|
||||
|
||||
export const makeInputAndOperationsStrings = (inputString,operationString,nextChar) => {
|
||||
let inputStringResult = inputString;
|
||||
let tempOperationString = operationString.includes('=') ? operationString.split('=')[1] : operationString;
|
||||
let operationStringResult = tempOperationString;
|
||||
|
||||
if (tempOperationString.length > 0 && ['*','/','+','-','='].includes(nextChar) && tempOperationString.slice(-1) === '.') {
|
||||
tempOperationString = tempOperationString.slice(0,-1);
|
||||
}
|
||||
|
||||
const numbers = ['1','2','3','4','5','6','7','8','9'];
|
||||
|
||||
if (numbers.includes(nextChar)) {
|
||||
if (inputString.length === 0) {
|
||||
inputStringResult = nextChar;
|
||||
operationStringResult = tempOperationString + nextChar;
|
||||
} else if (inputString.length === 1) {
|
||||
if (inputString[0] === '0') {
|
||||
inputStringResult = nextChar;
|
||||
operationStringResult = tempOperationString.slice(0,-1) + nextChar;
|
||||
} else if (inputString[0] === '-') {
|
||||
if (tempOperationString.length === 1) {
|
||||
inputStringResult = inputString + nextChar;
|
||||
operationStringResult = inputString + nextChar;
|
||||
} else {
|
||||
inputStringResult = inputString + nextChar;
|
||||
operationStringResult = tempOperationString + nextChar;
|
||||
}
|
||||
} else if (['*','/','+'].includes(inputString[0])) {
|
||||
inputStringResult = nextChar;
|
||||
operationStringResult = tempOperationString + nextChar;
|
||||
} else {
|
||||
inputStringResult = inputString + nextChar;
|
||||
operationStringResult = tempOperationString + nextChar;
|
||||
}
|
||||
} else if (inputString.length === 2) {
|
||||
if (inputString[0] === '-') {
|
||||
if (inputString[1] === '0') {
|
||||
inputStringResult = '-' + nextChar;
|
||||
operationStringResult = tempOperationString.slice(0,-1) + nextChar;
|
||||
} else {
|
||||
inputStringResult = inputString + nextChar;
|
||||
operationStringResult = tempOperationString + nextChar;
|
||||
}
|
||||
} else {
|
||||
inputStringResult = inputString + nextChar;
|
||||
operationStringResult = tempOperationString + nextChar;
|
||||
}
|
||||
} else if (inputString.length > 2) {
|
||||
inputStringResult = inputString + nextChar;
|
||||
operationStringResult = tempOperationString + nextChar;
|
||||
}
|
||||
} else if (nextChar === '0') {
|
||||
if (inputString.length === 0) {
|
||||
inputStringResult = nextChar;
|
||||
operationStringResult = tempOperationString + nextChar;
|
||||
} else if (inputString.length === 1) {
|
||||
if (inputString[0] !== '0') {
|
||||
if (['*','/','+'].includes(inputString[0])) {
|
||||
inputStringResult = nextChar;
|
||||
operationStringResult = tempOperationString + nextChar;
|
||||
} else {
|
||||
inputStringResult = inputString + nextChar;
|
||||
operationStringResult = tempOperationString + nextChar;
|
||||
}
|
||||
}
|
||||
} else if (inputString.length === 2) {
|
||||
if (inputString[0] !== '-') {
|
||||
inputStringResult = inputString + nextChar;
|
||||
operationStringResult = tempOperationString + nextChar;
|
||||
} else if (inputString[1] !== '0') {
|
||||
inputStringResult = inputString + nextChar;
|
||||
operationStringResult = tempOperationString + nextChar;
|
||||
}
|
||||
} else if (inputString.length > 2) {
|
||||
inputStringResult = inputString + nextChar;
|
||||
operationStringResult = tempOperationString + nextChar;
|
||||
}
|
||||
} else if (nextChar === '-') {
|
||||
if (tempOperationString.length === 0) {
|
||||
inputStringResult = nextChar;
|
||||
operationStringResult = nextChar;
|
||||
} else if (tempOperationString.length === 1) {
|
||||
if (!operators.includes(tempOperationString[0])) {
|
||||
inputStringResult = nextChar;
|
||||
operationStringResult = tempOperationString + nextChar;
|
||||
}
|
||||
} else {
|
||||
inputStringResult = nextChar;
|
||||
operationStringResult = tempOperationString + nextChar;
|
||||
}
|
||||
} else if (['*','/','+'].includes(nextChar)) {
|
||||
if (tempOperationString.length === 1) {
|
||||
if (!operators.includes(tempOperationString.slice(-1))) {
|
||||
operationStringResult = tempOperationString + nextChar;
|
||||
inputStringResult = nextChar;
|
||||
}
|
||||
} else if (tempOperationString.length > 1) {
|
||||
inputStringResult = nextChar;
|
||||
operationStringResult = tempOperationString + nextChar;
|
||||
}
|
||||
} else if (nextChar === '.' && (!inputString.includes('.'))) {
|
||||
if (inputString.length === 0) {
|
||||
inputStringResult = '0.';
|
||||
operationStringResult = tempOperationString + '0.';
|
||||
} else if (inputString.length === 1) {
|
||||
if (tempOperationString.length === 0) {
|
||||
if (inputString[0] === '-') {
|
||||
inputStringResult = '-0.';
|
||||
operationStringResult = tempOperationString + '-0.';
|
||||
} else {
|
||||
inputStringResult = inputString + nextChar;
|
||||
operationStringResult = tempOperationString + nextChar;
|
||||
}
|
||||
} else {
|
||||
if (operators.includes(inputString[0])) {
|
||||
inputStringResult = '0.';
|
||||
operationStringResult = tempOperationString + '0.';
|
||||
} else {
|
||||
inputStringResult = inputString + nextChar;
|
||||
operationStringResult = tempOperationString + nextChar;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
inputStringResult = inputString + nextChar;
|
||||
operationStringResult = tempOperationString + nextChar;
|
||||
}
|
||||
} else if (nextChar === '=' && tempOperationString.length > 0) {
|
||||
const lastOperationChar = tempOperationString.slice(-1);
|
||||
if (operators.includes(lastOperationChar) || lastOperationChar === '.') {
|
||||
inputStringResult = parseAndCalculate(tempOperationString.slice(0,-1));
|
||||
operationStringResult = tempOperationString.slice(0,-1) + '=' + inputStringResult;
|
||||
} else {
|
||||
inputStringResult = parseAndCalculate(tempOperationString);
|
||||
operationStringResult = tempOperationString + '=' + inputStringResult;
|
||||
}
|
||||
} else if (nextChar === 'b') {
|
||||
if (tempOperationString.length === 1) {
|
||||
inputStringResult = inputString.slice(0,-1);
|
||||
operationStringResult = tempOperationString.slice(0,-1);
|
||||
} else if (tempOperationString.length > 1) {
|
||||
if (operators.includes(tempOperationString.slice(-1))) {
|
||||
if (operators.includes(tempOperationString.slice(-2,-1))) {
|
||||
inputStringResult = tempOperationString.slice(-2,-1);
|
||||
operationStringResult = tempOperationString.slice(0,-1);
|
||||
} else {
|
||||
operationStringResult = tempOperationString.slice(0,-1);
|
||||
inputStringResult = operationStringResult.split(/[-*/+]/).slice(-1)[0];
|
||||
}
|
||||
} else {
|
||||
if (operators.includes(tempOperationString.slice(-2,-1))) {
|
||||
inputStringResult = tempOperationString.slice(-2,-1);
|
||||
operationStringResult = tempOperationString.slice(0,-1);
|
||||
} else {
|
||||
inputStringResult = inputString.slice(0,-1);
|
||||
operationStringResult = tempOperationString.slice(0,-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [inputStringResult,operationStringResult];
|
||||
}
|
||||
|
||||
const parseAndCalculate = (operationString) => {
|
||||
const statementArray = [];
|
||||
operationString.split('').forEach((item,index) => {
|
||||
if (index === 0) {
|
||||
statementArray.push(item);
|
||||
} else {
|
||||
if (operators.includes(item)) {
|
||||
statementArray.push(item);
|
||||
} else {
|
||||
if (statementArray[statementArray.length - 1] === '-') {
|
||||
if (index === 1 || operators.includes(statementArray[statementArray.length - 2])) {
|
||||
const previous = statementArray[statementArray.length - 1];
|
||||
statementArray[statementArray.length - 1] = previous + item;
|
||||
} else {
|
||||
statementArray.push(item);
|
||||
}
|
||||
} else if (['*','/','+'].includes(statementArray[statementArray.length - 1])) {
|
||||
statementArray.push(item);
|
||||
} else {
|
||||
const previous = statementArray[statementArray.length - 1];
|
||||
statementArray[statementArray.length - 1] = previous + item;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
// remove trailing operators
|
||||
while (operators.includes(statementArray[statementArray.length - 1])) {
|
||||
statementArray.pop();
|
||||
}
|
||||
const duplicateOperatorsFilteredArray = statementArray.filter((item,index) => {
|
||||
return (!operators.includes(statementArray[index + 1]) || !operators.includes(item));
|
||||
});
|
||||
if (duplicateOperatorsFilteredArray.length === 1) {
|
||||
return duplicateOperatorsFilteredArray[0];
|
||||
} else {
|
||||
return calculate(duplicateOperatorsFilteredArray.map(item => {
|
||||
if (operators.includes(item)) {
|
||||
return item;
|
||||
} else {
|
||||
return item.includes('.') ? parseFloat(item) : parseInt(item);
|
||||
}
|
||||
}))[0].toString();
|
||||
}
|
||||
}
|
||||
|
||||
const calculate = (statementArray) => {
|
||||
if (statementArray.length === 1) {
|
||||
return statementArray;
|
||||
} else if (statementArray.includes('*')) {
|
||||
const indexOfOperator = statementArray.indexOf('*');
|
||||
return calculate([
|
||||
...statementArray.slice(0,indexOfOperator - 1),
|
||||
statementArray[indexOfOperator - 1] * statementArray[indexOfOperator + 1],
|
||||
...statementArray.slice((indexOfOperator + 2),)
|
||||
]);
|
||||
} else if (statementArray.includes('/')) {
|
||||
const indexOfOperator = statementArray.indexOf('/');
|
||||
return calculate([
|
||||
...statementArray.slice(0,indexOfOperator - 1),
|
||||
statementArray[indexOfOperator - 1] / statementArray[indexOfOperator + 1],
|
||||
...statementArray.slice((indexOfOperator + 2),)
|
||||
]);
|
||||
} else if (statementArray.includes('-')) {
|
||||
const indexOfOperator = statementArray.indexOf('-');
|
||||
return calculate([
|
||||
...statementArray.slice(0,indexOfOperator - 1),
|
||||
statementArray[indexOfOperator - 1] - statementArray[indexOfOperator + 1],
|
||||
...statementArray.slice((indexOfOperator + 2),)
|
||||
]);
|
||||
} else if (statementArray.includes('+')) {
|
||||
const indexOfOperator = statementArray.indexOf('+');
|
||||
return calculate([
|
||||
...statementArray.slice(0,indexOfOperator - 1),
|
||||
statementArray[indexOfOperator - 1] + statementArray[indexOfOperator + 1],
|
||||
...statementArray.slice((indexOfOperator + 2),)
|
||||
]);
|
||||
}
|
||||
}
|
14
calculator-react/src/index.css
Normal file
14
calculator-react/src/index.css
Normal file
@ -0,0 +1,14 @@
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
|
||||
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
|
||||
sans-serif;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
code {
|
||||
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
|
||||
monospace;
|
||||
}
|
20
calculator-react/src/index.js
Normal file
20
calculator-react/src/index.js
Normal file
@ -0,0 +1,20 @@
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import { Provider } from "react-redux";
|
||||
import store from "./store";
|
||||
|
||||
import './index.css';
|
||||
import App from './App';
|
||||
import * as serviceWorker from './serviceWorker';
|
||||
|
||||
ReactDOM.render(
|
||||
<Provider store={store}>
|
||||
<App />
|
||||
</Provider>,
|
||||
document.getElementById('root')
|
||||
);
|
||||
|
||||
// If you want your app to work offline and load faster, you can change
|
||||
// unregister() to register() below. Note this comes with some pitfalls.
|
||||
// Learn more about service workers: https://bit.ly/CRA-PWA
|
||||
serviceWorker.unregister();
|
7
calculator-react/src/logo.svg
Normal file
7
calculator-react/src/logo.svg
Normal file
@ -0,0 +1,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 841.9 595.3">
|
||||
<g fill="#61DAFB">
|
||||
<path d="M666.3 296.5c0-32.5-40.7-63.3-103.1-82.4 14.4-63.6 8-114.2-20.2-130.4-6.5-3.8-14.1-5.6-22.4-5.6v22.3c4.6 0 8.3.9 11.4 2.6 13.6 7.8 19.5 37.5 14.9 75.7-1.1 9.4-2.9 19.3-5.1 29.4-19.6-4.8-41-8.5-63.5-10.9-13.5-18.5-27.5-35.3-41.6-50 32.6-30.3 63.2-46.9 84-46.9V78c-27.5 0-63.5 19.6-99.9 53.6-36.4-33.8-72.4-53.2-99.9-53.2v22.3c20.7 0 51.4 16.5 84 46.6-14 14.7-28 31.4-41.3 49.9-22.6 2.4-44 6.1-63.6 11-2.3-10-4-19.7-5.2-29-4.7-38.2 1.1-67.9 14.6-75.8 3-1.8 6.9-2.6 11.5-2.6V78.5c-8.4 0-16 1.8-22.6 5.6-28.1 16.2-34.4 66.7-19.9 130.1-62.2 19.2-102.7 49.9-102.7 82.3 0 32.5 40.7 63.3 103.1 82.4-14.4 63.6-8 114.2 20.2 130.4 6.5 3.8 14.1 5.6 22.5 5.6 27.5 0 63.5-19.6 99.9-53.6 36.4 33.8 72.4 53.2 99.9 53.2 8.4 0 16-1.8 22.6-5.6 28.1-16.2 34.4-66.7 19.9-130.1 62-19.1 102.5-49.9 102.5-82.3zm-130.2-66.7c-3.7 12.9-8.3 26.2-13.5 39.5-4.1-8-8.4-16-13.1-24-4.6-8-9.5-15.8-14.4-23.4 14.2 2.1 27.9 4.7 41 7.9zm-45.8 106.5c-7.8 13.5-15.8 26.3-24.1 38.2-14.9 1.3-30 2-45.2 2-15.1 0-30.2-.7-45-1.9-8.3-11.9-16.4-24.6-24.2-38-7.6-13.1-14.5-26.4-20.8-39.8 6.2-13.4 13.2-26.8 20.7-39.9 7.8-13.5 15.8-26.3 24.1-38.2 14.9-1.3 30-2 45.2-2 15.1 0 30.2.7 45 1.9 8.3 11.9 16.4 24.6 24.2 38 7.6 13.1 14.5 26.4 20.8 39.8-6.3 13.4-13.2 26.8-20.7 39.9zm32.3-13c5.4 13.4 10 26.8 13.8 39.8-13.1 3.2-26.9 5.9-41.2 8 4.9-7.7 9.8-15.6 14.4-23.7 4.6-8 8.9-16.1 13-24.1zM421.2 430c-9.3-9.6-18.6-20.3-27.8-32 9 .4 18.2.7 27.5.7 9.4 0 18.7-.2 27.8-.7-9 11.7-18.3 22.4-27.5 32zm-74.4-58.9c-14.2-2.1-27.9-4.7-41-7.9 3.7-12.9 8.3-26.2 13.5-39.5 4.1 8 8.4 16 13.1 24 4.7 8 9.5 15.8 14.4 23.4zM420.7 163c9.3 9.6 18.6 20.3 27.8 32-9-.4-18.2-.7-27.5-.7-9.4 0-18.7.2-27.8.7 9-11.7 18.3-22.4 27.5-32zm-74 58.9c-4.9 7.7-9.8 15.6-14.4 23.7-4.6 8-8.9 16-13 24-5.4-13.4-10-26.8-13.8-39.8 13.1-3.1 26.9-5.8 41.2-7.9zm-90.5 125.2c-35.4-15.1-58.3-34.9-58.3-50.6 0-15.7 22.9-35.6 58.3-50.6 8.6-3.7 18-7 27.7-10.1 5.7 19.6 13.2 40 22.5 60.9-9.2 20.8-16.6 41.1-22.2 60.6-9.9-3.1-19.3-6.5-28-10.2zM310 490c-13.6-7.8-19.5-37.5-14.9-75.7 1.1-9.4 2.9-19.3 5.1-29.4 19.6 4.8 41 8.5 63.5 10.9 13.5 18.5 27.5 35.3 41.6 50-32.6 30.3-63.2 46.9-84 46.9-4.5-.1-8.3-1-11.3-2.7zm237.2-76.2c4.7 38.2-1.1 67.9-14.6 75.8-3 1.8-6.9 2.6-11.5 2.6-20.7 0-51.4-16.5-84-46.6 14-14.7 28-31.4 41.3-49.9 22.6-2.4 44-6.1 63.6-11 2.3 10.1 4.1 19.8 5.2 29.1zm38.5-66.7c-8.6 3.7-18 7-27.7 10.1-5.7-19.6-13.2-40-22.5-60.9 9.2-20.8 16.6-41.1 22.2-60.6 9.9 3.1 19.3 6.5 28.1 10.2 35.4 15.1 58.3 34.9 58.3 50.6-.1 15.7-23 35.6-58.4 50.6zM320.8 78.4z"/>
|
||||
<circle cx="420.9" cy="296.5" r="45.7"/>
|
||||
<path d="M520.5 78.1z"/>
|
||||
</g>
|
||||
</svg>
|
After Width: | Height: | Size: 2.6 KiB |
14
calculator-react/src/reducers/innerWindowHeightReducer.js
Normal file
14
calculator-react/src/reducers/innerWindowHeightReducer.js
Normal file
@ -0,0 +1,14 @@
|
||||
import { SETWINDOWINNERHEIGHT } from "../actions/innerWindowHeightAction";
|
||||
|
||||
export default (state, action) => {
|
||||
if (!state) {
|
||||
state = window.innerHeight;
|
||||
}
|
||||
|
||||
switch (action.type) {
|
||||
case SETWINDOWINNERHEIGHT:
|
||||
return action.windowInnerHeight;
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
14
calculator-react/src/reducers/innerWindowWidthReducer.js
Normal file
14
calculator-react/src/reducers/innerWindowWidthReducer.js
Normal file
@ -0,0 +1,14 @@
|
||||
import { SETWINDOWINNERWIDTH } from "../actions/innerWindowWidthAction";
|
||||
|
||||
export default (state, action) => {
|
||||
if (!state) {
|
||||
state = window.innerWidth;
|
||||
}
|
||||
|
||||
switch (action.type) {
|
||||
case SETWINDOWINNERWIDTH:
|
||||
return action.windowInnerWidth;
|
||||
default:
|
||||
return state;
|
||||
};
|
||||
};
|
14
calculator-react/src/reducers/inputStringReducer.js
Normal file
14
calculator-react/src/reducers/inputStringReducer.js
Normal file
@ -0,0 +1,14 @@
|
||||
import { SETINPUTSTRING } from "../actions/inputStringAction";
|
||||
|
||||
export default (state, action) => {
|
||||
if (!state) {
|
||||
state = '';
|
||||
}
|
||||
|
||||
switch (action.type) {
|
||||
case SETINPUTSTRING:
|
||||
return action.inputString;
|
||||
default:
|
||||
return state;
|
||||
};
|
||||
};
|
14
calculator-react/src/reducers/operationStringReducer.js
Normal file
14
calculator-react/src/reducers/operationStringReducer.js
Normal file
@ -0,0 +1,14 @@
|
||||
import { SETOPERATIONSTRING } from "../actions/operationStringAction";
|
||||
|
||||
export default (state, action) => {
|
||||
if (!state) {
|
||||
state = '';
|
||||
}
|
||||
|
||||
switch (action.type) {
|
||||
case SETOPERATIONSTRING:
|
||||
return action.operationString;
|
||||
default:
|
||||
return state;
|
||||
};
|
||||
};
|
12
calculator-react/src/reducers/rootReducer.js
Normal file
12
calculator-react/src/reducers/rootReducer.js
Normal file
@ -0,0 +1,12 @@
|
||||
import { combineReducers } from "redux";
|
||||
import innerWindowWidthReducer from "./innerWindowWidthReducer";
|
||||
import innerWindowHeightReducer from "./innerWindowHeightReducer";
|
||||
import inputStringReducer from "./inputStringReducer";
|
||||
import operationStringReducer from "./operationStringReducer";
|
||||
|
||||
export default combineReducers({
|
||||
innerWindowWidth: innerWindowWidthReducer,
|
||||
innerWindowHeight: innerWindowHeightReducer,
|
||||
inputString: inputStringReducer,
|
||||
operationString: operationStringReducer,
|
||||
});
|
141
calculator-react/src/serviceWorker.js
Normal file
141
calculator-react/src/serviceWorker.js
Normal file
@ -0,0 +1,141 @@
|
||||
// This optional code is used to register a service worker.
|
||||
// register() is not called by default.
|
||||
|
||||
// This lets the app load faster on subsequent visits in production, and gives
|
||||
// it offline capabilities. However, it also means that developers (and users)
|
||||
// will only see deployed updates on subsequent visits to a page, after all the
|
||||
// existing tabs open on the page have been closed, since previously cached
|
||||
// resources are updated in the background.
|
||||
|
||||
// To learn more about the benefits of this model and instructions on how to
|
||||
// opt-in, read https://bit.ly/CRA-PWA
|
||||
|
||||
const isLocalhost = Boolean(
|
||||
window.location.hostname === 'localhost' ||
|
||||
// [::1] is the IPv6 localhost address.
|
||||
window.location.hostname === '[::1]' ||
|
||||
// 127.0.0.0/8 are considered localhost for IPv4.
|
||||
window.location.hostname.match(
|
||||
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
|
||||
)
|
||||
);
|
||||
|
||||
export function register(config) {
|
||||
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
|
||||
// The URL constructor is available in all browsers that support SW.
|
||||
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
|
||||
if (publicUrl.origin !== window.location.origin) {
|
||||
// Our service worker won't work if PUBLIC_URL is on a different origin
|
||||
// from what our page is served on. This might happen if a CDN is used to
|
||||
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
|
||||
return;
|
||||
}
|
||||
|
||||
window.addEventListener('load', () => {
|
||||
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
|
||||
|
||||
if (isLocalhost) {
|
||||
// This is running on localhost. Let's check if a service worker still exists or not.
|
||||
checkValidServiceWorker(swUrl, config);
|
||||
|
||||
// Add some additional logging to localhost, pointing developers to the
|
||||
// service worker/PWA documentation.
|
||||
navigator.serviceWorker.ready.then(() => {
|
||||
console.log(
|
||||
'This web app is being served cache-first by a service ' +
|
||||
'worker. To learn more, visit https://bit.ly/CRA-PWA'
|
||||
);
|
||||
});
|
||||
} else {
|
||||
// Is not localhost. Just register service worker
|
||||
registerValidSW(swUrl, config);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function registerValidSW(swUrl, config) {
|
||||
navigator.serviceWorker
|
||||
.register(swUrl)
|
||||
.then(registration => {
|
||||
registration.onupdatefound = () => {
|
||||
const installingWorker = registration.installing;
|
||||
if (installingWorker == null) {
|
||||
return;
|
||||
}
|
||||
installingWorker.onstatechange = () => {
|
||||
if (installingWorker.state === 'installed') {
|
||||
if (navigator.serviceWorker.controller) {
|
||||
// At this point, the updated precached content has been fetched,
|
||||
// but the previous service worker will still serve the older
|
||||
// content until all client tabs are closed.
|
||||
console.log(
|
||||
'New content is available and will be used when all ' +
|
||||
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
|
||||
);
|
||||
|
||||
// Execute callback
|
||||
if (config && config.onUpdate) {
|
||||
config.onUpdate(registration);
|
||||
}
|
||||
} else {
|
||||
// At this point, everything has been precached.
|
||||
// It's the perfect time to display a
|
||||
// "Content is cached for offline use." message.
|
||||
console.log('Content is cached for offline use.');
|
||||
|
||||
// Execute callback
|
||||
if (config && config.onSuccess) {
|
||||
config.onSuccess(registration);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error during service worker registration:', error);
|
||||
});
|
||||
}
|
||||
|
||||
function checkValidServiceWorker(swUrl, config) {
|
||||
// Check if the service worker can be found. If it can't reload the page.
|
||||
fetch(swUrl, {
|
||||
headers: { 'Service-Worker': 'script' },
|
||||
})
|
||||
.then(response => {
|
||||
// Ensure service worker exists, and that we really are getting a JS file.
|
||||
const contentType = response.headers.get('content-type');
|
||||
if (
|
||||
response.status === 404 ||
|
||||
(contentType != null && contentType.indexOf('javascript') === -1)
|
||||
) {
|
||||
// No service worker found. Probably a different app. Reload the page.
|
||||
navigator.serviceWorker.ready.then(registration => {
|
||||
registration.unregister().then(() => {
|
||||
window.location.reload();
|
||||
});
|
||||
});
|
||||
} else {
|
||||
// Service worker found. Proceed as normal.
|
||||
registerValidSW(swUrl, config);
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
console.log(
|
||||
'No internet connection found. App is running in offline mode.'
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
export function unregister() {
|
||||
if ('serviceWorker' in navigator) {
|
||||
navigator.serviceWorker.ready
|
||||
.then(registration => {
|
||||
registration.unregister();
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(error.message);
|
||||
});
|
||||
}
|
||||
}
|
5
calculator-react/src/setupTests.js
Normal file
5
calculator-react/src/setupTests.js
Normal file
@ -0,0 +1,5 @@
|
||||
// jest-dom adds custom jest matchers for asserting on DOM nodes.
|
||||
// allows you to do things like:
|
||||
// expect(element).toHaveTextContent(/react/i)
|
||||
// learn more: https://github.com/testing-library/jest-dom
|
||||
import '@testing-library/jest-dom/extend-expect';
|
6
calculator-react/src/store.js
Normal file
6
calculator-react/src/store.js
Normal file
@ -0,0 +1,6 @@
|
||||
import { createStore } from "redux";
|
||||
import rootReducer from "./reducers/rootReducer";
|
||||
|
||||
const store = createStore(rootReducer);
|
||||
|
||||
export default store;
|
@ -44,5 +44,8 @@
|
||||
<div>
|
||||
<a class="link" href="drum-machine-react/build" target="_blank">Drum Machine React</a>
|
||||
</div>
|
||||
<div>
|
||||
<a class="link" href="calculator-react/build" target="_blank">Calculator React</a>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
Loading…
Reference in New Issue
Block a user