diff --git a/src/components/BookCard/index.tsx b/src/components/BookCard/index.tsx index 6d2ebb7..5c2e881 100644 --- a/src/components/BookCard/index.tsx +++ b/src/components/BookCard/index.tsx @@ -1,14 +1,17 @@ import React from 'react' -import { useAppDispatch } from '../../app/hooks' +import { useAppDispatch, useAppSelector } from '../../app/hooks' import { deleteBook } from '../../features/book/bookSlice' -import formatDateString from '../../utils/formatDateString' +import { + formatDateString, + getFirstBookCategoryName, +} from '../../utils/formatDateString' import UpdateBookModal from '../UpdateBookModal' interface BookProps { bookId: string bookTitle: string bookAuthor: string - bookCategory: string[] + bookCategories: string[] bookUpdatedAt: string } @@ -16,10 +19,11 @@ const BookCard: React.FC = ({ bookId, bookTitle, bookAuthor, - bookCategory, + bookCategories, bookUpdatedAt, }) => { const dispatch = useAppDispatch() + const categories = useAppSelector((state) => state.categories.categoriesData) const handleDeleteBook = async () => { try { @@ -39,15 +43,19 @@ const BookCard: React.FC = ({
{bookTitle}

- Author: {bookAuthor} + Author: {bookAuthor}

- Category: {bookCategory} + + Category: + {getFirstBookCategoryName(bookCategories, categories)} +

- Last updated: {formatDateString(bookUpdatedAt)} + Last updated: + {formatDateString(bookUpdatedAt)}

diff --git a/src/components/BookList/index.tsx b/src/components/BookList/index.tsx index 056032c..71c3c32 100644 --- a/src/components/BookList/index.tsx +++ b/src/components/BookList/index.tsx @@ -35,7 +35,7 @@ const BookList: React.FC = () => { key={book.id} bookAuthor={book.author} bookTitle={book.title} - bookCategory={book.categories} + bookCategories={book.categories} bookUpdatedAt={book.updated_at} bookId={book.id} /> diff --git a/src/utils/formatDateString.ts b/src/utils/formatDateString.ts index 9e4243b..dbde98d 100644 --- a/src/utils/formatDateString.ts +++ b/src/utils/formatDateString.ts @@ -1,4 +1,6 @@ -const formatDateString = (dateString: string) => { +import { Category } from '../features/category/categorySlice' + +export const formatDateString = (dateString: string) => { const date = new Date(dateString) const options: Intl.DateTimeFormatOptions = { year: 'numeric', @@ -11,4 +13,12 @@ const formatDateString = (dateString: string) => { return date.toLocaleString('en-US', options) } -export default formatDateString +export const getFirstBookCategoryName = ( + bookCategories: string[], + availableCategories: Category[] +) => { + const firstCategory = availableCategories.filter( + (category: Category) => bookCategories[0] === category.id + )[0] + return firstCategory.name ? firstCategory.name : 'Uncategorized' +}