/
index.tsx
316 lines (293 loc) · 9.55 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import * as React from 'react'
import {convertToSerializeForNextResponse} from '@skillrecordings/commerce-server'
import type {SanityProduct} from '@skillrecordings/commerce-server/dist/@types'
import {useSession} from 'next-auth/react'
import {type GetServerSideProps} from 'next'
import {getToken} from 'next-auth/jwt'
import Layout from '@/components/layout'
import {getSdk, prisma} from '@skillrecordings/database'
import Link from 'next/link'
import {first, isString} from 'lodash'
import InviteTeam from '@skillrecordings/skill-lesson/team'
import {InvoiceCard} from '@/pages/invoices'
import {getAllProducts} from '@/server/products.server'
import Image from 'next/legacy/image'
import {trpc} from '../../trpc/trpc.client'
import {Transfer} from '@/purchase-transfer/purchase-transfer'
import {paymentOptions} from '../api/skill/[...skillRecordings]'
export const getServerSideProps: GetServerSideProps = async ({req, query}) => {
const {purchaseId: purchaseQueryParam, upgrade} = query
const session_id =
query.session_id instanceof Array ? query.session_id[0] : query.session_id
const token = await getToken({req})
const {getPurchaseDetails} = getSdk()
const paymentProvider = paymentOptions.providers.stripe
let purchaseId = purchaseQueryParam
if (session_id && paymentProvider) {
const {chargeIdentifier} = await paymentProvider.getPurchaseInfo(session_id)
const purchase = await prisma.purchase.findFirst({
where: {
merchantCharge: {
identifier: chargeIdentifier,
},
},
})
if (purchase) {
purchaseId = purchase.id
} else {
return {
redirect: {
destination: `/thanks/purchase?session_id=${session_id}`,
permanent: false,
},
}
}
}
if (token && isString(purchaseId) && isString(token?.sub)) {
const {purchase, existingPurchase, availableUpgrades} =
await getPurchaseDetails(purchaseId, token.sub)
if (purchase) {
const products = await getAllProducts()
const purchasedProduct = first(
products.filter(
(product: SanityProduct) => product.productId === purchase.product.id,
),
)
return {
props: {
product: purchasedProduct,
purchase: convertToSerializeForNextResponse(purchase),
existingPurchase,
availableUpgrades,
upgrade: upgrade === 'true',
},
}
} else {
return {
redirect: {
destination: `/`,
permanent: false,
},
}
}
}
return {
redirect: {
destination: `/`,
permanent: false,
},
}
}
type Purchase = {
id: string
merchantChargeId: string | null
bulkCoupon: {id: string; maxUses: number; usedCount: number} | null
product: {id: string; name: string}
}
type PersonalPurchase = {
id: string
product: {
id: string
name: string
}
}
const Welcome: React.FC<
React.PropsWithChildren<{
purchase: Purchase
existingPurchase: {
id: string
product: {id: string; name: string}
}
token: any
availableUpgrades: {upgradableTo: {id: string; name: string}}[]
upgrade: boolean
product?: SanityProduct
}>
> = ({
upgrade,
purchase,
token,
existingPurchase,
availableUpgrades,
product,
}) => {
const {data: session, status} = useSession()
const [personalPurchase, setPersonalPurchase] = React.useState<
PersonalPurchase | Purchase
>(purchase.bulkCoupon ? existingPurchase : purchase)
const redemptionsLeft =
purchase.bulkCoupon &&
purchase.bulkCoupon.maxUses > purchase.bulkCoupon.usedCount
const hasCharge = Boolean(purchase.merchantChargeId)
const {data: purchaseUserTransfers, refetch} =
trpc.purchaseUserTransfer.forPurchaseId.useQuery({
id: purchase.id,
})
const isTransferAvailable =
!purchase.bulkCoupon &&
Boolean(
purchaseUserTransfers?.filter((purchaseUserTransfer) =>
['AVAILABLE', 'INITIATED', 'COMPLETED'].includes(
purchaseUserTransfer.transferState,
),
).length,
)
return (
<Layout
meta={{title: `Welcome to ${process.env.NEXT_PUBLIC_SITE_TITLE}`}}
footer={null}
>
<main
className="mx-auto flex w-full flex-grow flex-col items-center justify-center px-5 pb-32 pt-10"
id="welcome"
>
<div className="flex w-full max-w-screen-md flex-col gap-3">
<Header
product={product}
upgrade={upgrade}
purchase={purchase}
personalPurchase={personalPurchase}
/>
<div className="flex flex-col gap-10">
<div>
<h2 className="pb-2 font-heading text-sm font-black uppercase">
Share Testing Javascript
</h2>
<Share productName={purchase.product.name} />
</div>
{redemptionsLeft && (
<div>
<h2 className="pb-2 font-heading text-sm font-black uppercase">
Invite your team
</h2>
<Invite
setPersonalPurchase={setPersonalPurchase}
session={session}
purchase={purchase}
existingPurchase={existingPurchase}
/>
</div>
)}
{hasCharge && (
<div>
<h2 className="pb-2 font-heading text-sm font-black uppercase">
Get your invoice
</h2>
<InvoiceCard purchase={purchase} />
</div>
)}
{isTransferAvailable && purchaseUserTransfers && (
<div>
<h2 className="pb-2 font-heading text-sm font-black uppercase">
Transfer this purchase to another email address
</h2>
<Transfer
purchaseUserTransfers={purchaseUserTransfers}
refetch={refetch}
/>
</div>
)}
</div>
</div>
</main>
</Layout>
)
}
const Header: React.FC<
React.PropsWithChildren<{
upgrade: boolean
purchase: Purchase
personalPurchase?: PersonalPurchase | Purchase
product?: SanityProduct
}>
> = ({upgrade, purchase, product, personalPurchase}) => {
return (
<header>
<div className="flex flex-col items-center gap-10 pb-8 sm:flex-row">
{product?.image?.url && (
<div className="flex flex-shrink-0 items-center justify-center">
<Image
src={product.image.url}
alt={product.title}
width={250}
height={250}
/>
</div>
)}
<div className="flex flex-col items-start">
<h1 className="font-heading text-3xl font-black sm:text-3xl lg:text-4xl">
<span className="block pb-4 font-heading text-sm font-black uppercase text-brand-red">
{upgrade ? `You've Upgraded ` : `Welcome to `}
</span>
{purchase.product.name}
</h1>
{personalPurchase && product && (
<Link
href={`/playlists/${product.modules[0].slug}`}
className="mt-8 rounded-full bg-brand-red px-8 py-3 font-heading text-lg font-bold shadow-xl shadow-black/10 transition hover:brightness-110"
>
Start Learning
</Link>
)}
</div>
</div>
{/* {purchase.bulkCoupon
? `${purchase.product?.name} team license!`
: `${purchase.product?.name} license!`} */}
</header>
)
}
const Invite: React.FC<React.PropsWithChildren<any>> = ({
setPersonalPurchase,
session,
purchase,
existingPurchase,
}) => {
return (
<InviteTeam
setPersonalPurchase={setPersonalPurchase}
session={session}
purchase={purchase}
existingPurchase={existingPurchase}
/>
)
}
const Share: React.FC<React.PropsWithChildren<{productName: string}>> = ({
productName,
}) => {
const tweet = `https://twitter.com/intent/tweet/?text=Testing Javascript by @${process.env.NEXT_PUBLIC_PARTNER_TWITTER} 🧙 https%3A%2F%2Fwww.testingjavascript.com%2F`
return (
<div className="flex flex-col justify-between gap-5 rounded-lg border border-gray-100 bg-white px-5 py-6 shadow-xl shadow-gray-400/5 sm:flex-row sm:items-center">
<p>
Tell your friends about {process.env.NEXT_PUBLIC_SITE_TITLE},{' '}
<br className="hidden sm:block" />
it would help me to get a word out.{' '}
<span role="img" aria-label="smiling face">
😊
</span>
</p>
<a
href={tweet}
rel="noopener noreferrer"
target="_blank"
className="flex items-center gap-2 self-start rounded-full border border-sky-500 px-5 py-2.5 font-heading font-semibold text-sky-500 transition hover:bg-sky-500 hover:text-white"
>
<TwitterIcon /> Share with your friends!
</a>
</div>
)
}
export const TwitterIcon = () => (
<svg
aria-hidden="true"
height="16"
width="16"
viewBox="0 0 16 16"
xmlns="http://www.w3.org/2000/svg"
>
<g fill="currentColor">
<path d="M16,3c-0.6,0.3-1.2,0.4-1.9,0.5c0.7-0.4,1.2-1,1.4-1.8c-0.6,0.4-1.3,0.6-2.1,0.8c-0.6-0.6-1.5-1-2.4-1 C9.3,1.5,7.8,3,7.8,4.8c0,0.3,0,0.5,0.1,0.7C5.2,5.4,2.7,4.1,1.1,2.1c-0.3,0.5-0.4,1-0.4,1.7c0,1.1,0.6,2.1,1.5,2.7 c-0.5,0-1-0.2-1.5-0.4c0,0,0,0,0,0c0,1.6,1.1,2.9,2.6,3.2C3,9.4,2.7,9.4,2.4,9.4c-0.2,0-0.4,0-0.6-0.1c0.4,1.3,1.6,2.3,3.1,2.3 c-1.1,0.9-2.5,1.4-4.1,1.4c-0.3,0-0.5,0-0.8,0c1.5,0.9,3.2,1.5,5,1.5c6,0,9.3-5,9.3-9.3c0-0.1,0-0.3,0-0.4C15,4.3,15.6,3.7,16,3z" />
</g>
</svg>
)
export default Welcome