stacker.news/pages/invites/[id].js

95 lines
2.5 KiB
JavaScript
Raw Normal View History

import Login from '@/components/login'
import { getProviders } from 'next-auth/react'
import { getServerSession } from 'next-auth/next'
import models from '@/api/models'
import serialize from '@/api/resolvers/serial'
2021-10-15 23:07:51 +00:00
import { gql } from '@apollo/client'
import { INVITE_FIELDS } from '@/fragments/invites'
import getSSRApolloClient from '@/api/ssrApollo'
2021-10-15 23:07:51 +00:00
import Link from 'next/link'
import { CenterLayout } from '@/components/layout'
2024-03-20 18:41:32 +00:00
import { getAuthOptions } from '@/pages/api/auth/[...nextauth]'
import { notifyInvite } from '@/lib/webPush'
2021-10-15 18:05:34 +00:00
export async function getServerSideProps ({ req, res, query: { id, error = null } }) {
2023-07-31 14:10:45 +00:00
const session = await getServerSession(req, res, getAuthOptions(req))
2021-10-15 18:05:34 +00:00
const client = await getSSRApolloClient({ req, res })
2021-10-15 23:07:51 +00:00
const { data } = await client.query({
query: gql`
${INVITE_FIELDS}
{
invite(id: "${id}") {
...InviteFields
}
}`
})
if (!data?.invite) {
2024-04-03 00:51:30 +00:00
res.writeHead(302, {
2024-02-23 15:32:20 +00:00
Location: '/404'
}).end()
2024-04-03 00:51:30 +00:00
return { props: {} }
2021-10-15 23:07:51 +00:00
}
2021-10-15 18:05:34 +00:00
if (session && res) {
2021-10-15 23:07:51 +00:00
try {
// attempt to send gift
// catch any errors and just ignore them for now
await serialize(
models.$queryRawUnsafe('SELECT invite_drain($1::INTEGER, $2::TEXT)', session.user.id, id),
{ models }
)
const invite = await models.invite.findUnique({ where: { id } })
notifyInvite(invite.userId)
2021-10-15 23:07:51 +00:00
} catch (e) {
console.log(e)
}
2024-04-03 00:51:30 +00:00
res.writeHead(302, {
Location: '/'
}).end()
return { props: {} }
2021-10-15 18:05:34 +00:00
}
return {
props: {
providers: await getProviders(),
2024-04-08 22:54:39 +00:00
callbackUrl: process.env.NEXT_PUBLIC_URL + req.url,
2021-10-15 23:07:51 +00:00
invite: data.invite,
2021-10-15 18:05:34 +00:00
error
}
}
}
2021-10-15 23:07:51 +00:00
function InviteHeader ({ invite }) {
let Inner
if (invite.revoked) {
Inner = () => <div className='text-danger'>this invite link expired</div>
} else if ((invite.limit && invite.limit <= invite.invitees.length) || invite.poor) {
Inner = () => <div className='text-danger'>this invite link has no more sats</div>
} else {
Inner = () => (
<div>
2023-01-10 00:33:44 +00:00
Get <span className='text-success'>{invite.gift} free sats</span> from{' '}
<Link href={`/${invite.user.name}`}>@{invite.user.name}</Link>{' '}
2021-10-15 23:07:51 +00:00
when you sign up today
</div>
)
}
return (
2023-01-10 00:33:44 +00:00
<h3 className='text-center pb-3'>
2021-10-15 23:07:51 +00:00
<Inner />
2023-01-10 00:33:44 +00:00
</h3>
2021-10-15 23:07:51 +00:00
)
}
export default function Invite ({ invite, ...props }) {
return (
<CenterLayout>
<Login Header={() => <InviteHeader invite={invite} />} text='Sign up' {...props} />
</CenterLayout>
)
2021-10-15 23:07:51 +00:00
}