diff --git a/components/cart/CartItemRow.tsx b/components/cart/CartItemRow.tsx new file mode 100644 index 0000000..f570e60 --- /dev/null +++ b/components/cart/CartItemRow.tsx @@ -0,0 +1,34 @@ +import React from "react"; +import Image from "next/image"; +import { LineItem } from "shopify-buy"; + +type Props = { + item: any; +}; + +const CartItemRow: React.FC = ({ item }) => { + console.log("CartItemRow rendered"); + console.log(JSON.stringify(item)); + const { handle, quantity, title } = item; + const price = Number(item.variant.price); + const subtotal = price * quantity; + const imgSrc = item.variant.image.src; + + return ( + + +
+ + + +
+ {title} + + ¥{price.toLocaleString("ja-JP")} + {quantity} + ¥{subtotal.toLocaleString("ja-JP")} + + ); +}; + +export default CartItemRow; diff --git a/components/cart/CartItemTable.tsx b/components/cart/CartItemTable.tsx new file mode 100644 index 0000000..fce5f77 --- /dev/null +++ b/components/cart/CartItemTable.tsx @@ -0,0 +1,30 @@ +import React, { useContext } from "react"; +import { CheckoutContext } from "pages/cart"; +import CartItemRow from "components/cart/CartItemRow"; + +const CartItemTable = () => { + console.log("CartItemTable rendered"); + + const checkout = useContext(CheckoutContext); + console.log(checkout?.lineItems?.length); + + return ( + + + + + + + + + + + {checkout?.lineItems?.map((item, idx) => ( + + ))} + +
商品名価格数量合計
+ ); +}; + +export default CartItemTable; diff --git a/components/collections/CollectionTitle.tsx b/components/collections/CollectionTitle.tsx deleted file mode 100644 index ea40ab5..0000000 --- a/components/collections/CollectionTitle.tsx +++ /dev/null @@ -1,11 +0,0 @@ -import React from 'react' - -type Props = { - title: string -} - -const CollectionTitle = ({ title }) => ( -

{ title }

-); - -export default CollectionTitle \ No newline at end of file diff --git a/components/product/CartDrawer.tsx b/components/product/CartDrawer.tsx index 5c01038..2525914 100644 --- a/components/product/CartDrawer.tsx +++ b/components/product/CartDrawer.tsx @@ -22,12 +22,11 @@ const CartDrawer: React.FC = ({ isOpen, setIsOpen, checkout }) => { const { product, imageId, variant } = useContext(ProductContext); const selectedImage = product.images.find((image) => image.id === imageId); - const { lineItems } = checkout; const reducer = (accumulator, currentValue) => { accumulator += currentValue.quantity; return accumulator; } - const totalQuqntity = lineItems?.reduce(reducer, 0); + const totalQuqntity = checkout?.lineItems?.reduce(reducer, 0); return ( diff --git a/lib/useCheckout.ts b/lib/useCheckout.ts index 5d4d436..92072b4 100644 --- a/lib/useCheckout.ts +++ b/lib/useCheckout.ts @@ -1,4 +1,4 @@ -import { useState, useEffect } from "react"; +import React, { useState, useEffect } from "react"; import { Cart } from "shopify-buy"; import client from "lib/client"; @@ -12,22 +12,23 @@ import client from "lib/client"; const useCheckout = () => { console.log('useCheckout') const [loading, setLoading] = useState(false); - const [checkout, setCheckout] = useState({}); + const [checkout, setCheckout] = useState(null); const [checkoutId, setCheckoutId] = useState(""); const initializeCart = () => { - console.log('initializeCart') - const id: string | null = localStorage.getItem("checkoutId") || null; + if (id) { client.checkout.fetch(id).then((checkout) => { setCheckoutId(checkout.id as string); + setCheckout(checkout); }); } else { client.checkout.create().then((checkout) => { localStorage.setItem("checkoutId", checkout.id as string); setCheckoutId(checkout.id as string); + setCheckout(checkout); }); } }; @@ -38,16 +39,13 @@ const useCheckout = () => { }, []); const addVariant = async (variantId: string, quantity: number) => { - console.log('addVariant関数の中') setLoading(true); const lineItemsToAdd = [{ variantId: variantId, quantity: quantity }]; const checkout = await client.checkout.addLineItems( checkoutId, lineItemsToAdd ); - console.log("addVariant関数の中でのaddLineItemsのあと"); setCheckout(checkout); - console.log("addVariant関数の中でのsetCheckoutのあと"); setLoading(false) } diff --git a/pages/cart.tsx b/pages/cart.tsx index d904937..eb13e03 100644 --- a/pages/cart.tsx +++ b/pages/cart.tsx @@ -1,5 +1,60 @@ -import React from "react"; +import React, { createContext } from "react"; +import { GetServerSideProps } from "next"; +import { Cart } from "shopify-buy"; +import { CircularProgress } from "@material-ui/core"; +import Layout from "components/common/Layout"; +import CartItemTable from "components/cart/CartItemTable"; +import useCheckout from 'lib/useCheckout'; -const Cart: React.FC = () =>
カートページです
; +export const CheckoutContext = createContext(null); -export default Cart; +const CartPage: React.FC = () => { + + const { checkout } = useCheckout(); + + return ( + + +
+
+

+ ショッピングカート +

+
+
+
+ {checkout ? ( + checkout.lineItems.length > 0 ? ( + + ) : ( + <> +
カート内に商品がありません
+ + + ) + ) : ( +
+ +
+ )} + {} +
+
+
+
+
+ ); +}; + +export default CartPage; diff --git a/pages/collections/all.tsx b/pages/collections/all.tsx index f20c0b4..5b6a3dc 100644 --- a/pages/collections/all.tsx +++ b/pages/collections/all.tsx @@ -2,9 +2,8 @@ import React from "react"; import { GetServerSideProps } from "next"; import client from "lib/client"; import { Product } from "shopify-buy"; -import { fetchCollectionWithProducts } from 'lib/graphql/collection' +import { fetchCollectionWithProducts } from "lib/graphql/collection"; import Layout from "components/common/Layout"; -import CollectionTitle from "components/collections/CollectionTitle" import FilterToolbar from "components/collections/FilterToolbar"; import ProductList from "components/collections/ProductList"; import Pagination from "components/utils/Pagination"; @@ -26,7 +25,9 @@ const collectionAll: React.FC = ({
- +

+ 商品 +

@@ -39,13 +40,9 @@ const collectionAll: React.FC = ({ ); - - - export const getServerSideProps: GetServerSideProps = async (context) => { + // サーバーサイドでこれを実行するとIP単位のコスト計算→すぐにコスト超過してしまうと思われる。。。 - // const collection = await fetchCollectionWithProducts(); - const products: Product[] = await client.product.fetchAll(); const total = products.length; @@ -65,6 +62,6 @@ export const getServerSideProps: GetServerSideProps = async (context) => { totalPage, }, }; -};; +}; export default collectionAll;