Skip to content

Commit

Permalink
UI completed
Browse files Browse the repository at this point in the history
  • Loading branch information
aminlotfi committed Apr 12, 2023
1 parent 3b7a055 commit 324b732
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/Components/CalculatorMonitor.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const CalculatorMonitor = ({ currentOperand, prevOperand, operation }) => {
return (
<div className="flex flex-col items-end justify-between w-full px-4 py-5 rounded-md mb-4">
<div className="text-md mb-3">{prevOperand} {operation}</div>
<div className="text-3xl whitespace-pre-wrap break-all">{currentOperand}</div>
</div>
)
}

export default CalculatorMonitor
13 changes: 13 additions & 0 deletions src/Components/NumberButton.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {ACTIONS} from "../Templates/Calculator/Calculator";

const NumberButton = ({number, dispatch}) => {
return (
<button
onClick={() => dispatch({type: ACTIONS.SELECT_NUMBER, payload: {number}})}
className="flex item-center justify-center bg-gray-200 hover:bg-gray-300 p-4 rounded-[10px] text-xl">
{number}
</button>
)
}

export default NumberButton
13 changes: 13 additions & 0 deletions src/Components/OperationButton.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {ACTIONS} from "../Templates/Calculator/Calculator";

const OperationButton = ({operation, dispatch}) => {
return (
<button
onClick={() => dispatch({type: ACTIONS.SELECT_OPERATOR, payload: {operation}})}
className="flex item-center justify-center bg-orange-400 hover:bg-orange-500 p-4 rounded-[10px] text-xl">
{operation}
</button>
)
}

export default OperationButton
2 changes: 1 addition & 1 deletion src/Layouts/MainLayout/MainLayout.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function MainLayout({children}) {
return <>
<div className="flex items-center justify-center h-[100vh] w-full p-16 main-gradient">
<div className="flex items-center justify-center h-[100vh] w-full p-4 main-gradient">
{children}
</div>
</>
Expand Down
49 changes: 48 additions & 1 deletion src/Templates/Calculator/Calculator.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,52 @@
import CalculatorMonitor from "../../Components/CalculatorMonitor";
import {useReducer} from "react";
import NumberButton from "../../Components/NumberButton";
import OperationButton from "../../Components/OperationButton";

export const ACTIONS = {
SELECT_NUMBER: 'select-number',
SELECT_OPERATOR: 'select-operator',
CALCULATE: 'calculate',
CLEAR: 'clear',
DELETE_NUMBER: 'delete-number',
}

const reducer = (state, action) => {
switch (action.type) {
case ACTIONS.SELECT_NUMBER:
return {
...state,
currentOperand: `${state.currentOperand || '0'}${action.payload}`,
}
}
}

const Calculator = () => {
return <div>Calculator</div>;
const [{ currentOperand, prevOperand, operation }, dispatch] = useReducer(reducer, {});

return <div className="flex flex-col items-center justify-start p-4 bg-white rounded-xl w-full max-w-[500px]">
<CalculatorMonitor currentOperand={currentOperand} prevOperand={prevOperand} operation={operation}/>
<div className="grid grid-cols-4 gap-4 w-full">
<button className="col-span-2 flex item-center justify-center bg-gray-300 hover:bg-gray-400 p-4 rounded-[10px] text-xl">AC</button>
<button className="flex item-center justify-center bg-gray-300 hover:bg-gray-400 p-4 rounded-[10px] text-xl">C</button>
<OperationButton operation={'/'} dispatch={dispatch} />
<NumberButton number={7} dispatch={dispatch}/>
<NumberButton number={8} dispatch={dispatch}/>
<NumberButton number={9} dispatch={dispatch}/>
<OperationButton operation={'*'} dispatch={dispatch} />
<NumberButton number={4} dispatch={dispatch}/>
<NumberButton number={5} dispatch={dispatch}/>
<NumberButton number={6} dispatch={dispatch}/>
<OperationButton operation={'-'} dispatch={dispatch} />
<NumberButton number={1} dispatch={dispatch}/>
<NumberButton number={2} dispatch={dispatch}/>
<NumberButton number={3} dispatch={dispatch}/>
<OperationButton operation={'+'} dispatch={dispatch} />
<NumberButton number={0} dispatch={dispatch}/>
<NumberButton number={'.'} dispatch={dispatch}/>
<button className="col-span-2 flex item-center justify-center bg-green-500 hover:bg-green-600 p-4 rounded-[10px] text-xl">=</button>
</div>
</div>;
}

export default Calculator;

0 comments on commit 324b732

Please sign in to comment.