|
| 1 | +import { useEffect, useState } from 'react'; |
| 2 | +import { Icon } from '@mdi/react'; |
| 3 | +import { mdiCashSync } from '@mdi/js'; |
| 4 | +import { mdiCurrencyUsd } from '@mdi/js'; |
| 5 | +import { mdiCurrencyEur } from '@mdi/js'; |
| 6 | +import { mdiCurrencyRub } from '@mdi/js'; |
| 7 | +import { useTranslation } from 'react-i18next'; |
| 8 | +import SEO from '../../components/SEO.jsx'; |
| 9 | +import { InputFilter } from '../../components/filter/index.js'; |
| 10 | +import useItemsData from '../../features/items/index.js'; |
| 11 | +import itemSearch from '../../modules/item-search.js'; |
| 12 | +import useKeyPress from '../../hooks/useKeyPress.jsx'; |
| 13 | + |
| 14 | +import './index.css'; |
| 15 | + |
| 16 | +function Converter() { |
| 17 | + const { t } = useTranslation(); |
| 18 | + |
| 19 | + const [RUBFilter, setRUBFilter] = useState(); |
| 20 | + const [USDFilter, setUSDFilter] = useState(); |
| 21 | + const [EURFilter, setEURFilter] = useState(); |
| 22 | + const [EURRate, setEURRate] = useState(159); |
| 23 | + const [USDRate, setUSDRate] = useState(149); |
| 24 | + const { data: items } = useItemsData(); |
| 25 | + |
| 26 | + |
| 27 | + const enterPress = useKeyPress('Enter'); |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + let euroConversion; |
| 31 | + let dollarConversion; |
| 32 | + const dollarsearch = itemSearch(items, 'Dollars'); |
| 33 | + const dollarItem = dollarsearch.find(item => item.name === 'Dollars'); |
| 34 | + if (dollarItem) { |
| 35 | + dollarConversion = dollarItem.buyForBest.price; |
| 36 | + } |
| 37 | + const euroSearch = itemSearch(items, 'Euros'); |
| 38 | + const euroItem = euroSearch.find(item => item.name === 'Euros'); |
| 39 | + if (euroItem) { |
| 40 | + euroConversion = euroItem.buyForBest.price; |
| 41 | + } |
| 42 | + setEURRate(euroConversion); |
| 43 | + setUSDRate(dollarConversion); |
| 44 | + }, [items, setEURRate, setUSDRate]); |
| 45 | + |
| 46 | + useEffect(() => { |
| 47 | + if (enterPress) { |
| 48 | + resetInput(); |
| 49 | + } |
| 50 | + }, [enterPress]); |
| 51 | + |
| 52 | + function exchangeRate() { |
| 53 | + const exchangeRates = { |
| 54 | + USD: { EUR: USDRate / EURRate, RUB: USDRate }, |
| 55 | + EUR: { USD: EURRate / USDRate, RUB: EURRate }, |
| 56 | + RUB: { USD: 1 / USDRate, EUR: 1 / EURRate }, |
| 57 | + }; |
| 58 | + return exchangeRates; |
| 59 | + } |
| 60 | + |
| 61 | + function inputFilter(fromCurrency, amount) { |
| 62 | + if (fromCurrency === 'RUB') { |
| 63 | + setRUBFilter(amount); |
| 64 | + convertCurrency(fromCurrency, amount); |
| 65 | + } else if (fromCurrency === 'USD') { |
| 66 | + setUSDFilter(amount); |
| 67 | + convertCurrency(fromCurrency, amount); |
| 68 | + } else if (fromCurrency === 'EUR') { |
| 69 | + setEURFilter(amount); |
| 70 | + convertCurrency(fromCurrency, amount); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + function convertCurrency(fromCurrency, amount) { |
| 75 | + if (fromCurrency === 'RUB') { |
| 76 | + let convertedAmount = Math.floor(amount * (exchangeRate()[fromCurrency]['USD'])); |
| 77 | + setUSDFilter(convertedAmount); |
| 78 | + convertedAmount = Math.floor(amount * (exchangeRate()[fromCurrency]['EUR'])); |
| 79 | + setEURFilter(convertedAmount); |
| 80 | + } |
| 81 | + else if (fromCurrency === 'USD') { |
| 82 | + let convertedAmount = Math.floor(amount * (exchangeRate()[fromCurrency]['EUR'])); |
| 83 | + setEURFilter(convertedAmount); |
| 84 | + convertedAmount = Math.floor(amount * (exchangeRate()[fromCurrency]['RUB'])); |
| 85 | + setRUBFilter(convertedAmount); |
| 86 | + } else if (fromCurrency === 'EUR') { |
| 87 | + let convertedAmount = Math.floor(amount * (exchangeRate()[fromCurrency]['USD'])); |
| 88 | + setUSDFilter(convertedAmount); |
| 89 | + convertedAmount = Math.floor(amount * (exchangeRate()[fromCurrency]['RUB'])); |
| 90 | + setRUBFilter(convertedAmount); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + function resetInput() { |
| 95 | + setRUBFilter(''); |
| 96 | + setUSDFilter(''); |
| 97 | + setEURFilter(''); |
| 98 | + } |
| 99 | + |
| 100 | + return [ |
| 101 | + <SEO |
| 102 | + title={`${t('Converter')} - ${t('Escape from Tarkov')} - ${t('Tarkov.dev')}`} |
| 103 | + description={t('converter-page-description', 'Search Escape from Tarkov players. Convert from one currancy to another.')} |
| 104 | + key="seo-wrapper" |
| 105 | + />, |
| 106 | + <div className={'page-wrapper'} key="converter-page-wrapper"> |
| 107 | + <div className="converter-headline-wrapper" key="converter-headline"> |
| 108 | + <h1 className="converter-page-title"> |
| 109 | + <Icon path={mdiCashSync} size={1.5} className="icon-with-text" /> |
| 110 | + {t('Converter')} |
| 111 | + </h1> |
| 112 | + </div> |
| 113 | + <div> |
| 114 | + <p> |
| 115 | + {t('Convert one currency to another')} |
| 116 | + </p> |
| 117 | + </div> |
| 118 | + <div className='currency-input'> |
| 119 | + <InputFilter |
| 120 | + value={RUBFilter} |
| 121 | + placeholder={'0'} |
| 122 | + type="number" |
| 123 | + onChange={(event) => { |
| 124 | + inputFilter('RUB', event.target.value); |
| 125 | + }} |
| 126 | + /> |
| 127 | + <Icon path={mdiCurrencyRub} size={1.5} className="icon-with-text" /> |
| 128 | + </div> |
| 129 | + <div className='currency-input'> |
| 130 | + <InputFilter |
| 131 | + value={USDFilter} |
| 132 | + placeholder={'0'} |
| 133 | + type="number" |
| 134 | + onChange={(event) => { |
| 135 | + inputFilter('USD', event.target.value); |
| 136 | + }} |
| 137 | + /> |
| 138 | + <Icon path={mdiCurrencyUsd} size={1.5} className="icon-with-text" /> |
| 139 | + </div> |
| 140 | + <div className='currency-input'> |
| 141 | + <InputFilter |
| 142 | + value={EURFilter} |
| 143 | + placeholder={'0'} |
| 144 | + type="number" |
| 145 | + onChange={(event) => { |
| 146 | + inputFilter('EUR', event.target.value); |
| 147 | + }} |
| 148 | + /> |
| 149 | + <Icon path={mdiCurrencyEur} size={1.5} className="icon-with-text" /> |
| 150 | + </div> |
| 151 | + <button className="reset-button" onClick={resetInput}>{t('Reset')}</button> |
| 152 | + </div> |
| 153 | + |
| 154 | + ]; |
| 155 | + |
| 156 | +} |
| 157 | +export default Converter; |
0 commit comments