-
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example/nextjs-app-router (#334)
* setting up new nextjs example for SSR testing * Add example/nextjs-app-router * Remove tsconfig.json * fix husky, remove nextjs test, update to use alpha version of USC * update .husky file * Add date-fns in package.json * Refactor code as per next13 app router --------- Co-authored-by: Nick DeJesus <[email protected]>
- Loading branch information
1 parent
0df8624
commit 46d1e87
Showing
30 changed files
with
1,017 additions
and
493 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env sh | ||
. "$(dirname -- "$0")/_/husky.sh" | ||
|
||
npm test | ||
./node_modules/.bin/lint-staged |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
examples/nextjs-app-router/src/app/components/CheckoutButton.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<article className="mt-3 flex flex-col"> | ||
<div className="text-red-700 text-xs mb-3 h-5 text-center"> | ||
{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} | ||
</div> | ||
<button | ||
onClick={handleClick} | ||
className="bg-emerald-50 hover:bg-emerald-500 hover:text-white transition-colors duration-500 text-emerald-500 py-3 px-5 rounded-md w-100 disabled:bg-slate-300 disabled:cursor-not-allowed disabled:text-white" | ||
disabled={ | ||
(totalPrice && totalPrice < 30) || | ||
(cartCount && cartCount > 20) || | ||
status == 'no-items' | ||
? true | ||
: false | ||
} | ||
> | ||
{status !== 'loading' ? 'Proceed to checkout' : 'Loading...'} | ||
</button> | ||
</article> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 6 additions & 6 deletions
12
...ples/nextjs-root-layout/src/app/layout.js → examples/nextjs-app-router/src/app/layout.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
import './globals.css' | ||
import { Inter } from 'next/font/google' | ||
|
||
// import { CartProvider } from 'use-shopping-cart' | ||
import CartProvider from './components/providers' | ||
import Layout from './components/Layout' | ||
import NavBar from './components/NavBar' | ||
|
||
const inter = Inter({ subsets: ['latin'] }) | ||
|
||
export const metadata = { | ||
title: 'Create Next App', | ||
description: 'Generated by create next app' | ||
title: 'Fresh', | ||
description: | ||
'Next.js 13 app router example to show how to use use-shopping-cart' | ||
} | ||
|
||
export default function RootLayout({ children }) { | ||
return ( | ||
<html lang="en"> | ||
<body className={inter.className}> | ||
<CartProvider> | ||
<Layout>{children}</Layout> | ||
<NavBar /> | ||
{children} | ||
</CartProvider> | ||
</body> | ||
</html> | ||
) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Product from './components/Product' | ||
import { products } from './data/products' | ||
|
||
export default function Home() { | ||
return ( | ||
<main className="bg-[#f8f7f5] min-h-[calc(100vh-76px)] px-10 py-8"> | ||
<div className="container md:mx-auto md:max-w-[850px]"> | ||
<div className="grid sm:grid-cols-2 md:grid-cols-4 justify-center mx-auto gap-4 place-center flex-wrap w-100 md:max-w-[900px]"> | ||
{products.map((product) => ( | ||
<Product product={product} key={product.id} /> | ||
))} | ||
</div> | ||
</div> | ||
</main> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { stripe } from '../../lib/stripe' | ||
import { headers } from 'next/headers' | ||
import { products } from '../data/products' | ||
import { validateCartItems } from 'use-shopping-cart/utilities' | ||
|
||
export async function POST(request) { | ||
const inventory = products | ||
const cartProducts = await request.json() | ||
const line_items = validateCartItems(inventory, cartProducts) | ||
console.log('line_items', line_items) | ||
const checkoutSession = await stripe.checkout.sessions.create({ | ||
mode: 'payment', | ||
submit_type: 'pay', | ||
line_items, | ||
success_url: `${headers().get('origin')}/success`, | ||
cancel_url: `${headers().get('origin')}/` | ||
}) | ||
return Response.json({ sessionId: checkoutSession.id }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function SuccessPage() { | ||
return ( | ||
<p className="text-black text-center"> | ||
Hi, Your order has been successfully placed | ||
</p> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Stripe from 'stripe' | ||
export const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, { | ||
// https://github.com/stripe/stripe-node#configuration | ||
apiVersion: '2023-08-16', | ||
appInfo: { | ||
name: 'projectname', | ||
url: 'http://localhost:3000/' | ||
} | ||
}) |
File renamed without changes.
55 changes: 0 additions & 55 deletions
55
examples/nextjs-root-layout/src/app/components/CheckoutButton.js
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.