hiteshchoudhary / chai-aur-react

chai aur react series on youtube
8.26k stars 1.06k forks source link

"From Currency Type" BOX OPTION is not working in your code #54

Open parthh1112 opened 10 months ago

parthh1112 commented 10 months ago

"From Currency Type" box is showing the options of different currency but when we are selecting the option it is not changing it remain on USD option only

i have resolve this bug you can check it from here

https://github.com/parthh1112/React/tree/main/11CustomHookCurrencyConverter/src

AliGates915 commented 9 months ago

Now check: because adding new Features like Reset Button https://github.com/AliGates915/React-Basic-Projects/tree/main/currency-converter/src

Currency Converter

DraGonoff69 commented 9 months ago

import { useState } from 'react' import {Input} from '../components/index' import useCurrencyInfo from '../hooks/useCurrencyInfo'

function CurrencyConvertor() {

const [amount, setAmount] = useState() const [from, setFrom] = useState("usd") const [to, setTo] = useState("inr") const [convertedAmount, setConvertedAmount] = useState(0)

const currencyInfo = useCurrencyInfo(from)

const options = Object.keys(currencyInfo)

const swap = () => { setFrom(to) setTo(from) setConvertedAmount(amount) setAmount(convertedAmount) }

const convert = () => { setConvertedAmount(amount * currencyInfo[to]) }

return ( <div className="w-full h-screen flex flex-wrap justify-center items-center bg-cover bg-no-repeat" style={{ backgroundImage: url('https://images.pexels.com/photos/3532540/pexels-photo-3532540.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2'), }}

{ e.preventDefault(); convert()
                }}
            >
                <div className="w-full mb-1">
                    <Input
                        label="From"
                        amount={amount}
                        currencyOptions={options}
                        onCurrencyChange={(currency) => setFrom(currency)}
                        selectCurrency={from}
                        onAmountChange={(amount) => setAmount(amount)}
                    />
                </div>
                <div className="relative w-full h-0.5">
                    <button
                        type="button"
                        className="absolute left-1/2 -translate-x-1/2 -translate-y-1/2 border-2 border-white rounded-md bg-blue-600 text-white px-2 py-0.5"
                        onClick={swap}
                    >
                        swap
                    </button>
                </div>
                <div className="w-full mt-1 mb-4">
                    <Input
                        label="To"
                        amount={convertedAmount}
                        currencyOptions={options}
                        onCurrencyChange={(currency) => setTo(currency)}
                        selectCurrency={to}
                        amountDisable
                    />
                </div>
                <button type="submit" className="w-full bg-blue-600 text-white px-4 py-3 rounded-lg">
                    Convert {from.toUpperCase()} to {to.toUpperCase()}
                </button>
            </form>
        </div>
    </div>
</div>

); }

export default CurrencyConvertor

This is the correct code. Replace it with your current app.jsx code . then it should work properly

sameekshaagupta commented 5 months ago

new api link:https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/${currency}.json

sameekshaagupta commented 5 months ago

also there was some error in input box.try using this code `

<InputBox label="From" amount={amount} currencyOptions={options} onCurrencyChange={(currency) => setFrom(currency)} selectCurrency={from} onAmountChange={(amount) => setAmount(amount)} />

                <div className="w-full mt-1 mb-4">
                    <InputBox
                        label="To"
                        amount={convertedAmount}
                        currencyOptions={options}
                        onCurrencyChange={(currency) => setTo(currency)}
                        selectCurrency={to}
                        amountDisable
                        onAmountChange={(amount) => setAmount(amount)}
                    />
                </div>`
AliGates915 commented 5 months ago
setFrom(currency)} selectCurrency={from} onAmountChange={(amount) => setAmount(amount)} />
        <div className="relative w-full h-0.5">
          <button
            type="button"
            className="absolute left-1/2 -translate-x-1/2 -translate-y-1/2 
          border-2 border-white rounded-md bg-blue-600 text-2xl text-white 
          px-2 py-0.5"
            onClick={swap}
          >Swap
          </button>
        </div>
        <div className="w-full mt-1 mb-4">
          <InputBox
            label="To"
            amount={convertedAmount}
            currencyOption={options}
            onCurrencyChange={(currency) => setTo(currency)}
            selectCurrency={to}
            amountDisable
          />
        </div> 

"Try run this code at the last after the amountDisable no need to add other line of code."

AliGates915 commented 5 months ago

new api link:https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/${currency}.json

"Your API works on localhost and can be used anywhere. However, I am unable to access your API because it is not open."

AliGates915 commented 5 months ago

https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/usd.json Now @sameekshaagupta this API is working perfectly, you change ${currency} instead of usd.

https://github.com/hiteshchoudhary/chai-aur-react/issues/125#issue-2321704449