import AccordianItem from './accordian-item' import { Col, InputGroup, Row, Form as BootstrapForm, Badge } from 'react-bootstrap' import { Checkbox, CheckboxGroup, Form, Input, MarkdownInput } from './form' import FeeButton, { FeeButtonProvider } from './fee-button' import { gql, useApolloClient, useLazyQuery, useMutation } from '@apollo/client' import { useCallback, useMemo, useState } from 'react' import { useRouter } from 'next/router' import { MAX_TERRITORY_DESC_LENGTH, POST_TYPES, TERRITORY_BILLING_OPTIONS, TERRITORY_PERIOD_COST } from '@/lib/constants' import { territorySchema } from '@/lib/validate' import { useMe } from './me' import Info from './info' import { abbrNum } from '@/lib/format' import { purchasedType } from '@/lib/territory' import { SUB } from '@/fragments/subs' export default function TerritoryForm ({ sub }) { const router = useRouter() const client = useApolloClient() const me = useMe() const [upsertSub] = useMutation( gql` mutation upsertSub($oldName: String, $name: String!, $desc: String, $baseCost: Int!, $postTypes: [String!]!, $allowFreebies: Boolean!, $billingType: String!, $billingAutoRenew: Boolean!, $moderated: Boolean!, $hash: String, $hmac: String, $nsfw: Boolean!) { upsertSub(oldName: $oldName, name: $name, desc: $desc, baseCost: $baseCost, postTypes: $postTypes, allowFreebies: $allowFreebies, billingType: $billingType, billingAutoRenew: $billingAutoRenew, moderated: $moderated, hash: $hash, hmac: $hmac, nsfw: $nsfw) { name } }` ) const [unarchiveTerritory] = useMutation( gql` mutation unarchiveTerritory($name: String!, $desc: String, $baseCost: Int!, $postTypes: [String!]!, $allowFreebies: Boolean!, $billingType: String!, $billingAutoRenew: Boolean!, $moderated: Boolean!, $hash: String, $hmac: String, $nsfw: Boolean!) { unarchiveTerritory(name: $name, desc: $desc, baseCost: $baseCost, postTypes: $postTypes, allowFreebies: $allowFreebies, billingType: $billingType, billingAutoRenew: $billingAutoRenew, moderated: $moderated, hash: $hash, hmac: $hmac, nsfw: $nsfw) { name } }` ) const schema = territorySchema({ client, me, sub }) const [fetchSub] = useLazyQuery(SUB) const [archived, setArchived] = useState(false) const onNameChange = useCallback(async (formik, e) => { // never show "territory archived" warning during edits if (sub) return const name = e.target.value const { data } = await fetchSub({ variables: { sub: name } }) setArchived(data?.sub?.status === 'STOPPED') }, [fetchSub, setArchived]) const onSubmit = useCallback( async ({ ...variables }) => { const { error } = archived ? await unarchiveTerritory({ variables }) : await upsertSub({ variables: { oldName: sub?.name, ...variables } }) if (error) { throw new Error({ message: error.toString() }) } // modify graphql cache to include new sub client.cache.modify({ fields: { subs (existing = []) { const filtered = existing.filter(s => s.name !== variables.name && s.name !== sub?.name) return [ ...filtered, { __typename: 'Sub', name: variables.name }] } } }) await router.push(`/~${variables.name}`) }, [client, upsertSub, unarchiveTerritory, router, archived] ) const [billing, setBilling] = useState((sub?.billingType || 'MONTHLY').toLowerCase()) const lineItems = useMemo(() => { const lines = { territory: TERRITORY_BILLING_OPTIONS('first')[billing] } if (!sub) return lines // we are changing billing type so prorate the change if (sub?.billingType?.toLowerCase() !== billing) { const alreadyBilled = TERRITORY_PERIOD_COST(purchasedType(sub)) lines.paid = { term: `- ${abbrNum(alreadyBilled)} sats`, label: 'already paid', modifier: cost => cost - alreadyBilled } return lines } }, [sub, billing]) return (
~} onChange={onNameChange} warn={archived && (
this territory is archived
  • This territory got archived because the previous founder did not pay for the upkeep
  • You can proceed but will inherit the old content
)} /> sats} /> {sub?.billingType !== 'ONCE' && <> billing {sub && sub.billingType !== 'ONCE' && You will be credited what you paid for your current billing period when you change your billing period to a longer duration. If you change from yearly to monthly, when your year ends, your monthly billing will begin. } } name='billing' groupClassName={billing !== 'once' ? 'mb-0' : ''} > checked && setBilling('monthly')} groupClassName='ms-1 mb-0' /> checked && setBilling('yearly')} groupClassName='ms-1 mb-0' /> checked && setBilling('once')} groupClassName='ms-1 mb-0' /> {billing !== 'once' && } } options} body={ <> moderation enable moderation
  1. Outlaw posts and comments with a click
  2. Your territory will get a moderated badge
} name='moderated' groupClassName='ms-1' /> nsfw mark as nsfw
  1. Let stackers know that your territory may contain explicit content
  2. Your territory will get a nsfw badge
} name='nsfw' groupClassName='ms-1' /> } />
) }