This repository has been archived by the owner on Jan 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.tsx
75 lines (71 loc) · 2.11 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { UserButton, UserProfile } from "@clerk/nextjs";
import Head from "next/head";
import { Gate, useSubscription } from "use-stripe-subscription";
export default function Home() {
const {
isLoaded,
products,
subscription,
redirectToCheckout,
redirectToCustomerPortal,
} = useSubscription();
if (!isLoaded) {
return null;
}
const alertResponse = async (path) => {
const res = await fetch(path);
const body = await res.text();
alert(`Path requested: ${path}\nResponse: ${body}`);
};
return (
<main style={{ padding: "1rem 2rem" }}>
<Head>
<title>use-stripe-subscription</title>
</Head>
<h1>use-stripe-subscription demo</h1>
<h2>Plans</h2>
{products.map(({ product, prices }) => (
<div key={product.id}>
<h4>{product.name}</h4>
<Gate unsubscribed>
{prices.map((price) => (
<button
key={price.id}
onClick={() => redirectToCheckout({ price: price.id })}
>
Purchase {price.unit_amount} {price.currency}
</button>
))}
</Gate>
<Gate product={product}>Active plan</Gate>
<Gate product={product} negate>
<button onClick={() => redirectToCustomerPortal()}>
Change plan
</button>
</Gate>
</div>
))}
<h2>Features</h2>
<div>
<Gate feature="feature1">Plan has "feature1"</Gate>
<Gate feature="feature1" negate>
Plan does not have "feature1"
</Gate>{" "}
<button onClick={() => alertResponse("/api/tryFeature1")}>
Test the backend!
</button>
</div>
<div>
<Gate feature="feature2">Plan has "feature2"</Gate>
<Gate feature="feature2" negate>
Plan does not have "feature2"
</Gate>{" "}
<button onClick={() => alertResponse("/api/tryFeature2")}>
Test the backend!
</button>
</div>
<h2>Account management</h2>
<UserButton />
</main>
);
}