Skip to content

Commit 275cb65

Browse files
authored
Fix/remove client check (#322)
* add nextjs example with root layout (this looks like dog shit rn) * adjust conditions for rendering with persistence * add "use client" directive to react modules (doesn't actually work tho) * update lib version
1 parent 92b7371 commit 275cb65

25 files changed

+1415
-18
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "next/core-web-vitals"
3+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
27+
# local env files
28+
.env*.local
29+
30+
# vercel
31+
.vercel
32+
33+
# typescript
34+
*.tsbuildinfo
35+
next-env.d.ts

examples/nextjs-root-layout/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
2+
3+
## Getting Started
4+
5+
First, run the development server:
6+
7+
```bash
8+
npm run dev
9+
# or
10+
yarn dev
11+
# or
12+
pnpm dev
13+
```
14+
15+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
16+
17+
You can start editing the page by modifying `app/page.js`. The page auto-updates as you edit the file.
18+
19+
This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.
20+
21+
## Learn More
22+
23+
To learn more about Next.js, take a look at the following resources:
24+
25+
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
26+
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
27+
28+
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
29+
30+
## Deploy on Vercel
31+
32+
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
33+
34+
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"paths": {
4+
"@/*": ["./src/*"]
5+
}
6+
}
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {}
3+
4+
module.exports = nextConfig
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "nextjs-root-layout",
3+
"version": "0.1.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "next dev",
7+
"build": "next build",
8+
"start": "next start",
9+
"lint": "next lint"
10+
},
11+
"dependencies": {
12+
"autoprefixer": "10.4.14",
13+
"eslint": "8.40.0",
14+
"eslint-config-next": "13.4.1",
15+
"next": "13.4.1",
16+
"postcss": "8.4.23",
17+
"react": "18.2.0",
18+
"react-dom": "18.2.0",
19+
"tailwindcss": "3.3.2",
20+
"use-shopping-cart": "workspace:^3.1.5"
21+
}
22+
}
23+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
plugins: {
3+
tailwindcss: {},
4+
autoprefixer: {},
5+
},
6+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { useShoppingCart } from "use-shopping-cart";
2+
import { formatCurrencyString } from "use-shopping-cart";
3+
import Image from "next/image";
4+
5+
export default function CartItem({ item }) {
6+
const { name, emoji, quantity, price } = item;
7+
const { removeItem } = useShoppingCart();
8+
9+
const removeItemFromCart = () => {
10+
removeItem(item.id);
11+
};
12+
13+
return (
14+
<div className="flex items-center gap-4 mb-3">
15+
<p className="text-4xl">{emoji}</p>
16+
<div>
17+
{name} <span className="text-xs">({quantity})</span>
18+
</div>
19+
<div className="ml-auto">
20+
{formatCurrencyString({ value: price, currency: "GBP" })}
21+
</div>
22+
<button
23+
onClick={() => removeItemFromCart()}
24+
className="hover:bg-emerald-50 transition-colors rounded-full duration-500 p-1"
25+
>
26+
<Image alt="delete icon" src="./trash.svg" width={20} height={20} />
27+
</button>
28+
</div>
29+
);
30+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { useState } from "react";
2+
import { useShoppingCart } from "use-shopping-cart";
3+
4+
export default function CheckoutButton() {
5+
const [status, setStatus] = useState("idle");
6+
const { redirectToCheckout, cartCount, totalPrice } = useShoppingCart();
7+
8+
async function handleClick(event) {
9+
event.preventDefault();
10+
if (cartCount > 0) {
11+
setStatus("loading");
12+
try {
13+
const result = await redirectToCheckout();
14+
if (result?.error) {
15+
console.error(result);
16+
setStatus("redirect-error");
17+
}
18+
} catch (error) {
19+
console.error(error);
20+
setStatus("redirect-error");
21+
}
22+
} else {
23+
setStatus("no-items");
24+
}
25+
}
26+
27+
return (
28+
<article className="mt-3 flex flex-col">
29+
<div className="text-red-700 text-xs mb-3 h-5 text-center">
30+
{totalPrice && totalPrice < 30
31+
? "You must have at least £0.30 in your basket"
32+
: cartCount && cartCount > 20
33+
? "You cannot have more than 20 items"
34+
: status === "redirect-error"
35+
? "Unable to redirect to Stripe checkout page"
36+
: status === "no-items"
37+
? "Please add some items to your cart"
38+
: null}
39+
</div>
40+
<button
41+
onClick={handleClick}
42+
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"
43+
disabled={
44+
(totalPrice && totalPrice < 30) ||
45+
(cartCount && cartCount > 20) ||
46+
status == "no-items"
47+
? true
48+
: false
49+
}
50+
>
51+
{status !== "loading" ? "Proceed to checkout" : "Loading..."}
52+
</button>
53+
</article>
54+
);
55+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use client'
2+
3+
import Head from 'next/head'
4+
import NavBar from './NavBar'
5+
6+
export default function Layout({ children }) {
7+
return (
8+
<>
9+
<Head>
10+
<title>fresh</title>
11+
<meta
12+
name="description"
13+
content="A simple website to show how to use use-shopping-cart"
14+
/>
15+
<meta name="viewport" content="width=device-width, initial-scale=1" />
16+
<link rel="icon" href="/favicon.ico" />
17+
</Head>
18+
<NavBar />
19+
<main className="bg-[#f8f7f5] min-h-[calc(100vh-76px)] px-10 py-8">
20+
<div className="container md:mx-auto md:max-w-[850px]">{children}</div>
21+
</main>
22+
</>
23+
)
24+
}

0 commit comments

Comments
 (0)