refactor: set country based on user data

This commit is contained in:
2025-05-13 06:55:03 +05:30
parent 4851f34357
commit ffc880f6f8
6 changed files with 225 additions and 65 deletions

View File

@@ -16,10 +16,16 @@ const Navbar = () => {
const [isSidebarOpen, setIsSidebarOpen] = useState(false);
const [isScrolled, setIsScrolled] = useState(false);
const { category } = useContext(ProductContext);
const { token } = useContext(MainContext);
const { token, user } = useContext(MainContext);
const [cartItemCount, setCartItemCount] = useState(0);
const { selectedCurrency, setSelectedCurrency, SUPPORTED_CURRENCIES, error } =
useCurrency();
const {
selectedCurrency,
setSelectedCurrency,
SUPPORTED_CURRENCIES,
error,
setUserCountry,
userCountry
} = useCurrency();
const toggleSidebar = () => setIsSidebarOpen(!isSidebarOpen);
@@ -41,12 +47,17 @@ const Navbar = () => {
};
}, []);
// Set user's country from profile if available
useEffect(() => {
if (user && user.country && !userCountry) {
setUserCountry(user.country);
}
}, [user, setUserCountry, userCountry]);
const getCart = async () => {
try {
const response = await authAxios.get("/orders/cart/");
const cartData = response.data;
console.log(cartData);
console.log(cartData.length);
// Calculate total items in cart
if (cartData && cartData.length > 0 && cartData[0].items) {
@@ -249,7 +260,7 @@ const Navbar = () => {
</div>
)}
<div className="h-[120px]"></div>
<div className="h-[130px]"></div>
</>
);
};