Hey everyone,
I got a file which gets the users currency using timezones:
"use client"
import { useEffect, useState } from 'react';
export default function CalcPrices({ price }) {
const [valuta, setValuta] = useState('');
useEffect(() => {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone
let detectedCurrency = null;
if (timezone.startsWith('Europe')) {
detectedCurrency = timezone === 'Europe/London' ? 'gbp' : 'eur';
} else if (timezone === 'America/Toronto' || timezone === 'America/Vancouver' || timezone === 'America/Edmonton' || timezone === 'America/St_Johns' || timezone === 'America/Winnipeg' || timezone === 'America/Halifax') {
detectedCurrency = 'cad';
} else if (timezone.startsWith('Australia')) {
detectedCurrency = 'aud';
} else {
detectedCurrency = 'usd';
}
setValuta(detectedCurrency)
}, []);
return(
<p className='text-xl font-medium text-secondary-50'>
{valuta == "eur" ? `\u20AC${(price * 0.897).toFixed(2)}` :
<>
{valuta == "gbp" ? `£${(price * 0.772).toFixed(2)}` :
<>
{valuta == "aud" ? `AU$${(price * 1.472).toFixed(2)}` :
<>
{valuta == "cad" ? `CA$${(price * 1.318).toFixed(2)}` :
<>
{valuta ? <> ${(price * 1).toFixed(2)} </> :
<span className='animate-pulse text-lg padding-2 text-secondary-100 bg-secondary-100 rounded'>$2.00</span>
}
</>
}
</>
}
</>
}
</>
}
</p>
)
}```
The only issue is, the prices are static, I found an API which I can use to get the latest exchange rate. Here is my API script:
```js
export default async function getCurrency(){
const rep = await fetch('https://api.exchangerate.host/latest?base=usd', { next: { revalidate: 3600 } })
if(!rep.ok) {
throw new Error('failed to fetch exchange rate');
}
return await rep.json()
}```
The only issue is, I'm not sure how to pass the props. I tried calling the user currency like this:
`{calcPrices}` but this doesn't work. I have to use `<CalcPrices />`, is there a way to expert the currency as a prop instead of component?
idk how to pass it to the client component