diff --git a/frontend/app/components/cart/CartCard.tsx b/frontend/app/components/cart/CartCard.tsx new file mode 100644 index 00000000..6c9a7503 --- /dev/null +++ b/frontend/app/components/cart/CartCard.tsx @@ -0,0 +1,29 @@ +import { Card } from '@mantine/core'; +import { CartProps } from '~/stores/cartAtom'; +import BookCardThumbnail from '../books/BookCardThumbnail'; +import CartCardHeader from './CartCardHeader'; + +interface CartCardProps { + book: CartProps; +} + +const CartCard = ({ book }: CartCardProps) => { + return ( + + + + + + + + + ); +}; + +export default CartCard; diff --git a/frontend/app/components/cart/CartCardHeader.tsx b/frontend/app/components/cart/CartCardHeader.tsx new file mode 100644 index 00000000..9615eb46 --- /dev/null +++ b/frontend/app/components/cart/CartCardHeader.tsx @@ -0,0 +1,81 @@ +import { Checkbox, Group } from '@mantine/core'; +import { useAtom } from 'jotai'; +import type { SelectedBookProps } from '~/stores/bookAtom'; +import { cartAtom, selectedCartBooksAtom } from '~/stores/cartAtom'; +import CartCardNumberInput from './CartCardNumberInput'; + +interface CartCardHeaderProps { + id: number; + stock: number; + title: string; + volume: number; + thumbnail?: string; +} + +const CartCardHeader = ({ + id, + stock, + title, + volume, + thumbnail, +}: CartCardHeaderProps) => { + const [cart, setCart] = useAtom(cartAtom); + const [selectedCartBook, setSelectedCartBook] = useAtom( + selectedCartBooksAtom, + ); + + // 該当する本のvolumeを変更する + const handleChangeVolume = (id: number, value: number) => { + setCart( + cart.map((element) => { + if (element.id === id) { + return { + id: element.id, + stock: element.stock, + title: element.title, + thumbnail: element.thumbnail, + volume: value, + }; + } + return element; + }), + ); + }; + + // 選択されている本のIDと表示する本のIDを比較する関数 + const selectedCheck = (element: SelectedBookProps) => element.id === id; + + const selectedBookAdd = () => { + // チェックボックスの状態が変化した時に + if (selectedCartBook.some(selectedCheck)) { + // すでに選択されていた場合は選択を外す + setSelectedCartBook( + selectedCartBook.filter((element) => element.id !== id), + ); + } else { + // 選択されていなかった場合は選択する + setSelectedCartBook([ + ...selectedCartBook, + { id, stock, title, thumbnail, volume: 1 }, + ]); + } + }; + + return ( + + + + + ); +}; + +export default CartCardHeader; diff --git a/frontend/app/components/cart/CartCardNumberInput.tsx b/frontend/app/components/cart/CartCardNumberInput.tsx new file mode 100644 index 00000000..4434ca9e --- /dev/null +++ b/frontend/app/components/cart/CartCardNumberInput.tsx @@ -0,0 +1,40 @@ +import { Group, Select, Text } from '@mantine/core'; +import { range } from '@mantine/hooks'; + +interface CartCardHeaderBadgeProps { + id: number; + stock: number; + volume: number; + handleChangeVolume: (id: number, value: number) => void; +} + +const CartCardNumberInput = ({ + id, + stock, + volume, + handleChangeVolume, +}: CartCardHeaderBadgeProps) => { + const stockList = range(0, stock); + const dataList = stock >= volume ? stockList : [...stockList, volume]; + const strList = dataList.map((data) => data.toString()); + + const handleOnChange = (volume: string | null) => { + if (!volume) return; + const numVolume = Number(volume); + handleChangeVolume(id, numVolume); + }; + return ( + + 冊数 +