refactor: improvements in UI and book consultion option
- Login is not responsive - Add country field in book consultant any country - correct form submit of book consultant in mobile view - updatr email in privacy page - make home page slider responsive - redirect login whentokenexpired
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
"use client";
|
||||
import React, { createContext, useContext, useState, useEffect } from "react";
|
||||
import axios from 'axios';
|
||||
|
||||
|
||||
const CurrencyContext = createContext();
|
||||
|
||||
@@ -15,6 +17,7 @@ export const CurrencyProvider = ({ children }) => {
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [error, setError] = useState(null);
|
||||
|
||||
|
||||
const fetchExchangeRates = async () => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
@@ -23,60 +26,74 @@ export const CurrencyProvider = ({ children }) => {
|
||||
const cachedData = localStorage.getItem("exchangeRates");
|
||||
const cached = cachedData ? JSON.parse(cachedData) : null;
|
||||
|
||||
if (
|
||||
cached &&
|
||||
new Date().getTime() - cached.timestamp < 24 * 60 * 60 * 1000
|
||||
) {
|
||||
if (cached && new Date().getTime() - cached.timestamp < 24 * 60 * 60 * 1000) {
|
||||
setExchangeRates(cached.rates);
|
||||
setIsLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const currencies = Object.keys(SUPPORTED_CURRENCIES)
|
||||
.filter((key) => key !== "INR")
|
||||
.join(",");
|
||||
try {
|
||||
const currencies = Object.keys(SUPPORTED_CURRENCIES)
|
||||
.filter((key) => key !== "INR")
|
||||
.join(",");
|
||||
|
||||
const response = await fetch(
|
||||
`https://apilayer.net/api/live?access_key=9bcb30907dee1cda9866f7b49f0f8def¤cies=${currencies}&source=INR&format=1`
|
||||
);
|
||||
const response = await axios.get("https://apilayer.net/api/live", {
|
||||
params: {
|
||||
access_key: "9bcb30907dee1cda9866f7b49f0f8def",
|
||||
currencies: currencies,
|
||||
source: "INR",
|
||||
format: 1,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to fetch exchange rates");
|
||||
const data = response.data;
|
||||
|
||||
if (!data.success) {
|
||||
throw new Error(data.error?.info || "API request failed");
|
||||
}
|
||||
|
||||
const rates = Object.keys(SUPPORTED_CURRENCIES).reduce(
|
||||
(acc, currency) => {
|
||||
if (currency === "INR") {
|
||||
acc[currency] = 1;
|
||||
} else {
|
||||
const rate = data.quotes?.[`INR${currency}`];
|
||||
if (!rate) {
|
||||
throw new Error(`Rate not found for ${currency}`);
|
||||
}
|
||||
acc[currency] = rate;
|
||||
}
|
||||
return acc;
|
||||
},
|
||||
{}
|
||||
);
|
||||
|
||||
localStorage.setItem(
|
||||
"exchangeRates",
|
||||
JSON.stringify({ rates, timestamp: new Date().getTime() })
|
||||
);
|
||||
|
||||
setExchangeRates(rates);
|
||||
} catch (error) {
|
||||
console.error("Error fetching exchange rates:", error);
|
||||
|
||||
setError("Failed to load currency conversion rates. Please try again later.");
|
||||
|
||||
if (cached) {
|
||||
console.log("Using older cached rates as fallback");
|
||||
setExchangeRates(cached.rates);
|
||||
} else {
|
||||
setExchangeRates(null);
|
||||
}
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
if (!data.success) {
|
||||
throw new Error(data.error?.info || "API request failed");
|
||||
}
|
||||
|
||||
const rates = Object.keys(SUPPORTED_CURRENCIES).reduce(
|
||||
(acc, currency) => {
|
||||
if (currency === "INR") {
|
||||
acc[currency] = 1;
|
||||
} else {
|
||||
const rate = data.quotes?.[`INR${currency}`];
|
||||
acc[currency] = rate || null;
|
||||
}
|
||||
return acc;
|
||||
},
|
||||
{}
|
||||
);
|
||||
|
||||
const ratesData = {
|
||||
rates,
|
||||
timestamp: data.timestamp * 1000,
|
||||
};
|
||||
|
||||
localStorage.setItem("exchangeRates", JSON.stringify(ratesData));
|
||||
setExchangeRates(rates);
|
||||
} catch (err) {
|
||||
setError("Error fetching exchange rates");
|
||||
console.error("Exchange rate fetch error:", err);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
} catch (error) {
|
||||
console.error("Error in exchange rate handling:", error);
|
||||
setError("Failed to load currency conversion rates");
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const convertPrice = (price) => {
|
||||
if (!price || typeof price !== "number") return price;
|
||||
if (!exchangeRates || !exchangeRates[selectedCurrency]) return price;
|
||||
|
||||
Reference in New Issue
Block a user