|
| 1 | +import React, { useState, useEffect, useRef } from 'react'; |
| 2 | + |
| 3 | +export default function CryptoCalculatorModal({ coinData, onClose }) { |
| 4 | + const [amount, setAmount] = useState(''); |
| 5 | + const [selectedCoinId, setSelectedCoinId] = useState(coinData[0]?.id || ''); |
| 6 | + |
| 7 | + const [searchTerm, setSearchTerm] = useState(''); |
| 8 | + const [isDropdownOpen, setIsDropdownOpen] = useState(false); |
| 9 | + const dropdownRef = useRef(null); |
| 10 | + |
| 11 | + const [result, setResult] = useState(null); |
| 12 | + |
| 13 | + useEffect(() => { |
| 14 | + setResult(null); |
| 15 | + }, [amount, selectedCoinId]); |
| 16 | + |
| 17 | + useEffect(() => { |
| 18 | + function handleClickOutside(event) { |
| 19 | + if (dropdownRef.current && !dropdownRef.current.contains(event.target)) { |
| 20 | + setIsDropdownOpen(false); |
| 21 | + } |
| 22 | + } |
| 23 | + document.addEventListener("mousedown", handleClickOutside); |
| 24 | + return () => { |
| 25 | + document.removeEventListener("mousedown", handleClickOutside); |
| 26 | + }; |
| 27 | + }, [dropdownRef]); |
| 28 | + |
| 29 | + |
| 30 | + const isButtonDisabled = !amount || parseFloat(amount) <= 0; |
| 31 | + |
| 32 | + const handleCalculate = () => { |
| 33 | + const coin = coinData.find((c) => c.id === selectedCoinId); |
| 34 | + const inputAmount = parseFloat(amount); |
| 35 | + if (!coin) return; |
| 36 | + |
| 37 | + const changePercent = coin.price_change_percentage_24h; |
| 38 | + const changeDecimal = changePercent / 100; |
| 39 | + const profitOrLoss = inputAmount * changeDecimal; |
| 40 | + const finalAmount = inputAmount + profitOrLoss; |
| 41 | + |
| 42 | + setResult({ |
| 43 | + finalAmount: finalAmount.toFixed(2), |
| 44 | + profitOrLoss: profitOrLoss.toFixed(2), |
| 45 | + isProfit: profitOrLoss >= 0, |
| 46 | + error: null, |
| 47 | + }); |
| 48 | + }; |
| 49 | + |
| 50 | + const filteredCoins = coinData.filter(coin => |
| 51 | + coin.name.toLowerCase().includes(searchTerm.toLowerCase()) || |
| 52 | + coin.symbol.toLowerCase().includes(searchTerm.toLowerCase()) |
| 53 | + ); |
| 54 | + |
| 55 | + const selectedCoin = coinData.find(c => c.id === selectedCoinId); |
| 56 | + |
| 57 | + return ( |
| 58 | + <div className="modal-overlay" onClick={onClose}> |
| 59 | + <div className="modal" onClick={(e) => e.stopPropagation()}> |
| 60 | + <button className="modal-close-button" onClick={onClose}> |
| 61 | + × |
| 62 | + </button> |
| 63 | + <h2>24h Profit/Loss Calculator</h2> |
| 64 | + |
| 65 | + <div className="modal-input-group"> |
| 66 | + <label htmlFor="amount">Amount (USD)</label> |
| 67 | + <input |
| 68 | + id="amount" |
| 69 | + type="number" |
| 70 | + min="0" |
| 71 | + value={amount} |
| 72 | + onChange={(e) => setAmount(e.target.value)} |
| 73 | + placeholder="e.g., 100" |
| 74 | + className="modal-input" |
| 75 | + /> |
| 76 | + </div> |
| 77 | + |
| 78 | + <div className="modal-input-group"> |
| 79 | + <label htmlFor="crypto-search">Cryptocurrency</label> |
| 80 | + <div className="modal-dropdown-container" ref={dropdownRef}> |
| 81 | + <input |
| 82 | + id="crypto-search" |
| 83 | + type="text" |
| 84 | + className="modal-input" |
| 85 | + value={isDropdownOpen ? searchTerm : (selectedCoin?.name || '')} |
| 86 | + onChange={(e) => { |
| 87 | + setSearchTerm(e.target.value); |
| 88 | + setIsDropdownOpen(true); |
| 89 | + }} |
| 90 | + onFocus={() => setIsDropdownOpen(true)} |
| 91 | + placeholder="Search coin..." |
| 92 | + /> |
| 93 | + {isDropdownOpen && ( |
| 94 | + <div className="modal-dropdown-list"> |
| 95 | + {filteredCoins.length > 0 ? ( |
| 96 | + filteredCoins.map((coin) => ( |
| 97 | + <div |
| 98 | + key={coin.id} |
| 99 | + className="modal-dropdown-item" |
| 100 | + onClick={() => { |
| 101 | + setSelectedCoinId(coin.id); |
| 102 | + setSearchTerm(''); |
| 103 | + setIsDropdownOpen(false); |
| 104 | + }} |
| 105 | + > |
| 106 | + <img src={coin.image} alt={coin.symbol} style={{ width: 20, height: 20 }}/> |
| 107 | + {coin.name} ({coin.symbol.toUpperCase()}) |
| 108 | + </div> |
| 109 | + )) |
| 110 | + ) : ( |
| 111 | + <div className="modal-dropdown-item-none"> |
| 112 | + No results found |
| 113 | + </div> |
| 114 | + )} |
| 115 | + </div> |
| 116 | + )} |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + |
| 120 | + <button |
| 121 | + onClick={handleCalculate} |
| 122 | + className="modal-button" |
| 123 | + disabled={isButtonDisabled} |
| 124 | + > |
| 125 | + Calculate |
| 126 | + </button> |
| 127 | + |
| 128 | + {result && ( |
| 129 | + <div className="modal-result"> |
| 130 | + {result.error ? ( |
| 131 | + <p style={{ color: 'var(--danger)' }}>{result.error}</p> |
| 132 | + ) : ( |
| 133 | + <> |
| 134 | + <p>Your ${amount} invested 24h ago would now be worth:</p> |
| 135 | + <h3 |
| 136 | + style={{ |
| 137 | + color: result.isProfit ? '#16a34a' : 'var(--danger)', |
| 138 | + fontSize: '1.5rem', |
| 139 | + margin: '0.5rem 0', |
| 140 | + }} |
| 141 | + > |
| 142 | + ${result.finalAmount} |
| 143 | + </h3> |
| 144 | + <p> |
| 145 | + A {result.isProfit ? 'profit' : 'loss'} of{' '} |
| 146 | + <strong> |
| 147 | + {result.isProfit ? '+' : ''}${result.profitOrLoss} |
| 148 | + </strong> |
| 149 | + </p> |
| 150 | + </> |
| 151 | + )} |
| 152 | + </div> |
| 153 | + )} |
| 154 | + </div> |
| 155 | + </div> |
| 156 | + ); |
| 157 | +} |
0 commit comments