import { Alert } from 'react-bootstrap'
import { useMe } from './me'
import FeeButton, { FeeButtonProvider } from './fee-button'
import { TERRITORY_BILLING_OPTIONS, TERRITORY_GRACE_DAYS } from '../lib/constants'
import { Form } from './form'
import { datePivot, timeSince } from '../lib/time'
import { LongCountdown } from './countdown'
import { useCallback } from 'react'
import { useApolloClient, useMutation } from '@apollo/client'
import { SUB_PAY } from '../fragments/subs'
const billingDueDate = (sub, grace) => {
if (!sub || sub.billingType === 'ONCE') return null
const pivot = sub.billingType === 'MONTHLY'
? { months: 1 }
: { years: 1 }
pivot.days = grace ? TERRITORY_GRACE_DAYS : 0
return datePivot(new Date(sub.billedLastAt), pivot)
}
export default function TerritoryPaymentDue ({ sub }) {
const me = useMe()
const client = useApolloClient()
const [paySub] = useMutation(SUB_PAY)
const onSubmit = useCallback(
async ({ ...variables }) => {
const { error } = await paySub({
variables
})
if (error) {
throw new Error({ message: error.toString() })
}
}, [client, paySub])
if (!sub || sub.userId !== Number(me?.id) || sub.status === 'ACTIVE') return null
const dueDate = billingDueDate(sub, true)
if (!dueDate) return null
return (