@@ -3,6 +3,7 @@ import type { Gift } from "../../types/Gift";
33import Icon from "../utils/Icon" ;
44import "./giftcard.css" ;
55import "./Wishlist.css" ;
6+ import { useEffect , useState } from "react" ;
67
78type GiftCardProps = {
89 gift : Gift ;
@@ -14,6 +15,16 @@ type GiftCardProps = {
1415
1516export default function GiftCard ( { gift, className, onEdit, onDelete } : GiftCardProps ) {
1617 const { name, description, imageUrl, url } = gift ;
18+ const [ errorImageUrl , setErrorImageUrl ] = useState ( false ) ;
19+
20+ useEffect ( ( ) => {
21+ if ( imageUrl ) {
22+ const img = new Image ( ) ;
23+ img . src = imageUrl ;
24+ img . onerror = ( ) => setErrorImageUrl ( true ) ;
25+ img . onload = ( ) => setErrorImageUrl ( false ) ;
26+ }
27+ } , [ imageUrl ] ) ;
1728
1829 return (
1930 < div
@@ -26,7 +37,11 @@ export default function GiftCard({ gift, className, onEdit, onDelete }: GiftCard
2637 { url ? (
2738 < a href = { url } target = "_blank" rel = "noopener noreferrer" className = "block" >
2839 { imageUrl ? (
29- < img src = { imageUrl } alt = { name } className = "w-full h-40 object-cover" />
40+ < img
41+ src = { errorImageUrl ? "/images/gift-not-find.png" : imageUrl }
42+ alt = { name }
43+ className = { `w-full h-40 ${ errorImageUrl ? "object-contain" : "object-cover" } ` }
44+ />
3045 ) : (
3146 < div className = "flex items-center justify-center w-full h-40 bg-[#FDFBF6]" >
3247 < Icon icon = "gift" className = "text-9xl text-[#EA4B09] opacity-70" />
@@ -40,7 +55,11 @@ export default function GiftCard({ gift, className, onEdit, onDelete }: GiftCard
4055 ) : (
4156 < div >
4257 { imageUrl ? (
43- < img src = { imageUrl } alt = { name } className = "w-full h-40 object-cover" />
58+ < img
59+ src = { errorImageUrl ? "/images/gift-not-find.png" : imageUrl }
60+ alt = { name }
61+ className = { `w-full h-40 ${ errorImageUrl ? "object-contain" : "object-cover" } ` }
62+ />
4463 ) : (
4564 < div className = "flex items-center justify-center w-full h-40 bg-[#FDFBF6]" >
4665 < Icon icon = "gift" className = "text-9xl text-[#EA4B09] opacity-70" />
0 commit comments