From 5a63ee26e60d1eb397fc45cf8542f2e4fed2962c Mon Sep 17 00:00:00 2001 From: saksham vitwekar Date: Mon, 30 Mar 2026 23:29:24 +0530 Subject: [PATCH] Complete and working implementation of renderCalc.tsx | v1.0 --- .../calculate/[section]/[id]/page.tsx | 32 ++------ .../calculate/[section]/[id]/renderCalc.tsx | 74 +++++++++++++++++++ src/app/(calculators)/layoutClient.tsx | 8 +- src/app/utils/Input.tsx | 63 ++++++++-------- src/app/utils/Result.tsx | 29 ++++++++ src/app/utils/Section.tsx | 6 +- .../miscellaneous/cockcroft-gault-eq.ts | 12 +-- src/app/utils/calculators/types.ts | 5 +- 8 files changed, 161 insertions(+), 68 deletions(-) create mode 100644 src/app/(calculators)/calculate/[section]/[id]/renderCalc.tsx create mode 100644 src/app/utils/Result.tsx diff --git a/src/app/(calculators)/calculate/[section]/[id]/page.tsx b/src/app/(calculators)/calculate/[section]/[id]/page.tsx index e3e5283..827cfcb 100644 --- a/src/app/(calculators)/calculate/[section]/[id]/page.tsx +++ b/src/app/(calculators)/calculate/[section]/[id]/page.tsx @@ -1,29 +1,9 @@ -import InputComponent from "@/app/utils/Input"; -import Input from "@/app/utils/Input"; -import { CalculatorRegistry } from "@/app/utils/calculators/registry"; +import RenderCalculator from "./renderCalc"; + + export default async function Calculator({params} : {params:Promise<{section:string, id:string}>}){ const calcData : {section:string, id:string} = await params; - const calculator = CalculatorRegistry[calcData.section].calculators.find(e => e.id === calcData.id); - console.log(calculator); - return calculator ? ( - <> -
-

- {calculator?.name} -

- -

- {calculator?.desc} -

-

- Unit of the value:- {calculator?.unit} -

-
- {calculator.inputs.map(e => { - return( - - ); - })} - - ) : null; + return( + + ); } \ No newline at end of file diff --git a/src/app/(calculators)/calculate/[section]/[id]/renderCalc.tsx b/src/app/(calculators)/calculate/[section]/[id]/renderCalc.tsx new file mode 100644 index 0000000..9cfd27e --- /dev/null +++ b/src/app/(calculators)/calculate/[section]/[id]/renderCalc.tsx @@ -0,0 +1,74 @@ +"use client" + +import InputComponent from "@/app/utils/Input"; +import Input from "@/app/utils/Input"; +import Result from "@/app/utils/Result"; +import { CalculatorRegistry } from "@/app/utils/calculators/registry"; +import { Interpretation } from "@/app/utils/calculators/types"; +import { useState } from "react"; + +export default function RenderCalculator({section, id} : {section:string, id:string}){ + const calculator = CalculatorRegistry[section].calculators.find(e => e.id === id); + const [form, setForm] = useState>({}); + const [value, setValue] = useState(""); + function handleDataChange(key:string, value:string){ + setForm(prev => { + return { + ...prev, + [key]:value + } + }); + } + const [showResult, setResultStatus] = useState(false); + const [result, setResult] = useState({ + level:"none", + message:"none" + }); + console.log(form); + return calculator ? ( + <> +
+

+ {calculator?.name} +

+ +

+ {calculator?.desc} +

+

+ Unit of the value:- {calculator?.unit} +

+
+
+
{ + e.preventDefault(); + setResultStatus(true); + const calculatedValue = calculator.calc_func(form); + setValue(calculatedValue); + setResult(calculator.interpret_func(calculatedValue)); + }}> +
+
+
+ {calculator.inputs.map(e => { + return( + + ); + })} + +
+
+
+ {showResult ?

+ +

: null} +
+
+ + ) : null; +} \ No newline at end of file diff --git a/src/app/(calculators)/layoutClient.tsx b/src/app/(calculators)/layoutClient.tsx index b27b625..3488f79 100644 --- a/src/app/(calculators)/layoutClient.tsx +++ b/src/app/(calculators)/layoutClient.tsx @@ -7,11 +7,13 @@ const font = Roboto({}); export default function LayoutClient({children} : {children:React.ReactNode}){ const [navbar, setNavbar] = useState(false); return( -
+
-
- {children} +
+
+ {children} +
diff --git a/src/app/utils/Input.tsx b/src/app/utils/Input.tsx index 31e3467..e63cf21 100644 --- a/src/app/utils/Input.tsx +++ b/src/app/utils/Input.tsx @@ -2,41 +2,41 @@ import { useState } from "react"; import { Input } from "./calculators/types"; -export default function InputComponent({id, type, inputOptions, min, max, required, defaultUnit, unitOptions, name, placeholder} : Input){ +export default function InputComponent({id, type, inputOptions, min, max, required, defaultUnit, unitOptions, name, placeholder, handleDataChange} : Input){ const [showError, setErrorStatus] = useState(false); const [error, setError] = useState(); const [option, setOption] = useState(""); - console.log(id); if(type === "number"){ - console.log(min); return( <>
{name} - { - if(e.target.value === ""){ - return; - } - const val = Number(e.target.value); - if(min !== undefined && val < min){ - setError(`Value must be greater than or equal to ${min}`) - setErrorStatus(true); - }else if(max !== undefined && val > max){ - setError(`Value must be smaller than or equal to ${max}`); - setErrorStatus(true); - }else{ - setError(""); - setErrorStatus(false); - } - }} - /> + { + if(e.target.value === ""){ + return; + } + const val = Number(e.target.value); + if(min !== undefined && val < min){ + setError(`Value must be greater than or equal to ${min}`) + setErrorStatus(true); + }else if(max !== undefined && val > max){ + setError(`Value must be smaller than or equal to ${max}`); + setErrorStatus(true); + }else{ + setError(""); + setErrorStatus(false); + handleDataChange !== undefined ? handleDataChange(id, e.target.value) : null; + } + }} + /> {showError ?

{error}

: null}
@@ -46,11 +46,14 @@ export default function InputComponent({id, type, inputOptions, min, max, requir
{name}