import { useRef, useState } from "react"; import { Modal, Button, Row, Col } from "react-bootstrap" type Props = { isOpen: boolean, onClose: () => void, } type Result = { pizza1?: { diameter?: number, area?: number, pricePerM?: number, }, pizza2?: { diameter?: number, area?: number, pricePerM?: number, } choice?: number, ratio?: number, diameterDiff?: number, } /** Modální dialog pro výpočet výhodnosti pizzy. */ export default function PizzaCalculatorModal({ isOpen, onClose }: Readonly) { const diameter1Ref = useRef(null); const price1Ref = useRef(null); const diameter2Ref = useRef(null); const price2Ref = useRef(null); const [result, setResult] = useState(null); const recalculate = () => { const r: Result = { ...result } // 1. pizza if (diameter1Ref.current?.value) { const diameter1 = parseInt(diameter1Ref.current?.value); if (!r.pizza1) { r.pizza1 = {}; } if (diameter1 && diameter1 > 0) { r.pizza1.diameter = diameter1; r.pizza1.area = Math.PI * Math.pow(diameter1 / 2, 2); if (price1Ref.current?.value) { const price1 = parseInt(price1Ref.current?.value); if (price1) { r.pizza1.pricePerM = price1 / r.pizza1.area; } else { r.pizza1.pricePerM = undefined; } } } else { r.pizza1.area = undefined; } } // 2. pizza if (diameter2Ref.current?.value) { const diameter2 = parseInt(diameter2Ref.current?.value); if (!r.pizza2) { r.pizza2 = {}; } if (diameter2 && diameter2 > 0) { r.pizza2.diameter = diameter2; r.pizza2.area = Math.PI * Math.pow(diameter2 / 2, 2); if (price2Ref.current?.value) { const price2 = parseInt(price2Ref.current?.value); if (price2) { r.pizza2.pricePerM = price2 / r.pizza2.area; } else { r.pizza2.pricePerM = undefined; } } } else { r.pizza2.area = undefined; } } // Srovnání if (r.pizza1?.pricePerM && r.pizza2?.pricePerM && r.pizza1.diameter && r.pizza2.diameter) { r.choice = r.pizza1.pricePerM < r.pizza2.pricePerM ? 1 : 2; const bigger = r.pizza1.pricePerM > r.pizza2.pricePerM ? r.pizza1.pricePerM : r.pizza2.pricePerM; const smaller = r.pizza1.pricePerM < r.pizza2.pricePerM ? r.pizza1.pricePerM : r.pizza2.pricePerM; r.ratio = (bigger / smaller) - 1; r.diameterDiff = Math.abs(r.pizza1.diameter - r.pizza2.diameter); } else { r.choice = undefined; r.ratio = undefined; r.diameterDiff = undefined; } setResult(r); } const close = () => { setResult(null); onClose(); } return Pizza kalkulačka

Zadejte parametry pizzy pro jejich srovnání.

e.stopPropagation()} /> e.stopPropagation()} /> e.stopPropagation()} /> e.stopPropagation()} /> {result?.pizza1?.area &&

Plocha: {Math.round(result.pizza1.area * 10) / 10} cm²

} {result?.pizza1?.pricePerM &&

Cena za m²: {Math.round(result.pizza1.pricePerM * 1000000) / 100}

} {result?.pizza2?.area &&

Plocha: {Math.round(result.pizza2.area * 10) / 10} cm²

} {result?.pizza2?.pricePerM &&

Cena za m²: {Math.round(result.pizza2.pricePerM * 1000000) / 100}

}
{(result?.choice && result?.ratio && result?.ratio > 0 && result?.diameterDiff != null &&

{result.choice}. pizza je zhruba o {Math.round(result.ratio * 1000) / 10}% výhodnější než {result.choice === 1 ? "2" : "1"}. pizza.

) || ''}
}