Skip to content

Commit

Permalink
fix: cart props
Browse files Browse the repository at this point in the history
  • Loading branch information
Kosei805 committed Nov 13, 2024
1 parent ffcf996 commit 17bcfc3
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 9 deletions.
3 changes: 2 additions & 1 deletion frontend/app/components/books/BookCardCartButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Button } from '@mantine/core';
import { useAtom } from 'jotai';
import { BiSolidCartAdd } from 'react-icons/bi';
import { cartAtom } from '~/stores/cartAtom';
import { addBookToCart } from '~/utils/cart';

interface BookCardCartButtonProps {
id: number;
Expand All @@ -11,7 +12,7 @@ interface BookCardCartButtonProps {
const BookCardCartButton = ({ id, stock }: BookCardCartButtonProps) => {
const [cart, setCart] = useAtom(cartAtom);
const addCart = () => {
setCart([...cart, { id, stock }]);
setCart(addBookToCart(cart, id, stock));
};
return (
<Button
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/components/books/BookCardHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const BookCardHeader = ({
setSelectedBook(selectedBook.filter((element) => element.id !== id));
} else {
// 選択されていなかった場合は選択する
setSelectedBook([...selectedBook, { id, stock, title, thumbnail }]);
setSelectedBook([...selectedBook, { id, stock, thumbnail }]);
}
};

Expand Down
1 change: 0 additions & 1 deletion frontend/app/components/cart/CartCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const CartCard = ({ book }: CartCardProps) => {
<CartCardHeader
id={book.id}
stock={book.stock}
title={book.title}
volume={book.volume}
thumbnail={book.thumbnail}
/>
Expand Down
5 changes: 1 addition & 4 deletions frontend/app/components/cart/CartCardHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import CartCardNumberInput from './CartCardNumberInput';
interface CartCardHeaderProps {
id: number;
stock: number;
title: string;
volume: number;
thumbnail?: string;
}

const CartCardHeader = ({
id,
stock,
title,
volume,
thumbnail,
}: CartCardHeaderProps) => {
Expand All @@ -32,7 +30,6 @@ const CartCardHeader = ({
return {
id: element.id,
stock: element.stock,
title: element.title,
thumbnail: element.thumbnail,
volume: value,
};
Expand All @@ -56,7 +53,7 @@ const CartCardHeader = ({
// 選択されていなかった場合は選択する
setSelectedCartBook([
...selectedCartBook,
{ id, stock, title, thumbnail, volume: 1 },
{ id, stock, thumbnail, volume: 1 },
]);
}
};
Expand Down
1 change: 0 additions & 1 deletion frontend/app/stores/bookAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { atom } from 'jotai';
export interface SelectedBookProps {
id: number;
stock: number;
title: string;
thumbnail?: string;
}

Expand Down
20 changes: 19 additions & 1 deletion frontend/app/utils/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const addBooksToCart = (
cart.push({
id: book.id,
stock: book.stock,
title: book.title,
thumbnail: book.thumbnail,
volume: 1,
});
Expand All @@ -22,6 +21,25 @@ export const addBooksToCart = (
return cart;
};

export const addBookToCart = (
cart: CartProps[],
bookId: number,
stock: number,
) => {
const index = cart.findIndex((cartBook) => cartBook.id === bookId);
if (index !== -1) {
cart[index].volume += 1;
} else {
cart.push({
id: bookId,
stock,
thumbnail: '',
volume: 1,
});
}
return cart;
};

export const removeBooksFromCart = (cart: CartProps[], books: CartProps[]) => {
const idList = books.map((book) => book.id);
return cart.filter((cartBook) => !idList.includes(cartBook.id));
Expand Down

0 comments on commit 17bcfc3

Please sign in to comment.