Skip to content

Commit 37bb93c

Browse files
committed
fix: fix small image undefined bug
1 parent 278f5ac commit 37bb93c

File tree

7 files changed

+17
-6
lines changed

7 files changed

+17
-6
lines changed

app/(routes)/categories/[category]/layouts/BooksContainer.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Pagination from "app/components/Pagination"
66
import CardSkeletons from "@/loading-ui/CardSkeletons"
77
import scrollToTop from "@/utils/scrollToTop"
88
import { getBooksByCategory } from "@/lib/api"
9+
import { getOptimizedImage } from "@/utils/utilFuncs"
910
import { Book } from "@/types/Book"
1011

1112
type Props = {
@@ -39,6 +40,7 @@ export default function BooksContainer({
3940
<div className="cards-container">
4041
{data.data.map(({ id, attributes }) => {
4142
const { slug, price, title, image } = attributes
43+
4244
return (
4345
<ItemCard
4446
key={id}
@@ -47,7 +49,7 @@ export default function BooksContainer({
4749
price={price}
4850
slug={slug}
4951
title={title}
50-
image={image.data[0].attributes.formats.small.url}
52+
image={getOptimizedImage(image)}
5153
/>
5254
)
5355
})}

app/(routes)/wishlist/components/WishlistTable.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useQuery } from "@tanstack/react-query"
66
import CancelIcon from "@/icons/CancelIcon"
77
import LoadingIcon from "@/icons/LoadingIcon"
88
import EmptyBoxIcon from "@/icons/EmptyBoxIcon"
9+
import { getOptimizedImage } from "@/utils/utilFuncs"
910
import { getBooksByIds } from "@/lib/api"
1011
import { useMounted } from "@/hooks"
1112
import {
@@ -31,7 +32,7 @@ const fetchBooks = async (wishlistIds: number[], wishlist: WishlistItem[]) => {
3132
return {
3233
id: item.id,
3334
slug: slug,
34-
image: image.data[0].attributes.formats.small.url,
35+
image: getOptimizedImage(image),
3536
title: title,
3637
price: price,
3738
inStock: in_stock,

app/components/BookRow.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { useQuery } from "@tanstack/react-query"
44
import ItemCard from "@/components/ItemCard"
55
import CardSkeletons from "@/loading-ui/CardSkeletons"
6+
import { getOptimizedImage } from "@/utils/utilFuncs"
67
import { getBooksBySlug } from "@/lib/api"
78

89
type Props = {
@@ -37,7 +38,7 @@ export default function BookRow({ slug }: Props) {
3738
price={price}
3839
slug={slug}
3940
title={title}
40-
image={image.data[0].attributes.formats.small.url}
41+
image={getOptimizedImage(image)}
4142
/>
4243
)
4344
})}

app/components/SearchDialog.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Image from "next/image"
66
import * as Dialog from "@radix-ui/react-dialog"
77
import { useQuery } from "@tanstack/react-query"
88
import SearchIcon from "@/icons/SearchIcon"
9+
import { getOptimizedImage } from "@/utils/utilFuncs"
910
import { getBooksByTitle } from "@/lib/api"
1011
import { useDebounce } from "@/hooks"
1112
import { Book } from "@/types/Book"
@@ -110,7 +111,7 @@ const SearchDialog = () => {
110111
>
111112
<div className="relative h-32 w-28 overflow-hidden">
112113
<Image
113-
src={image.data[0].attributes.formats.small.url}
114+
src={getOptimizedImage(image)}
114115
alt={title}
115116
fill
116117
sizes="

lib/hooks/useCart.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useQuery } from "@tanstack/react-query"
2+
import { getOptimizedImage } from "@/utils/utilFuncs"
23
import { getBooksByIds } from "@/lib/api"
34
import { useCartStore } from "@/store"
45

@@ -46,7 +47,7 @@ export const useCart = () => {
4647
title,
4748
price,
4849
slug,
49-
image: image.data[0].attributes.formats.small.url,
50+
image: getOptimizedImage(image),
5051
quantity: qtyMap.get(item.id) || 1,
5152
timestamp: timestampMap.get(item.id) || 1,
5253
}

lib/types/Book.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export interface ImageAttributes {
8787
}
8888

8989
export interface Formats {
90-
small: Small
90+
small?: Small
9191
thumbnail: Small
9292
}
9393

lib/utils/utilFuncs.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Image } from "@/types/Book"
2+
13
export const defaultStroke = (className: string): string =>
24
new RegExp("stroke-*", "g").test(className) ? "" : "stroke-2 stroke-skin-dark"
35

@@ -10,3 +12,6 @@ export const generateUniqueArray = (num: number) => {
1012

1113
return Array.from(numbers)
1214
}
15+
16+
export const getOptimizedImage = (image: Image) =>
17+
image.data[0].attributes.formats.small?.url || image.data[0].attributes.url

0 commit comments

Comments
 (0)