Skip to content

Commit

Permalink
fix: fix small image undefined bug
Browse files Browse the repository at this point in the history
  • Loading branch information
satnaing committed Mar 22, 2023
1 parent 278f5ac commit 37bb93c
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Pagination from "app/components/Pagination"
import CardSkeletons from "@/loading-ui/CardSkeletons"
import scrollToTop from "@/utils/scrollToTop"
import { getBooksByCategory } from "@/lib/api"
import { getOptimizedImage } from "@/utils/utilFuncs"
import { Book } from "@/types/Book"

type Props = {
Expand Down Expand Up @@ -39,6 +40,7 @@ export default function BooksContainer({
<div className="cards-container">
{data.data.map(({ id, attributes }) => {
const { slug, price, title, image } = attributes

return (
<ItemCard
key={id}
Expand All @@ -47,7 +49,7 @@ export default function BooksContainer({
price={price}
slug={slug}
title={title}
image={image.data[0].attributes.formats.small.url}
image={getOptimizedImage(image)}
/>
)
})}
Expand Down
3 changes: 2 additions & 1 deletion app/(routes)/wishlist/components/WishlistTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useQuery } from "@tanstack/react-query"
import CancelIcon from "@/icons/CancelIcon"
import LoadingIcon from "@/icons/LoadingIcon"
import EmptyBoxIcon from "@/icons/EmptyBoxIcon"
import { getOptimizedImage } from "@/utils/utilFuncs"
import { getBooksByIds } from "@/lib/api"
import { useMounted } from "@/hooks"
import {
Expand All @@ -31,7 +32,7 @@ const fetchBooks = async (wishlistIds: number[], wishlist: WishlistItem[]) => {
return {
id: item.id,
slug: slug,
image: image.data[0].attributes.formats.small.url,
image: getOptimizedImage(image),
title: title,
price: price,
inStock: in_stock,
Expand Down
3 changes: 2 additions & 1 deletion app/components/BookRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { useQuery } from "@tanstack/react-query"
import ItemCard from "@/components/ItemCard"
import CardSkeletons from "@/loading-ui/CardSkeletons"
import { getOptimizedImage } from "@/utils/utilFuncs"
import { getBooksBySlug } from "@/lib/api"

type Props = {
Expand Down Expand Up @@ -37,7 +38,7 @@ export default function BookRow({ slug }: Props) {
price={price}
slug={slug}
title={title}
image={image.data[0].attributes.formats.small.url}
image={getOptimizedImage(image)}
/>
)
})}
Expand Down
3 changes: 2 additions & 1 deletion app/components/SearchDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Image from "next/image"
import * as Dialog from "@radix-ui/react-dialog"
import { useQuery } from "@tanstack/react-query"
import SearchIcon from "@/icons/SearchIcon"
import { getOptimizedImage } from "@/utils/utilFuncs"
import { getBooksByTitle } from "@/lib/api"
import { useDebounce } from "@/hooks"
import { Book } from "@/types/Book"
Expand Down Expand Up @@ -110,7 +111,7 @@ const SearchDialog = () => {
>
<div className="relative h-32 w-28 overflow-hidden">
<Image
src={image.data[0].attributes.formats.small.url}
src={getOptimizedImage(image)}
alt={title}
fill
sizes="
Expand Down
3 changes: 2 additions & 1 deletion lib/hooks/useCart.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useQuery } from "@tanstack/react-query"
import { getOptimizedImage } from "@/utils/utilFuncs"
import { getBooksByIds } from "@/lib/api"
import { useCartStore } from "@/store"

Expand Down Expand Up @@ -46,7 +47,7 @@ export const useCart = () => {
title,
price,
slug,
image: image.data[0].attributes.formats.small.url,
image: getOptimizedImage(image),
quantity: qtyMap.get(item.id) || 1,
timestamp: timestampMap.get(item.id) || 1,
}
Expand Down
2 changes: 1 addition & 1 deletion lib/types/Book.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export interface ImageAttributes {
}

export interface Formats {
small: Small
small?: Small
thumbnail: Small
}

Expand Down
5 changes: 5 additions & 0 deletions lib/utils/utilFuncs.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Image } from "@/types/Book"

export const defaultStroke = (className: string): string =>
new RegExp("stroke-*", "g").test(className) ? "" : "stroke-2 stroke-skin-dark"

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

return Array.from(numbers)
}

export const getOptimizedImage = (image: Image) =>
image.data[0].attributes.formats.small?.url || image.data[0].attributes.url

1 comment on commit 37bb93c

@vercel
Copy link

@vercel vercel bot commented on 37bb93c Mar 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.