My code
`import { useEffect, useState } from 'react';
import { useCart } from 'medusa-react';
import Cookies from 'universal-cookie';
interface GeoData {
country_code: string;
}
const CurrencySwitcher = () => {
const { cart, updateCart } = useCart();
const [visited, setVisited] = useState(false);
const cookies = new Cookies();
const changeRegionId = (regionId: string) => {
cart?.id && updateCart.mutate({ region_id: regionId });
};
useEffect(() => {
const handleGeoIp = (geoData: GeoData) => {
const { country_code } = geoData;
const hasVisited = cookies.get('visited');
if (!hasVisited) {
switch (country_code) {
case 'VN':
changeRegionId('reg_01H1KEF7D8MNAGQNB4JG41FWPS');
cookies.set('currency', 'VND', { path: '/', maxAge: 7 * 24 * 60 * 60 });
break;
default:
changeRegionId('reg_01H1KEF7D8MNAGQNB4JG41FWPS');
cookies.set('currency', 'USD', { path: '/', maxAge: 7 * 24 * 60 * 60 });
break;
}
cookies.set('visited', 'true', { path: '/', maxAge: 7 * 24 * 60 * 60 });
setVisited(true);
}
};
const script = document.createElement('script');
script.src = 'https://get.geojs.io/v1/ip/geo.js';
script.async = true;
document.body.appendChild(script);
(window as any).geoip = (geoData: GeoData) => {
handleGeoIp(geoData);
};
return () => {
document.body.removeChild(script);
delete (window as any).geoip;
};
}, [cart?.id, updateCart, cookies]);
return null;
};
export default CurrencySwitcher;`