All files / src/routes/_authed/properties/$propertyId/tenancy/$tenancyId/fees $feeId.tsx

3.12% Statements 1/32
0% Branches 0/14
0% Functions 0/13
3.33% Lines 1/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120                              1x                                                                                                                                                                                                                
import {
  createFileRoute,
  notFound,
  redirect,
  useRouter,
} from '@tanstack/react-router'
import { useState } from 'react'
import { NotFoundView } from '#/components/NotFoundView'
import { UndoDialog } from '#/features/recurring-amounts/UndoDialog'
import { getTenancy, undoTenantFee } from '#/features/tenancies/functions'
import { TenantFeeForm } from '#/features/tenancies/TenantFeeForm'
import { chargeName } from '#/lib/charge-labels'
import { formatDate, formatMoney } from '#/lib/format'
import { m } from '#/paraglide/messages.js'
 
export const Route = createFileRoute(
  '/_authed/properties/$propertyId/tenancy/$tenancyId/fees/$feeId',
)({
  loader: async ({ params }) => {
    const result = await getTenancy({
      data: { propertyId: params.propertyId, tenancyId: params.tenancyId },
    })
    if (!result.ok || !result.fees.some((fee) => fee.id === params.feeId)) {
      // eslint-disable-next-line @typescript-eslint/only-throw-error -- TanStack Router's not-found signal
      throw notFound()
    }
    if (result.role !== 'owner') {
      throw redirect({
        to: '/properties/$propertyId/tenancy',
        params: { propertyId: params.propertyId },
      })
    }
    return result
  },
  head: () => ({
    meta: [
      {
        title: m.amounts_form_page_title({ title: m.tenant_fee_form_title() }),
      },
    ],
  }),
  component: TenantFeeRoute,
  notFoundComponent: () => (
    <NotFoundView
      title={m.reason_tenant_fee_not_found_title()}
      text={m.reason_tenant_fee_not_found()}
      backLabel={m.property_back_to_list()}
    />
  ),
})
 
function TenantFeeRoute() {
  const { tenancy, rents, occupants, fees, property } = Route.useLoaderData()
  const { propertyId, feeId } = Route.useParams()
  const router = useRouter()
  const [undoing, setUndoing] = useState(false)
  const fee = fees.find((f) => f.id === feeId)
  const toPage = () =>
    router.navigate({
      to: '/properties/$propertyId/tenancy',
      params: { propertyId },
    })
  if (!fee) {
    return null
  }
  const latest = fee.amounts.at(-1)
  const previous = fee.amounts.at(-2)
  return (
    <>
      <TenantFeeForm
        propertyId={propertyId}
        address={property.streetAddress}
        tenancy={tenancy}
        rents={rents}
        occupants={occupants}
        fees={fees}
        fee={fee}
        onUndo={() => {
          setUndoing(true)
        }}
        onDone={async () => {
          await router.invalidate()
          await toPage()
        }}
        onClose={() => {
          void toPage()
        }}
      />
      {undoing && latest ? (
        <UndoDialog
          open
          onClose={() => {
            setUndoing(false)
          }}
          title={m.amounts_undo_title({ date: formatDate(latest.validFrom) })}
          text={
            previous
              ? m.amounts_undo_text({
                  amount: formatMoney(previous.value),
                  date: formatDate(previous.validFrom),
                })
              : m.tenant_fee_undo_remove({ name: chargeName(fee) })
          }
          undo={() =>
            undoTenantFee({
              data: { propertyId, tenancyId: tenancy.id, feeId },
            })
          }
          failureEvent="tenancy.fee_undo_failed"
          onDone={async () => {
            setUndoing(false)
            await router.invalidate()
            await toPage()
          }}
        />
      ) : null}
    </>
  )
}