7
This commit is contained in:
parent
606636bded
commit
537c499699
@ -1,4 +1,4 @@
|
|||||||
import { mdiCalculator, mdiHistory } from '@mdi/js'
|
import { mdiCalculator } from '@mdi/js'
|
||||||
import Head from 'next/head'
|
import Head from 'next/head'
|
||||||
import React, { useState, ReactElement } from 'react'
|
import React, { useState, ReactElement } from 'react'
|
||||||
import CardBox from '../components/CardBox'
|
import CardBox from '../components/CardBox'
|
||||||
@ -11,23 +11,37 @@ import BaseButtons from '../components/BaseButtons'
|
|||||||
import FormField from '../components/FormField'
|
import FormField from '../components/FormField'
|
||||||
|
|
||||||
const CalculatorPage = () => {
|
const CalculatorPage = () => {
|
||||||
const [input, setInput] = useState('')
|
const [val1, setVal1] = useState('')
|
||||||
const [results, setResults] = useState({ hex: '', dec: '', oct: '', bin: '' })
|
const [val2, setVal2] = useState('')
|
||||||
|
const [base, setBase] = useState<'hex' | 'dec' | 'oct' | 'bin'>('dec')
|
||||||
|
const [operation, setOperation] = useState<'+' | '-' | '*' | '/' | 'AND' | 'OR' | 'XOR'>('+')
|
||||||
|
const [result, setResult] = useState<string | null>(null)
|
||||||
|
|
||||||
|
const parseInput = (val: string, base: number) => parseInt(val, base)
|
||||||
|
|
||||||
const handleCalculate = () => {
|
const handleCalculate = () => {
|
||||||
// Basic implementation for now - this should ideally call the backend
|
|
||||||
try {
|
try {
|
||||||
const decValue = parseInt(input, 10)
|
const baseMap = { hex: 16, dec: 10, oct: 8, bin: 2 }
|
||||||
if (isNaN(decValue)) throw new Error('Invalid input')
|
const v1 = parseInput(val1, baseMap[base])
|
||||||
|
const v2 = parseInput(val2, baseMap[base])
|
||||||
|
|
||||||
setResults({
|
if (isNaN(v1) || (val2 && isNaN(v2))) throw new Error('Invalid input')
|
||||||
hex: decValue.toString(16).toUpperCase(),
|
|
||||||
dec: decValue.toString(10),
|
let res: number
|
||||||
oct: decValue.toString(8),
|
switch(operation) {
|
||||||
bin: decValue.toString(2)
|
case '+': res = v1 + v2; break
|
||||||
})
|
case '-': res = v1 - v2; break
|
||||||
|
case '*': res = v1 * v2; break
|
||||||
|
case '/': res = v1 / v2; break
|
||||||
|
case 'AND': res = v1 & v2; break
|
||||||
|
case 'OR': res = v1 | v2; break
|
||||||
|
case 'XOR': res = v1 ^ v2; break
|
||||||
|
default: res = v1
|
||||||
|
}
|
||||||
|
|
||||||
|
setResult(res.toString(baseMap[base]).toUpperCase())
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
alert('Error: Please enter a valid decimal number')
|
alert('Error: Invalid operation')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,28 +55,29 @@ const CalculatorPage = () => {
|
|||||||
{''}
|
{''}
|
||||||
</SectionTitleLineWithButton>
|
</SectionTitleLineWithButton>
|
||||||
<CardBox className="mb-6">
|
<CardBox className="mb-6">
|
||||||
<FormField label="Enter Decimal Number">
|
<div className="flex gap-2 mb-4">
|
||||||
<input
|
{(['hex', 'dec', 'oct', 'bin'] as const).map(b => (
|
||||||
type="text"
|
<BaseButton key={b} color={base === b ? 'info' : 'light'} label={b.toUpperCase()} onClick={() => setBase(b)} />
|
||||||
value={input}
|
))}
|
||||||
onChange={(e) => setInput(e.target.value)}
|
</div>
|
||||||
className="w-full p-2 border rounded"
|
<div className="flex gap-4">
|
||||||
placeholder="e.g. 255"
|
<FormField label="Value 1" className="flex-1"><input value={val1} onChange={(e) => setVal1(e.target.value)} className="w-full p-2 border rounded" /></FormField>
|
||||||
/>
|
<FormField label="Operation" className="w-24">
|
||||||
</FormField>
|
<select value={operation} onChange={(e) => setOperation(e.target.value as any)} className="w-full p-2 border rounded">
|
||||||
|
<option value="+">+</option><option value="-">-</option><option value="*">*</option><option value="/">/</option>
|
||||||
|
<option value="AND">AND</option><option value="OR">OR</option><option value="XOR">XOR</option>
|
||||||
|
</select>
|
||||||
|
</FormField>
|
||||||
|
<FormField label="Value 2" className="flex-1"><input value={val2} onChange={(e) => setVal2(e.target.value)} className="w-full p-2 border rounded" /></FormField>
|
||||||
|
</div>
|
||||||
<BaseButtons>
|
<BaseButtons>
|
||||||
<BaseButton color="info" label="Calculate" onClick={handleCalculate} />
|
<BaseButton color="info" label="Calculate" onClick={handleCalculate} />
|
||||||
</BaseButtons>
|
</BaseButtons>
|
||||||
</CardBox>
|
</CardBox>
|
||||||
|
|
||||||
{results.dec && (
|
{result !== null && (
|
||||||
<CardBox>
|
<CardBox>
|
||||||
<div className="grid grid-cols-2 gap-4">
|
<div className="text-xl font-bold">Result: {result}</div>
|
||||||
<div><strong>Decimal:</strong> {results.dec}</div>
|
|
||||||
<div><strong>Hex:</strong> 0x{results.hex}</div>
|
|
||||||
<div><strong>Octal:</strong> {results.oct}</div>
|
|
||||||
<div><strong>Binary:</strong> {results.bin}</div>
|
|
||||||
</div>
|
|
||||||
</CardBox>
|
</CardBox>
|
||||||
)}
|
)}
|
||||||
</SectionMain>
|
</SectionMain>
|
||||||
@ -70,8 +85,6 @@ const CalculatorPage = () => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
CalculatorPage.getLayout = function getLayout(page: ReactElement) {
|
CalculatorPage.getLayout = (page: ReactElement) => <LayoutAuthenticated>{page}</LayoutAuthenticated>
|
||||||
return <LayoutAuthenticated>{page}</LayoutAuthenticated>
|
|
||||||
}
|
|
||||||
|
|
||||||
export default CalculatorPage
|
export default CalculatorPage
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user