stacker.news/pages/invites/index.js

174 lines
4.5 KiB
JavaScript
Raw Normal View History

2021-10-12 23:49:04 +00:00
import Layout from '../../components/layout'
import * as Yup from 'yup'
2021-10-14 21:05:37 +00:00
import { CopyInput, Form, Input, SubmitButton } from '../../components/form'
2021-10-12 23:49:04 +00:00
import { InputGroup } from 'react-bootstrap'
import { gql, useMutation, useQuery } from '@apollo/client'
import { INVITE_FIELDS } from '../../fragments/invites'
2021-10-14 21:05:37 +00:00
import AccordianItem from '../../components/accordian-item'
import styles from '../../styles/invites.module.css'
2021-10-12 23:49:04 +00:00
export const InviteSchema = Yup.object({
gift: Yup.number().typeError('must be a number')
.min(0, 'must be positive').integer('must be whole').required(),
limit: Yup.number().typeError('must be a number')
.positive('must be positive').integer('must be whole')
})
function InviteForm () {
const [createInvite] = useMutation(
gql`
${INVITE_FIELDS}
mutation createInvite($gift: Int!, $limit: Int) {
createInvite(gift: $gift, limit: $limit) {
...InviteFields
}
}`, {
update (cache, { data: { createInvite } }) {
cache.modify({
fields: {
invites (existingInviteRefs = []) {
const newInviteRef = cache.writeFragment({
data: createInvite,
fragment: INVITE_FIELDS
})
return [newInviteRef, ...existingInviteRefs]
}
}
})
}
}
)
return (
<Form
initial={{
gift: 100,
limit: undefined
}}
schema={InviteSchema}
2021-10-15 23:07:51 +00:00
onSubmit={async ({ limit, gift }) => {
2021-10-12 23:49:04 +00:00
const { error } = await createInvite({
variables: {
2021-10-15 23:07:51 +00:00
gift: Number(gift), limit: limit ? Number(limit) : limit
2021-10-12 23:49:04 +00:00
}
})
if (error) {
throw new Error({ message: error.String() })
}
}}
>
<Input
label='gift'
name='gift'
append={<InputGroup.Text className='text-monospace'>sats</InputGroup.Text>}
required
autoFocus
/>
<Input
label={<>invitee limit <small className='text-muted ml-2'>optional</small></>}
name='limit'
/>
2021-10-14 21:05:37 +00:00
<SubmitButton variant='secondary'>create</SubmitButton>
2021-10-12 23:49:04 +00:00
</Form>
)
}
2021-10-14 21:05:37 +00:00
function Invite ({ invite, active }) {
const [revokeInvite] = useMutation(
gql`
${INVITE_FIELDS}
mutation revokeInvite($id: ID!) {
revokeInvite(id: $id) {
...InviteFields
}
}`
)
return (
<div
className={styles.invite}
>
<CopyInput
groupClassName='mb-1'
size='sm' type='text'
placeholder={`https://stacker.news/invites/${invite.id}`} readOnly
/>
<div className={styles.other}>
2021-10-15 23:07:51 +00:00
<span>{invite.gift} sat gift</span>
<span> \ </span>
2021-10-14 21:05:37 +00:00
<span>{invite.invitees.length} joined{invite.limit ? ` of ${invite.limit}` : ''}</span>
{active
? (
<>
<span> \ </span>
<span
className={styles.revoke}
onClick={() => revokeInvite({ variables: { id: invite.id } })}
>revoke
</span>
</>)
: invite.revoked && (
<>
<span> \ </span>
<span
className='text-danger'
>revoked
</span>
</>)}
</div>
</div>
)
}
function InviteList ({ name, invites }) {
return (
<div className='mt-4'>
<AccordianItem
show
headerColor='#212529'
header={<div style={{ fontWeight: 'bold', fontSize: '92%' }}>{name}</div>} body={
<div className={styles.invites}>{invites.map(invite => {
return <Invite invite={invite} key={invite.id} active={name === 'active'} />
})}
</div>
2021-10-15 17:56:54 +00:00
}
2021-10-14 21:05:37 +00:00
/>
</div>
)
}
2021-10-12 23:49:04 +00:00
export default function Invites () {
const { data } = useQuery(
gql`
${INVITE_FIELDS}
{
invites {
...InviteFields
}
}
`)
2021-10-14 21:05:37 +00:00
const [active, inactive] = data && data.invites
? data.invites.reduce((result, invite) => {
2021-10-15 17:56:54 +00:00
result[
invite.revoked || (invite.limit &&
invite.invitees.length >= invite.limit)
? 1
: 0].push(invite)
2021-10-14 21:05:37 +00:00
return result
},
[[], []])
: [[], []]
2021-10-12 23:49:04 +00:00
return (
<Layout>
2021-10-15 17:56:54 +00:00
<h2 className='text-center mt-3'>invite links</h2>
2021-10-12 23:49:04 +00:00
<InviteForm />
2021-10-14 21:05:37 +00:00
{active.length > 0 && <InviteList name='active' invites={active} />}
{inactive.length > 0 && <InviteList name='inactive' invites={inactive} />}
2021-10-12 23:49:04 +00:00
</Layout>
)
}