import { Col, InputGroup, Row } from 'react-bootstrap' import { Checkbox, CheckboxGroup, Form, Input, MarkdownInput } from './form' import FeeButton, { FeeButtonProvider } from './fee-button' import { gql, useApolloClient, useMutation } from '@apollo/client' import { useCallback, useState } from 'react' import { useRouter } from 'next/router' import { MAX_TERRITORY_DESC_LENGTH, POST_TYPES, TERRITORY_BILLING_OPTIONS } from '../lib/constants' import { territorySchema } from '../lib/validate' import { useMe } from './me' export default function TerritoryForm ({ sub }) { const router = useRouter() const client = useApolloClient() const me = useMe() const [upsertSub] = useMutation( gql` mutation upsertSub($name: String!, $desc: String, $baseCost: Int!, $postTypes: [String!]!, $allowFreebies: Boolean!, $billingType: String!, $billingAutoRenew: Boolean!, $hash: String, $hmac: String) { upsertSub(name: $name, desc: $desc, baseCost: $baseCost, postTypes: $postTypes, allowFreebies: $allowFreebies, billingType: $billingType, billingAutoRenew: $billingAutoRenew, hash: $hash, hmac: $hmac) { name } }` ) const onSubmit = useCallback( async ({ ...variables }) => { const { error } = await upsertSub({ variables }) if (error) { throw new Error({ message: error.toString() }) } // modify graphql cache to include new sub client.cache.modify({ fields: { subs (existing = []) { console.log('existing', existing, variables.name) return [ ...existing, { __typename: 'Sub', name: variables.name }] } } }) await router.push(`/~${variables.name}`) }, [client, upsertSub, router] ) const [billing, setBilling] = useState((sub?.billingType || 'MONTHLY').toLowerCase()) return (
{sub?.name ? name read only} name='name' readOnly prepend={~} className='text-muted' /> : ~} />} sats} /> {(!sub?.billingType || sub.billingType === 'MONTHLY') && checked && setBilling('monthly')} groupClassName='ms-1 mb-0' />} {(!sub?.billingType || sub.billingType === 'YEARLY') && checked && setBilling('yearly')} groupClassName='ms-1 mb-0' />} {(!sub?.billingType || sub.billingType === 'ONCE') && checked && setBilling('once')} groupClassName='ms-1 mb-0' />} {billing !== 'once' && }
) }