diff --git a/.husky/pre-commit b/.husky/pre-commit index e69de29..6a109d8 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -0,0 +1,5 @@ +#!/usr/bin/env sh +. "$(dirname -- "$0")/_/husky.sh" + +npm test +./node_modules/.bin/lint-staged diff --git a/examples/nextjs-root-layout/.eslintrc.json b/examples/nextjs-app-router/.eslintrc.json similarity index 100% rename from examples/nextjs-root-layout/.eslintrc.json rename to examples/nextjs-app-router/.eslintrc.json diff --git a/examples/nextjs-root-layout/.gitignore b/examples/nextjs-app-router/.gitignore similarity index 100% rename from examples/nextjs-root-layout/.gitignore rename to examples/nextjs-app-router/.gitignore diff --git a/examples/nextjs-root-layout/README.md b/examples/nextjs-app-router/README.md similarity index 100% rename from examples/nextjs-root-layout/README.md rename to examples/nextjs-app-router/README.md diff --git a/examples/nextjs-root-layout/jsconfig.json b/examples/nextjs-app-router/jsconfig.json similarity index 100% rename from examples/nextjs-root-layout/jsconfig.json rename to examples/nextjs-app-router/jsconfig.json diff --git a/examples/nextjs-root-layout/next.config.js b/examples/nextjs-app-router/next.config.js similarity index 100% rename from examples/nextjs-root-layout/next.config.js rename to examples/nextjs-app-router/next.config.js diff --git a/examples/nextjs-root-layout/package.json b/examples/nextjs-app-router/package.json similarity index 75% rename from examples/nextjs-root-layout/package.json rename to examples/nextjs-app-router/package.json index 73d434e..1cc24fe 100644 --- a/examples/nextjs-root-layout/package.json +++ b/examples/nextjs-app-router/package.json @@ -1,5 +1,5 @@ { - "name": "nextjs-root-layout", + "name": "nextjs-app-router", "version": "0.1.0", "private": true, "scripts": { @@ -9,6 +9,7 @@ "lint": "next lint" }, "dependencies": { + "@stripe/stripe-js": "^1.14.0", "autoprefixer": "10.4.14", "eslint": "8.40.0", "eslint-config-next": "13.4.1", @@ -16,8 +17,8 @@ "postcss": "8.4.23", "react": "18.2.0", "react-dom": "18.2.0", + "stripe": "^13.10.0", "tailwindcss": "3.3.2", - "use-shopping-cart": "workspace:^3.1.5" + "use-shopping-cart": "3.2.0-alpha.0" } } - diff --git a/examples/nextjs-root-layout/postcss.config.js b/examples/nextjs-app-router/postcss.config.js similarity index 100% rename from examples/nextjs-root-layout/postcss.config.js rename to examples/nextjs-app-router/postcss.config.js diff --git a/examples/nextjs-root-layout/src/app/components/CartItem.js b/examples/nextjs-app-router/src/app/components/CartItem.js similarity index 63% rename from examples/nextjs-root-layout/src/app/components/CartItem.js rename to examples/nextjs-app-router/src/app/components/CartItem.js index d0425ec..346a7dd 100644 --- a/examples/nextjs-root-layout/src/app/components/CartItem.js +++ b/examples/nextjs-app-router/src/app/components/CartItem.js @@ -1,14 +1,14 @@ -import { useShoppingCart } from "use-shopping-cart"; -import { formatCurrencyString } from "use-shopping-cart"; -import Image from "next/image"; +'use client' +import { useShoppingCart, formatCurrencyString } from 'use-shopping-cart' +import Image from 'next/image' export default function CartItem({ item }) { - const { name, emoji, quantity, price } = item; - const { removeItem } = useShoppingCart(); + const { name, emoji, quantity, price } = item + const { removeItem } = useShoppingCart() const removeItemFromCart = () => { - removeItem(item.id); - }; + removeItem(item.id) + } return (
@@ -17,7 +17,7 @@ export default function CartItem({ item }) { {name} ({quantity})
- {formatCurrencyString({ value: price, currency: "GBP" })} + {formatCurrencyString({ value: price, currency: 'GBP' })}
- ); + ) } diff --git a/examples/nextjs-app-router/src/app/components/CheckoutButton.js b/examples/nextjs-app-router/src/app/components/CheckoutButton.js new file mode 100644 index 0000000..6f20150 --- /dev/null +++ b/examples/nextjs-app-router/src/app/components/CheckoutButton.js @@ -0,0 +1,61 @@ +import { useState } from 'react' +import { useShoppingCart } from 'use-shopping-cart' + +export default function CheckoutButton() { + const [status, setStatus] = useState('idle') + const { redirectToCheckout, cartCount, totalPrice, cartDetails } = + useShoppingCart() + + async function handleClick(event) { + event.preventDefault() + if (cartCount > 0) { + setStatus('loading') + try { + const res = await fetch('/session', { + method: 'POST', + body: JSON.stringify(cartDetails) + }) + const data = await res.json() + const result = await redirectToCheckout(data.sessionId) + if (result?.error) { + console.error(result) + setStatus('redirect-error') + } + } catch (error) { + console.error(error) + setStatus('redirect-error') + } + } else { + setStatus('no-items') + } + } + + return ( +
+
+ {totalPrice && totalPrice < 30 + ? 'You must have at least £0.30 in your basket' + : cartCount && cartCount > 20 + ? 'You cannot have more than 20 items' + : status === 'redirect-error' + ? 'Unable to redirect to Stripe checkout page' + : status === 'no-items' + ? 'Please add some items to your cart' + : null} +
+ +
+ ) +} diff --git a/examples/nextjs-root-layout/src/app/components/NavBar.js b/examples/nextjs-app-router/src/app/components/NavBar.js similarity index 74% rename from examples/nextjs-root-layout/src/app/components/NavBar.js rename to examples/nextjs-app-router/src/app/components/NavBar.js index 25b440c..c0bcb78 100644 --- a/examples/nextjs-root-layout/src/app/components/NavBar.js +++ b/examples/nextjs-app-router/src/app/components/NavBar.js @@ -8,11 +8,12 @@ import ShoppingCart from './ShoppingCart' export default function NavBar() { const { handleCartClick, cartCount } = useShoppingCart() return ( -