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

94 lines
2.6 KiB
JavaScript
Raw Normal View History

2021-10-15 18:05:34 +00:00
import Login from '../../components/login'
import { getProviders } from 'next-auth/react'
import { getServerSession } from 'next-auth/next'
2021-10-15 23:07:51 +00:00
import models from '../../api/models'
import serialize from '../../api/resolvers/serial'
import { gql } from '@apollo/client'
import { INVITE_FIELDS } from '../../fragments/invites'
import getSSRApolloClient from '../../api/ssrApollo'
import Link from 'next/link'
import { CenterLayout } from '../../components/layout'
2023-07-31 14:10:45 +00:00
import { getAuthOptions } from '../api/auth/[...nextauth]'
import { sendUserNotification } from '../../api/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) {
return {
notFound: true
}
}
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,
2023-07-27 00:18:42 +00:00
models.$queryRawUnsafe('SELECT invite_drain($1::INTEGER, $2::INTEGER)', session.user.id, id))
const invite = await models.invite.findUnique({ where: { id } })
sendUserNotification(invite.userId, { title: 'your invite has been redeemed', tag: 'INVITE' }).catch(console.error)
2021-10-15 23:07:51 +00:00
} catch (e) {
console.log(e)
}
return {
redirect: {
destination: '/',
permanent: false
}
}
2021-10-15 18:05:34 +00:00
}
return {
props: {
providers: await getProviders(),
2021-11-11 17:38:41 +00:00
callbackUrl: process.env.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
}