feat: move currency converter to server

This commit is contained in:
Vasily Zubarev
2025-04-04 13:49:51 +02:00
parent bd6cd2c25c
commit 1bb6447141
3 changed files with 87 additions and 58 deletions

View File

@@ -1,4 +1,3 @@
import { getCurrencyRate } from "@/lib/currency-scraper"
import { formatCurrency } from "@/lib/utils"
import { format, startOfDay } from "date-fns"
import { Loader2 } from "lucide-react"
@@ -75,3 +74,24 @@ export const FormConvertCurrency = ({
</div>
)
}
async function getCurrencyRate(currencyCodeFrom: string, currencyCodeTo: string, date: Date): Promise<number> {
try {
const formattedDate = format(date, "yyyy-MM-dd")
const url = `/api/currency?from=${currencyCodeFrom}&to=${currencyCodeTo}&date=${formattedDate}`
const response = await fetch(url)
if (!response.ok) {
const errorData = await response.json()
console.error("Currency API error:", errorData.error)
return 0
}
const data = await response.json()
return data.rate
} catch (error) {
console.error("Error fetching currency rate:", error)
return 0
}
}