From d6d9d780bbf8cfcc2177d0a3afc4c2ae21e047fa Mon Sep 17 00:00:00 2001 From: Leonardo Santos Date: Sun, 7 Nov 2021 13:02:20 -0300 Subject: [PATCH] css calculator --- Projects/1-Beginner/calculator/README.md | 106 ++++++++++-------- Projects/1-Beginner/calculator/README.old.md | 62 ---------- .../1-Beginner/calculator/public/index.html | 2 +- Projects/1-Beginner/calculator/src/App.css | 49 +++----- Projects/1-Beginner/calculator/src/App.js | 50 +-------- .../src/features/calculator/Calculator.jsx | 43 +++++++ .../features/calculator/Calculator.module.css | 67 +++++++++++ .../features/calculator/calculatorSlice.js | 27 +++++ .../calculatorSlice.spec.js} | 0 .../src/features/counter/Counter.js | 67 ----------- .../src/features/counter/Counter.module.css | 78 ------------- .../src/features/counter/counterAPI.js | 6 - .../src/features/counter/counterSlice.js | 73 ------------ Projects/1-Beginner/calculator/src/index.css | 18 +-- .../1-Beginner/calculator/src/redux/index.js | 4 +- 15 files changed, 227 insertions(+), 425 deletions(-) delete mode 100644 Projects/1-Beginner/calculator/README.old.md create mode 100644 Projects/1-Beginner/calculator/src/features/calculator/Calculator.jsx create mode 100644 Projects/1-Beginner/calculator/src/features/calculator/Calculator.module.css create mode 100644 Projects/1-Beginner/calculator/src/features/calculator/calculatorSlice.js rename Projects/1-Beginner/calculator/src/features/{counter/counterSlice.spec.js => calculator/calculatorSlice.spec.js} (100%) delete mode 100644 Projects/1-Beginner/calculator/src/features/counter/Counter.js delete mode 100644 Projects/1-Beginner/calculator/src/features/counter/Counter.module.css delete mode 100644 Projects/1-Beginner/calculator/src/features/counter/counterAPI.js delete mode 100644 Projects/1-Beginner/calculator/src/features/counter/counterSlice.js diff --git a/Projects/1-Beginner/calculator/README.md b/Projects/1-Beginner/calculator/README.md index b4ced64d..c6219f3a 100644 --- a/Projects/1-Beginner/calculator/README.md +++ b/Projects/1-Beginner/calculator/README.md @@ -1,44 +1,62 @@ -This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app), using the [Redux](https://redux.js.org/) and [Redux Toolkit](https://redux-toolkit.js.org/) template. - -## Available Scripts - -In the project directory, you can run: - -### `yarn start` - -Runs the app in the development mode.
-Open [http://localhost:3000](http://localhost:3000) to view it in the browser. - -The page will reload if you make edits.
-You will also see any lint errors in the console. - -### `yarn test` - -Launches the test runner in the interactive watch mode.
-See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. - -### `yarn build` - -Builds the app for production to the `build` folder.
-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.
-Your app is ready to be deployed! - -See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. - -### `yarn 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/). +# Calculator + +**Tier:** 1-Beginner + +Calculators are not only one of the most useful tools available, but they are +also a great way to understand UI and event processing in an application. In +this problem you will create a calculator that supports basic arithmetic +calculations on integers. + +The styling is up to you so use your imagination and get creative! You might +also find it worth your time to experiment with the calculator app on your +mobile device to better understand basic functionality and edge cases. + +### Constraints + +- You may not use the `eval()` function to execute calculations + +## User Stories + +- [ ] User can see a display showing the current number entered or the +result of the last operation. +- [x] User can see an entry pad containing buttons for the digits 0-9, +operations - '+', '-', '/', and '=', a 'C' button (for clear), and an 'AC' +button (for clear all). +- [ ] User can enter numbers as sequences up to 8 digits long by clicking on +digits in the entry pad. Entry of any digits more than 8 will be ignored. +- [ ] User can click on an operation button to display the result of that +operation on: + * the result of the preceding operation and the last number entered OR + * the last two numbers entered OR + * the last number entered +- [ ] User can click the 'C' button to clear the last number or the last +operation. If the users last entry was an operation the display will be +updated to the value that preceded it. +- [ ] User can click the 'AC' button to clear all internal work areas and +to set the display to 0. +- [ ] User can see 'ERR' displayed if any operation would exceed the +8 digit maximum. + +## Bonus features + +- [ ] User can click a '+/-' button to change the sign of the number that is +currently displayed. +- [ ] User can see a decimal point ('.') button on the entry pad to that +allows floating point numbers up to 3 places to be entered and operations to +be carried out to the maximum number of decimal places entered for any one +number. + +## Useful links and resources + +- [Calculator (Wikipedia)](https://en.wikipedia.org/wiki/Calculator) +- [MDN](https://developer.mozilla.org/en-US/) + +## Example projects + +- [BHMBS - JS-Neumorphic-Calculator](https://barhouum7.github.io/JS-Neumorphic-Calc.github.io/) +- [Javascript iOS Style Calculator](https://codepen.io/ssmkhrj/full/jOWBQqO) +- [Javascript Calculator](https://codepen.io/giana/pen/GJMBEv) +- [React Calculator](https://codepen.io/mjijackson/pen/xOzyGX) +- [Javascript-CALC](https://github.com/x0uter/javascript-calc) +- [Sample Calculator](https://sevlasnog.github.io/sample-calculator) +- [Python Calculator](https://github.com/kana800/Side-Projects/tree/master/1-Beginner/calculator) diff --git a/Projects/1-Beginner/calculator/README.old.md b/Projects/1-Beginner/calculator/README.old.md deleted file mode 100644 index be10ecbb..00000000 --- a/Projects/1-Beginner/calculator/README.old.md +++ /dev/null @@ -1,62 +0,0 @@ -# Calculator - -**Tier:** 1-Beginner - -Calculators are not only one of the most useful tools available, but they are -also a great way to understand UI and event processing in an application. In -this problem you will create a calculator that supports basic arithmetic -calculations on integers. - -The styling is up to you so use your imagination and get creative! You might -also find it worth your time to experiment with the calculator app on your -mobile device to better understand basic functionality and edge cases. - -### Constraints - -- You may not use the `eval()` function to execute calculations - -## User Stories - -- [ ] User can see a display showing the current number entered or the -result of the last operation. -- [ ] User can see an entry pad containing buttons for the digits 0-9, -operations - '+', '-', '/', and '=', a 'C' button (for clear), and an 'AC' -button (for clear all). -- [ ] User can enter numbers as sequences up to 8 digits long by clicking on -digits in the entry pad. Entry of any digits more than 8 will be ignored. -- [ ] User can click on an operation button to display the result of that -operation on: - * the result of the preceding operation and the last number entered OR - * the last two numbers entered OR - * the last number entered -- [ ] User can click the 'C' button to clear the last number or the last -operation. If the users last entry was an operation the display will be -updated to the value that preceded it. -- [ ] User can click the 'AC' button to clear all internal work areas and -to set the display to 0. -- [ ] User can see 'ERR' displayed if any operation would exceed the -8 digit maximum. - -## Bonus features - -- [ ] User can click a '+/-' button to change the sign of the number that is -currently displayed. -- [ ] User can see a decimal point ('.') button on the entry pad to that -allows floating point numbers up to 3 places to be entered and operations to -be carried out to the maximum number of decimal places entered for any one -number. - -## Useful links and resources - -- [Calculator (Wikipedia)](https://en.wikipedia.org/wiki/Calculator) -- [MDN](https://developer.mozilla.org/en-US/) - -## Example projects - -- [BHMBS - JS-Neumorphic-Calculator](https://barhouum7.github.io/JS-Neumorphic-Calc.github.io/) -- [Javascript iOS Style Calculator](https://codepen.io/ssmkhrj/full/jOWBQqO) -- [Javascript Calculator](https://codepen.io/giana/pen/GJMBEv) -- [React Calculator](https://codepen.io/mjijackson/pen/xOzyGX) -- [Javascript-CALC](https://github.com/x0uter/javascript-calc) -- [Sample Calculator](https://sevlasnog.github.io/sample-calculator) -- [Python Calculator](https://github.com/kana800/Side-Projects/tree/master/1-Beginner/calculator) diff --git a/Projects/1-Beginner/calculator/public/index.html b/Projects/1-Beginner/calculator/public/index.html index 1dbdca65..26de10b4 100644 --- a/Projects/1-Beginner/calculator/public/index.html +++ b/Projects/1-Beginner/calculator/public/index.html @@ -24,7 +24,7 @@ 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`. --> - React Redux App + Calculator diff --git a/Projects/1-Beginner/calculator/src/App.css b/Projects/1-Beginner/calculator/src/App.css index 01cc5867..f592f633 100644 --- a/Projects/1-Beginner/calculator/src/App.css +++ b/Projects/1-Beginner/calculator/src/App.css @@ -1,39 +1,16 @@ .App { - text-align: center; + width:100vw; + max-width:100%; + min-height:100vh; + background: var(--primaryDark); + display: grid; + grid: 1fr 0.05fr / 1fr; } -.App-logo { - height: 40vmin; - pointer-events: none; -} - -@media (prefers-reduced-motion: no-preference) { - .App-logo { - animation: App-logo-float infinite 3s ease-in-out; - } -} - -.App-header { - min-height: 100vh; - display: flex; - flex-direction: column; - align-items: center; - justify-content: center; - font-size: calc(10px + 2vmin); -} - -.App-link { - color: rgb(112, 76, 182); -} - -@keyframes App-logo-float { - 0% { - transform: translateY(0); - } - 50% { - transform: translateY(10px); - } - 100% { - transform: translateY(0px); - } -} +.App > footer { + width:100%; + background:rgb(29, 29, 29); + text-align:center; + padding:10px; + color:white; +} \ No newline at end of file diff --git a/Projects/1-Beginner/calculator/src/App.js b/Projects/1-Beginner/calculator/src/App.js index a40f66d6..a6d8db3c 100644 --- a/Projects/1-Beginner/calculator/src/App.js +++ b/Projects/1-Beginner/calculator/src/App.js @@ -1,56 +1,12 @@ import React from 'react'; -import logo from './logo.svg'; -import { Counter } from './features/counter/Counter'; +import { Calculator } from './features/calculator/Calculator'; import './App.css'; function App() { return (
-
- logo - -

- Edit src/App.js and save to reload. -

- - Learn - - React - - , - - Redux - - , - - Redux Toolkit - - , and - - React Redux - - -
+ +
Feito com 💜 por Leonardo Santos
); } diff --git a/Projects/1-Beginner/calculator/src/features/calculator/Calculator.jsx b/Projects/1-Beginner/calculator/src/features/calculator/Calculator.jsx new file mode 100644 index 00000000..87c99633 --- /dev/null +++ b/Projects/1-Beginner/calculator/src/features/calculator/Calculator.jsx @@ -0,0 +1,43 @@ +import React, { useState } from 'react'; +import { useSelector, useDispatch } from 'react-redux'; +import { decrement, increment, selectCount } from './calculatorSlice'; +import styles from './Calculator.module.css'; + +export function Calculator() { + const values = useSelector(selectCount); + const dispatch = useDispatch(); + const [count, setCount] = useState(0); + + return ( +
+
+ {values.map((value) => {value})} + +
+
+
+ + +
+ + + + + + + + + + + + + + + + + + +
+
+ ); +} diff --git a/Projects/1-Beginner/calculator/src/features/calculator/Calculator.module.css b/Projects/1-Beginner/calculator/src/features/calculator/Calculator.module.css new file mode 100644 index 00000000..d1532dcd --- /dev/null +++ b/Projects/1-Beginner/calculator/src/features/calculator/Calculator.module.css @@ -0,0 +1,67 @@ +.section { + display: grid; + grid: 0.25fr 1fr / 1fr; + padding:25px; + background: var(--secondaryLight); +} + +.inputContainer { + display: grid; + place-items:center; + width:100%; +} + +.inputContainer > input { + width:80%; + height:50%; + border-radius:5px; + border:0; + padding:5px; + box-shadow:2px 2px 2px rgba(0, 0, 0, 0.15); +} + +.buttonsContainer { + display: grid; + grid: repeat(5, 1fr) / repeat(4, 1fr); + gap:5px; + border: 0.5px solid var(--secondaryDark); + padding:25px; + background:rgb(219, 219, 219); +} + +.buttonsContainer button { + border:0; + border-radius:1.5px; + box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.15); + cursor:pointer; + transition: filter 1s ease, background 1s ease; +} + +.buttonsContainer button:hover { + filter: brightness(125%); +} + +.clearContainer { + display: grid; + grid: 1fr / 1fr 1fr; + gap:1.5px; +} + +.clear { + background: orangered; + color:white; +} + +.clear:hover { + background: rgb(255, 90, 31); +} + +.operator { + background: orange; +} + +.result { + grid-column: span 2; + background: rgb(39, 218, 23); + color:white +} \ No newline at end of file diff --git a/Projects/1-Beginner/calculator/src/features/calculator/calculatorSlice.js b/Projects/1-Beginner/calculator/src/features/calculator/calculatorSlice.js new file mode 100644 index 00000000..1f29e50b --- /dev/null +++ b/Projects/1-Beginner/calculator/src/features/calculator/calculatorSlice.js @@ -0,0 +1,27 @@ +import { createSlice } from '@reduxjs/toolkit'; + +const initialState = { + values: [0], +}; + +export const counterSlice = createSlice({ + name: 'calculator', + initialState, + reducers: { + increment: (state) => { + state.value += 1; + }, + decrement: (state) => { + state.value -= 1; + }, + incrementByAmount: (state, action) => { + state.value += action.payload; + }, + } +}); + +export const { increment, decrement, incrementByAmount } = counterSlice.actions; + +export const selectCount = (state) => state.calculator.values; + +export default counterSlice.reducer; diff --git a/Projects/1-Beginner/calculator/src/features/counter/counterSlice.spec.js b/Projects/1-Beginner/calculator/src/features/calculator/calculatorSlice.spec.js similarity index 100% rename from Projects/1-Beginner/calculator/src/features/counter/counterSlice.spec.js rename to Projects/1-Beginner/calculator/src/features/calculator/calculatorSlice.spec.js diff --git a/Projects/1-Beginner/calculator/src/features/counter/Counter.js b/Projects/1-Beginner/calculator/src/features/counter/Counter.js deleted file mode 100644 index 772a6baf..00000000 --- a/Projects/1-Beginner/calculator/src/features/counter/Counter.js +++ /dev/null @@ -1,67 +0,0 @@ -import React, { useState } from 'react'; -import { useSelector, useDispatch } from 'react-redux'; -import { - decrement, - increment, - incrementByAmount, - incrementAsync, - incrementIfOdd, - selectCount, -} from './counterSlice'; -import styles from './Counter.module.css'; - -export function Counter() { - const count = useSelector(selectCount); - const dispatch = useDispatch(); - const [incrementAmount, setIncrementAmount] = useState('2'); - - const incrementValue = Number(incrementAmount) || 0; - - return ( -
-
- - {count} - -
-
- setIncrementAmount(e.target.value)} - /> - - - -
-
- ); -} diff --git a/Projects/1-Beginner/calculator/src/features/counter/Counter.module.css b/Projects/1-Beginner/calculator/src/features/counter/Counter.module.css deleted file mode 100644 index 004ae337..00000000 --- a/Projects/1-Beginner/calculator/src/features/counter/Counter.module.css +++ /dev/null @@ -1,78 +0,0 @@ -.row { - display: flex; - align-items: center; - justify-content: center; -} - -.row > button { - margin-left: 4px; - margin-right: 8px; -} -.row:not(:last-child) { - margin-bottom: 16px; -} - -.value { - font-size: 78px; - padding-left: 16px; - padding-right: 16px; - margin-top: 2px; - font-family: 'Courier New', Courier, monospace; -} - -.button { - appearance: none; - background: none; - font-size: 32px; - padding-left: 12px; - padding-right: 12px; - outline: none; - border: 2px solid transparent; - color: rgb(112, 76, 182); - padding-bottom: 4px; - cursor: pointer; - background-color: rgba(112, 76, 182, 0.1); - border-radius: 2px; - transition: all 0.15s; -} - -.textbox { - font-size: 32px; - padding: 2px; - width: 64px; - text-align: center; - margin-right: 4px; -} - -.button:hover, -.button:focus { - border: 2px solid rgba(112, 76, 182, 0.4); -} - -.button:active { - background-color: rgba(112, 76, 182, 0.2); -} - -.asyncButton { - composes: button; - position: relative; -} - -.asyncButton:after { - content: ''; - background-color: rgba(112, 76, 182, 0.15); - display: block; - position: absolute; - width: 100%; - height: 100%; - left: 0; - top: 0; - opacity: 0; - transition: width 1s linear, opacity 0.5s ease 1s; -} - -.asyncButton:active:after { - width: 0%; - opacity: 1; - transition: 0s; -} diff --git a/Projects/1-Beginner/calculator/src/features/counter/counterAPI.js b/Projects/1-Beginner/calculator/src/features/counter/counterAPI.js deleted file mode 100644 index cc9b4a44..00000000 --- a/Projects/1-Beginner/calculator/src/features/counter/counterAPI.js +++ /dev/null @@ -1,6 +0,0 @@ -// A mock function to mimic making an async request for data -export function fetchCount(amount = 1) { - return new Promise((resolve) => - setTimeout(() => resolve({ data: amount }), 500) - ); -} diff --git a/Projects/1-Beginner/calculator/src/features/counter/counterSlice.js b/Projects/1-Beginner/calculator/src/features/counter/counterSlice.js deleted file mode 100644 index 8dc4b5cf..00000000 --- a/Projects/1-Beginner/calculator/src/features/counter/counterSlice.js +++ /dev/null @@ -1,73 +0,0 @@ -import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'; -import { fetchCount } from './counterAPI'; - -const initialState = { - value: 0, - status: 'idle', -}; - -// The function below is called a thunk and allows us to perform async logic. It -// can be dispatched like a regular action: `dispatch(incrementAsync(10))`. This -// will call the thunk with the `dispatch` function as the first argument. Async -// code can then be executed and other actions can be dispatched. Thunks are -// typically used to make async requests. -export const incrementAsync = createAsyncThunk( - 'counter/fetchCount', - async (amount) => { - const response = await fetchCount(amount); - // The value we return becomes the `fulfilled` action payload - return response.data; - } -); - -export const counterSlice = createSlice({ - name: 'counter', - initialState, - // The `reducers` field lets us define reducers and generate associated actions - reducers: { - increment: (state) => { - // Redux Toolkit allows us to write "mutating" logic in reducers. It - // doesn't actually mutate the state because it uses the Immer library, - // which detects changes to a "draft state" and produces a brand new - // immutable state based off those changes - state.value += 1; - }, - decrement: (state) => { - state.value -= 1; - }, - // Use the PayloadAction type to declare the contents of `action.payload` - incrementByAmount: (state, action) => { - state.value += action.payload; - }, - }, - // The `extraReducers` field lets the slice handle actions defined elsewhere, - // including actions generated by createAsyncThunk or in other slices. - extraReducers: (builder) => { - builder - .addCase(incrementAsync.pending, (state) => { - state.status = 'loading'; - }) - .addCase(incrementAsync.fulfilled, (state, action) => { - state.status = 'idle'; - state.value += action.payload; - }); - }, -}); - -export const { increment, decrement, incrementByAmount } = counterSlice.actions; - -// The function below is called a selector and allows us to select a value from -// the state. Selectors can also be defined inline where they're used instead of -// in the slice file. For example: `useSelector((state: RootState) => state.counter.value)` -export const selectCount = (state) => state.counter.value; - -// We can also write thunks by hand, which may contain both sync and async logic. -// Here's an example of conditionally dispatching actions based on current state. -export const incrementIfOdd = (amount) => (dispatch, getState) => { - const currentValue = selectCount(getState()); - if (currentValue % 2 === 1) { - dispatch(incrementByAmount(amount)); - } -}; - -export default counterSlice.reducer; diff --git a/Projects/1-Beginner/calculator/src/index.css b/Projects/1-Beginner/calculator/src/index.css index ec2585e8..24a3b339 100644 --- a/Projects/1-Beginner/calculator/src/index.css +++ b/Projects/1-Beginner/calculator/src/index.css @@ -1,13 +1,13 @@ -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; + padding: 0; + outline: 0; + box-sizing: border-box; } -code { - font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', - monospace; +:root { + --primaryDark: #264653; + --primaryLight: #2a9d8f; + --secondaryDark: #e76f51; + --secondaryLight: #f4a261; } diff --git a/Projects/1-Beginner/calculator/src/redux/index.js b/Projects/1-Beginner/calculator/src/redux/index.js index 9eca6d24..5d822130 100644 --- a/Projects/1-Beginner/calculator/src/redux/index.js +++ b/Projects/1-Beginner/calculator/src/redux/index.js @@ -1,8 +1,8 @@ import { configureStore } from '@reduxjs/toolkit'; -import counterReducer from '../features/counter/counterSlice'; +import calculatorReducer from '../features/calculator/calculatorSlice'; export const store = configureStore({ reducer: { - counter: counterReducer, + calculator: calculatorReducer, }, });