This repository has been archived by the owner on Jun 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
125 lines (117 loc) 路 3.67 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import NextLink from "next/link";
import { useRouter } from "next/router";
import DefaultErrorPage from "next/error";
import Stripe from "stripe";
import { getURL, isValidStripeId } from "../../../utils/helpers";
import Layout from "../../../components/Layout";
import NavBar from "../../../components/NavBar";
import {
Skeleton,
Flex,
Breadcrumb,
BreadcrumbItem,
BreadcrumbLink,
Box,
} from "@chakra-ui/core";
import Cart from "../../../components/Cart";
import SuccessModal from "../../../components/SuccessModal";
import ProductDetailCard from "../../../components/ProductDetailCard";
import { useManageCart } from "../../../utils/cart-manager";
import { useEffect } from "react";
export default function ProductPage({
account,
product,
}: {
account: {
id: string;
name: string;
details_submitted: boolean;
default_currency: string;
branding: Stripe.Account.Settings.Branding;
};
product: Stripe.Product;
}) {
const { initMerchant } = useManageCart();
useEffect(() => initMerchant(), []);
const router = useRouter();
const { success } = router.query;
// If the page is not yet generated, this will be displayed
// initially until getStaticProps() finishes running
// If the page is not yet generated, this will be displayed
// initially until getStaticProps() finishes running
if (router.isFallback)
return (
<Layout>
<NavBar />
<Skeleton height="20px" my="10px" />
<Skeleton height="20px" my="10px" />
<Skeleton height="20px" my="10px" />
</Layout>
);
if (!product) return <DefaultErrorPage statusCode={404} />;
return (
<Layout account={account} product={product}>
<Cart merchant={account.id} currency={account.default_currency}>
<NavBar account={account} />
<SuccessModal account={account} success={success} />
<Flex p={4} align="center" justify="center">
<Box>
<Breadcrumb mb={4}>
<BreadcrumbItem>
<NextLink href={`/${account.id}`} passHref>
<BreadcrumbLink>All products</BreadcrumbLink>
</NextLink>
</BreadcrumbItem>
<BreadcrumbItem isCurrentPage>
<BreadcrumbLink>{product.name}</BreadcrumbLink>
</BreadcrumbItem>
</Breadcrumb>
<ProductDetailCard product={product} account={account} />
</Box>
</Flex>
</Cart>
</Layout>
);
}
// This function gets called at build time
export async function getStaticPaths() {
return {
// We don't want to prebuilt the pages so we pass an empty array.
paths: [],
// We set fallback:true so the page will be built the first time it is visited.
fallback: true,
};
}
// This get's called the first time our page is visited as well as when the revalidate timer expires.
export async function getStaticProps({
params,
}: {
params: { merchant: string; product: string };
}) {
const props: { account: object; product: object } = {
account: null,
product: null,
};
if (
isValidStripeId("account", params.merchant) &&
isValidStripeId("product", params.product)
) {
const accountRes = await fetch(
`${getURL()}/api/accounts/${params.merchant}`
);
const { account } = await accountRes.json();
props.account = account;
const res = await fetch(
`${getURL()}/api/products/${params.merchant}/${params.product}`
);
const { product } = await res.json();
props.product = product;
}
// Pass the product data to the page via props
return {
props,
// Re-generate the product page at most once per minute
// if a request comes in
revalidate: 60, // in seconds
};
}