Skip to content

Commit

Permalink
Added Discount Calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitkumawat1m committed Oct 24, 2023
1 parent f806a08 commit 4d2b67d
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions packages/kitchen-sink/src/examples/discount-calculator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { useState } from 'react';
import { block } from 'million/react';

interface Results {
discountPrice: number;
finalPrice: number;
}



const DiscountCalculator = block(() => {
const [originalPrice, setOriginalPrice] = useState<number | string>('');
const [discountRate, setDiscountRate] = useState<number | string>('');
const [results, setResults] = useState<Results | null>(null);


const calculateDiscount =()=>{
const p = parseFloat(originalPrice as string);
const r = parseFloat(discountRate as string) / 100;


if (isNaN(p) || isNaN(r) || p <= 0 || r <= 0 ) {
alert('Please enter valid numbers.');
return;
}

const discountPrice = p * r;
const finalPrice = p - discountPrice;

setResults({
discountPrice,
finalPrice,
});
}


return (
<div className='Discount Calculator'>
<h1>Discount Calculator</h1>
<div>
<label>Original Price ($): </label>
<input
type="number"
value={originalPrice}
onChange={(e) => setOriginalPrice(e.target.value)}
style={{ display: 'block', marginBottom: '15px' }}
/>
</div>
<div>
<label>Discount (% off): </label>
<input
type="number"
value={discountRate}
onChange={(e) => setDiscountRate(e.target.value)}
style={{ display: 'block', marginBottom: '15px' }}
/>
</div>
<button onClick={calculateDiscount}>Calculate</button>


{results && (
<div className="results">
<h2>Results:</h2>
<div>Final Price : ${results.finalPrice.toFixed(2)}</div>
<div>You Save : ${results.discountPrice.toFixed(2)}</div>
</div>
)}
</div>
);
});

export default DiscountCalculator;

0 comments on commit 4d2b67d

Please sign in to comment.