To enhance your Wishlist feature with smooth animations, improved performance, and stability, we'll integrate Framer Motion for animations and apply some optimizations to minimize unnecessary re-renders and refetches. Here's a step-by-step guide to achieving this:
- Install Framer Motion
- Update
WishlistItem.jsx with Animations
- Update
Wishlist.jsx with AnimatePresence
- Optimize Performance by Memoizing Components
- Ensure Stable Handlers with
useCallback
Let's go through each step in detail.
1. Install Framer Motion
First, install Framer Motion if you haven't already:
npm install framer-motion
# or
yarn add framer-motion
2. Update WishlistItem.jsx with Animations
We'll wrap the WishlistItem with Framer Motion's motion.div and define exit animations for smooth removal.
// src/components/WishlistItem.jsx
'use client';
import React from "react";
import Image from "next/image";
import { useRouter } from "next/navigation";
import { X } from "lucide-react";
import ShoppingBag from "@/components/ShoppingBag";
import { motion } from "framer-motion"; // Import motion
const getCheapestPrice = (product) => {
const prices = [
product.price && product.price !== '0.00' ? parseFloat(product.price) : Infinity,
...(product.sizes?.map((size) => parseFloat(size.price)) || []),
...(product.colors?.flatMap((color) => [
parseFloat(color.price),
...(color.sizes?.map((size) => parseFloat(size.price)) || []),
]) || []),
].filter((price) => !isNaN(price) && isFinite(price));
return prices.length > 0 ? Math.min(...prices) : null;
};
// Memoize the component to prevent unnecessary re-renders
const WishlistItem = React.memo(({ product, onRemove }) => {
const router = useRouter();
const handleShoppingBagClick = () => {
router.push(`/products/${product.id}`);
};
if (!product) {
// Optionally, render nothing or a fallback UI
return null;
}
// Ensure product.images is an array and check its length
const image = (Array.isArray(product.images) && product.images.length > 0)
? `https://storage.naayiq.com/resources/${product.images[0]?.url}`
: "https://storage.naayiq.com/resources/noimage.webp";
return (
<motion.div
layout
initial={{ opacity: 0, scale: 0.95 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.95, transition: { duration: 0.3 } }}
transition={{ type: "spring", stiffness: 300, damping: 30 }}
className="bg-white min-h-[201px] rounded-lg flex items-stretch space-x-4 mb-4"
>
<div className="relative min-h-[154px] aspect-[154/169]">
<Image
src={image}
alt={product.name}
fill={true}
unoptimized={true}
className="object-cover rounded-md"
/>
</div>
<div className="flex-grow flex flex-col gap-2 items-center justify-center">
<h3 className="font-serif font-medium text-sm ssm2:text-base text-center">{product.name}</h3>
<h3 className="font-serif font-medium text-sm ssm2:text-base text-center">
{getCheapestPrice(product) !== null ? `${getCheapestPrice(product).toLocaleString()} IQD` : "N/A"}
</h3>
</div>
<div className="flex flex-col min-h-[201px] justify-between items-center space-y-2">
<button onClick={() => onRemove(product.id)} className="p-1">
<X size={40} strokeWidth={1} />
</button>
<button onClick={handleShoppingBagClick} className="bg-[#3B5345] rounded-full p-2">
<ShoppingBag width={40} height={40} />
</button>
</div>
</motion.div>
);
});
export default WishlistItem;
Explanation of Changes:
3. Update Wishlist.jsx with AnimatePresence
We'll use Framer Motion's AnimatePresence to handle the presence of components and animate their entry and exit.
// src/pages/Wishlist.jsx
'use client';
import { useState, useEffect, useCallback } from "react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { CircleArrowLeft } from "lucide-react";
import WishlistItem from "@/components/WishlistItem";
import Loading from "@/components/Loading";
import { AnimatePresence, motion } from "framer-motion"; // Import AnimatePresence and motion
export const getToken = () => {
if (typeof window === 'undefined') return null;
return localStorage.getItem('token'); // Adjust based on your token storage strategy
};
export const fetchWithAuth = async (url, options = {}) => {
const token = getToken();
if (!token) {
throw new Error('No authentication token found');
}
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
...options.headers,
};
const response = await fetch(url, {
...options,
headers,
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.message || 'API request failed');
}
return response.json();
};
export default function Wishlist() {
const router = useRouter();
const [wishlistProductIds, setWishlistProductIds] = useState([]);
const [wishlistProducts, setWishlistProducts] = useState([]);
const [isLoading, setIsLoading] = useState(true); // Overall loading state
const [error, setError] = useState(null);
// Fetch Wishlist IDs and Products
const fetchWishlist = useCallback(async () => {
try {
const data = await fetchWithAuth(`${process.env.NEXT_PUBLIC_API}/wishlist`);
if (!Array.isArray(data.wishlist)) {
throw new Error('Invalid wishlist data format');
}
setWishlistProductIds(data.wishlist); // Assuming data.wishlist is an array of { id: ... }
// Fetch all products in parallel
const productPromises = data.wishlist.map(async (item) => {
try {
const productData = await fetchWithAuth(`${process.env.NEXT_PUBLIC_API}/products/${item.id}`);
return productData.product;
} catch (err) {
console.error(`Error fetching product with ID ${item.id}:`, err);
setError('Some wishlist items failed to load.');
return null;
}
});
const products = await Promise.all(productPromises);
const validProducts = products.filter(p => p !== null);
setWishlistProducts(validProducts);
} catch (err) {
console.error('Error fetching wishlist:', err);
setError(err.message);
} finally {
setIsLoading(false);
}
}, []);
// Initial Data Fetch
useEffect(() => {
fetchWishlist();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
// Handle Remove Item
const handleRemoveItem = useCallback(async (productId) => {
// Optimistically remove the item from UI
setWishlistProductIds(prevIds => prevIds.filter(id => id.id !== productId));
setWishlistProducts(prevProducts => prevProducts.filter(p => p.id !== productId));
try {
await fetchWithAuth(`${process.env.NEXT_PUBLIC_API}/wishlist/`, {
body: JSON.stringify({ product_id: productId }),
method: 'DELETE',
});
// Successfully removed on server
} catch (err) {
console.error('Error removing item:', err);
setError('Failed to remove item from wishlist.');
// Revert UI changes
fetchWishlist();
}
}, [fetchWishlist]);
if (isLoading) {
return <Loading />; // Display the Loading component while fetching
}
return (
<>
<header className="flex items-center mb-6 px-4">
<CircleArrowLeft
size={52}
strokeWidth={0.7}
onClick={() => router.back()}
className="p-2 relative z-20 cursor-pointer"
/>
<h1 className="text-3xl z-10 text-[#181717] left-0 right-0 absolute font-sans text-center font-medium">
Wishlist
</h1>
</header>
<main className="flex-grow flex flex-col items-center px-4">
{error && (
<div className="text-red-500 text-center mb-4">{error}</div>
)}
{wishlistProductIds.length === 0 ? (
<EmptyWishlist />
) : (
<div className="w-full max-w-md">
<AnimatePresence>
{wishlistProducts.map((product) => (
<motion.div
key={product.id}
initial={{ opacity: 0, height: 0 }}
animate={{ opacity: 1, height: 'auto' }}
exit={{ opacity: 0, height: 0, marginBottom: 0 }}
transition={{ duration: 0.3 }}
>
<WishlistItem
product={product}
onRemove={handleRemoveItem}
/>
</motion.div>
))}
</AnimatePresence>
</div>
)}
</main>
</>
);
}
const EmptyWishlist = () => (
<>
<div className="relative w-full h-[33vh]">
<img
src="https://storage.naayiq.com/resources/empty_wishlist.gif"
alt="Empty wishlist"
className="object-contain w-full h-full"
/>
</div>
<section className="text-center">
<p className="font-serif text-3xl mb-2 font-semibold">Wishlist is empty!</p>
<p className="font-serif text-lg leading-none">Tap the Heart Button to start</p>
<p className="font-serif text-lg">saving your favorite items.</p>
<Link href="/" className="min-h-[56px] mt-4 inline-flex items-center justify-center px-3 sm:px-6 bg-[#3B5345] text-white text-xl rounded-lg">
Start Browsing
</Link>
</section>
</>
);
const WishlistSkeleton = () => {
return (
<div className="bg-white rounded-lg p-4 flex items-stretch justify-between mb-4 shadow-sm animate-pulse">
{/* Skeleton for Product Details */}
<div className="flex-grow flex flex-col justify-center space-y-2">
<div className="h-6 bg-gray-200 rounded w-3/4"></div>
<div className="h-6 bg-gray-200 rounded w-1/2"></div>
</div>
{/* Skeleton for Action Buttons */}
<div className="flex flex-col justify-center items-center space-y-2">
<div className="h-6 w-6 bg-gray-200 rounded-full"></div>
<div className="h-10 w-10 bg-gray-200 rounded-full"></div>
</div>
</div>
);
};
Explanation of Changes:
4. Optimize Performance by Memoizing Components
We've already memoized the WishlistItem component using React.memo, which ensures it only re-renders when its props change. This is crucial for performance, especially when dealing with lists.
If you have other child components or complex logic, consider applying similar memoization strategies.
5. Ensure Stable Handlers with useCallback
Using useCallback for handler functions like handleRemoveItem ensures that their references don't change between renders unless their dependencies do. This stability is essential for memoized child components to prevent unnecessary re-renders.
We've already applied useCallback to handleRemoveItem in the Wishlist.jsx:
const handleRemoveItem = useCallback(async (productId) => {
// Optimistically remove the item from UI
setWishlistProductIds(prevIds => prevIds.filter(id => id.id !== productId));
setWishlistProducts(prevProducts => prevProducts.filter(p => p.id !== productId));
try {
await fetchWithAuth(`${process.env.NEXT_PUBLIC_API}/wishlist/`, {
body: JSON.stringify({ product_id: productId }),
method: 'DELETE',
});
// Successfully removed on server
} catch (err) {
console.error('Error removing item:', err);
setError('Failed to remove item from wishlist.');
// Revert UI changes
fetchWishlist();
}
}, [fetchWishlist]);
This ensures that handleRemoveItem maintains a stable reference unless fetchWishlist changes.
Final Enhanced Components
Below are the fully updated components with all the enhancements applied.
WishlistItem.jsx
// src/components/WishlistItem.jsx
'use client';
import React from "react";
import Image from "next/image";
import { useRouter } from "next/navigation";
import { X } from "lucide-react";
import ShoppingBag from "@/components/ShoppingBag";
import { motion } from "framer-motion"; // Import motion
const getCheapestPrice = (product) => {
const prices = [
product.price && product.price !== '0.00' ? parseFloat(product.price) : Infinity,
...(product.sizes?.map((size) => parseFloat(size.price)) || []),
...(product.colors?.flatMap((color) => [
parseFloat(color.price),
...(color.sizes?.map((size) => parseFloat(size.price)) || []),
]) || []),
].filter((price) => !isNaN(price) && isFinite(price));
return prices.length > 0 ? Math.min(...prices) : null;
};
// Memoize the component to prevent unnecessary re-renders
const WishlistItem = React.memo(({ product, onRemove }) => {
const router = useRouter();
const handleShoppingBagClick = () => {
router.push(`/products/${product.id}`);
};
if (!product) {
// Optionally, render nothing or a fallback UI
return null;
}
// Ensure product.images is an array and check its length
const image = (Array.isArray(product.images) && product.images.length > 0)
? `https://storage.naayiq.com/resources/${product.images[0]?.url}`
: "https://storage.naayiq.com/resources/noimage.webp";
return (
<motion.div
layout
initial={{ opacity: 0, scale: 0.95 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.95, transition: { duration: 0.3 } }}
transition={{ type: "spring", stiffness: 300, damping: 30 }}
className="bg-white min-h-[201px] rounded-lg flex items-stretch space-x-4 mb-4"
>
<div className="relative min-h-[154px] aspect-[154/169]">
<Image
src={image}
alt={product.name}
fill={true}
unoptimized={true}
className="object-cover rounded-md"
/>
</div>
<div className="flex-grow flex flex-col gap-2 items-center justify-center">
<h3 className="font-serif font-medium text-sm ssm2:text-base text-center">{product.name}</h3>
<h3 className="font-serif font-medium text-sm ssm2:text-base text-center">
{getCheapestPrice(product) !== null ? `${getCheapestPrice(product).toLocaleString()} IQD` : "N/A"}
</h3>
</div>
<div className="flex flex-col min-h-[201px] justify-between items-center space-y-2">
<button onClick={() => onRemove(product.id)} className="p-1">
<X size={40} strokeWidth={1} />
</button>
<button onClick={handleShoppingBagClick} className="bg-[#3B5345] rounded-full p-2">
<ShoppingBag width={40} height={40} />
</button>
</div>
</motion.div>
);
});
export default WishlistItem;
Wishlist.jsx
// src/pages/Wishlist.jsx
'use client';
import { useState, useEffect, useCallback } from "react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { CircleArrowLeft } from "lucide-react";
import WishlistItem from "@/components/WishlistItem";
import Loading from "@/components/Loading";
import { AnimatePresence, motion } from "framer-motion"; // Import AnimatePresence and motion
export const getToken = () => {
if (typeof window === 'undefined') return null;
return localStorage.getItem('token'); // Adjust based on your token storage strategy
};
export const fetchWithAuth = async (url, options = {}) => {
const token = getToken();
if (!token) {
throw new Error('No authentication token found');
}
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
...options.headers,
};
const response = await fetch(url, {
...options,
headers,
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.message || 'API request failed');
}
return response.json();
};
export default function Wishlist() {
const router = useRouter();
const [wishlistProductIds, setWishlistProductIds] = useState([]);
const [wishlistProducts, setWishlistProducts] = useState([]);
const [isLoading, setIsLoading] = useState(true); // Overall loading state
const [error, setError] = useState(null);
// Fetch Wishlist IDs and Products
const fetchWishlist = useCallback(async () => {
try {
const data = await fetchWithAuth(`${process.env.NEXT_PUBLIC_API}/wishlist`);
if (!Array.isArray(data.wishlist)) {
throw new Error('Invalid wishlist data format');
}
setWishlistProductIds(data.wishlist); // Assuming data.wishlist is an array of { id: ... }
// Fetch all products in parallel
const productPromises = data.wishlist.map(async (item) => {
try {
const productData = await fetchWithAuth(`${process.env.NEXT_PUBLIC_API}/products/${item.id}`);
return productData.product;
} catch (err) {
console.error(`Error fetching product with ID ${item.id}:`, err);
setError('Some wishlist items failed to load.');
return null;
}
});
const products = await Promise.all(productPromises);
const validProducts = products.filter(p => p !== null);
setWishlistProducts(validProducts);
} catch (err) {
console.error('Error fetching wishlist:', err);
setError(err.message);
} finally {
setIsLoading(false);
}
}, []);
// Initial Data Fetch
useEffect(() => {
fetchWishlist();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
// Handle Remove Item
const handleRemoveItem = useCallback(async (productId) => {
// Optimistically remove the item from UI
setWishlistProductIds(prevIds => prevIds.filter(id => id.id !== productId));
setWishlistProducts(prevProducts => prevProducts.filter(p => p.id !== productId));
try {
await fetchWithAuth(`${process.env.NEXT_PUBLIC_API}/wishlist/`, {
body: JSON.stringify({ product_id: productId }),
method: 'DELETE',
});
// Successfully removed on server
} catch (err) {
console.error('Error removing item:', err);
setError('Failed to remove item from wishlist.');
// Revert UI changes
fetchWishlist();
}
}, [fetchWishlist]);
if (isLoading) {
return <Loading />; // Display the Loading component while fetching
}
return (
<>
<header className="flex items-center mb-6 px-4">
<CircleArrowLeft
size={52}
strokeWidth={0.7}
onClick={() => router.back()}
className="p-2 relative z-20 cursor-pointer"
/>
<h1 className="text-3xl z-10 text-[#181717] left-0 right-0 absolute font-sans text-center font-medium">
Wishlist
</h1>
</header>
<main className="flex-grow flex flex-col items-center px-4">
{error && (
<div className="text-red-500 text-center mb-4">{error}</div>
)}
{wishlistProductIds.length === 0 ? (
<EmptyWishlist />
) : (
<div className="w-full max-w-md">
<AnimatePresence>
{wishlistProducts.map((product) => (
<motion.div
key={product.id}
initial={{ opacity: 0, height: 0 }}
animate={{ opacity: 1, height: 'auto' }}
exit={{ opacity: 0, height: 0, marginBottom: 0 }}
transition={{ duration: 0.3 }}
>
<WishlistItem
product={product}
onRemove={handleRemoveItem}
/>
</motion.div>
))}
</AnimatePresence>
</div>
)}
</main>
</>
);
}
const EmptyWishlist = () => (
<>
<div className="relative w-full h-[33vh]">
<img
src="https://storage.naayiq.com/resources/empty_wishlist.gif"
alt="Empty wishlist"
className="object-contain w-full h-full"
/>
</div>
<section className="text-center">
<p className="font-serif text-3xl mb-2 font-semibold">Wishlist is empty!</p>
<p className="font-serif text-lg leading-none">Tap the Heart Button to start</p>
<p className="font-serif text-lg">saving your favorite items.</p>
<Link href="/" className="min-h-[56px] mt-4 inline-flex items-center justify-center px-3 sm:px-6 bg-[#3B5345] text-white text-xl rounded-lg">
Start Browsing
</Link>
</section>
</>
);
const WishlistSkeleton = () => {
return (
<div className="bg-white rounded-lg p-4 flex items-stretch justify-between mb-4 shadow-sm animate-pulse">
{/* Skeleton for Product Details */}
<div className="flex-grow flex flex-col justify-center space-y-2">
<div className="h-6 bg-gray-200 rounded w-3/4"></div>
<div className="h-6 bg-gray-200 rounded w-1/2"></div>
</div>
{/* Skeleton for Action Buttons */}
<div className="flex flex-col justify-center items-center space-y-2">
<div className="h-6 w-6 bg-gray-200 rounded-full"></div>
<div className="h-10 w-10 bg-gray-200 rounded-full"></div>
</div>
</div>
);
};
Additional Recommendations
-
Avoid Storing Both IDs and Product Objects:
Storing both wishlistProductIds and wishlistProducts can lead to data synchronization issues. Consider storing only wishlistProducts, as it contains the necessary data. Update the state accordingly in handleRemoveItem.
-
Error Handling Enhancements:
- Display more detailed error messages when possible.
- Provide retry mechanisms for failed fetches.
-
Loading States for Individual Items:
Implement individual loading states for actions like removing items to enhance user experience.
-
TypeScript Integration:
If possible, consider using TypeScript for better type safety and developer experience.
-
Testing:
- Write unit and integration tests to ensure your components work as expected.
- Use tools like React Testing Library and Jest.
-
Accessibility:
- Ensure buttons are accessible by adding
aria-labels.
- Make sure the application is navigable via keyboard.
Summary
By integrating Framer Motion and applying React performance optimizations, your Wishlist feature will now have smooth animations when adding or removing items, reduced unnecessary re-renders, and an overall improved user experience. Remember to tailor the animations (initial, animate, exit, and transition properties) to fit your design preferences for the best results.