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

7.69% Statements 2/26
0% Branches 0/16
0% Functions 0/10
7.69% Lines 2/26

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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148                                      1x   1x                                                                                                                                                                                                                                                            
import Button from '@mui/material/Button'
import Stack from '@mui/material/Stack'
import Typography from '@mui/material/Typography'
import {
  createFileRoute,
  createLink,
  notFound,
  redirect,
  useRouter,
} from '@tanstack/react-router'
import { useState } from 'react'
import { NotFoundView } from '#/components/NotFoundView'
import { getTenancy } from '#/features/tenancies/functions'
import { depositStatus } from '#/features/deposits/deposit'
import { RemoveTenancyDialog } from '#/features/tenancies/RemoveTenancyDialog'
import { TenancyForm } from '#/features/tenancies/TenancyForm'
import { formatDate, formatMoney } from '#/lib/format'
import { m } from '#/paraglide/messages.js'
 
const ButtonLink = createLink(Button)
 
export const Route = createFileRoute(
  '/_authed/properties/$propertyId/tenancy/$tenancyId/edit',
)({
  loader: async ({ params }) => {
    const result = await getTenancy({
      data: { propertyId: params.propertyId, tenancyId: params.tenancyId },
    })
    if (!result.ok) {
      // 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.tenancy_form_page_title() }] }),
  component: EditTenancyRoute,
  notFoundComponent: () => (
    <NotFoundView
      title={m.reason_tenancy_not_found_title()}
      text={m.reason_tenancy_not_found()}
      backLabel={m.property_back_to_list()}
    />
  ),
})
 
function EditTenancyRoute() {
  const { tenancy, property, today, rents, depositReturn } =
    Route.useLoaderData()
  const { propertyId } = Route.useParams()
  const router = useRouter()
  const toPage = () =>
    router.navigate({
      to: '/properties/$propertyId/tenancy',
      params: { propertyId },
    })
  const [removing, setRemoving] = useState(false)
  // An ended tenancy with a deposit: the return's status and its page.
  const state = depositStatus(tenancy, depositReturn, today)
  const statusText =
    state.kind === 'returned'
      ? state.kept > 0
        ? m.deposit_status_returned_kept({
            date: formatDate(state.on),
            amount: formatMoney(state.returned),
            kept: formatMoney(state.kept),
          })
        : m.deposit_status_returned({
            date: formatDate(state.on),
            amount: formatMoney(state.returned),
          })
      : m.deposit_status_pending()
  return (
    <>
      <TenancyForm
        propertyId={propertyId}
        address={property.streetAddress}
        editing={tenancy}
        startRent={rents.at(0)?.value}
        onSaved={async () => {
          await router.invalidate()
          await toPage()
        }}
        onClose={() => {
          void toPage()
        }}
        onRemove={
          // Only before its start day (openspec: tenancies, "Removing a
          // tenancy that hasn't started").
          tenancy.validFrom > today
            ? () => {
                setRemoving(true)
              }
            : undefined
        }
        depositReturn={
          state.kind === 'returned' || state.kind === 'pending' ? (
            <Stack sx={{ gap: 0.75 }}>
              <Typography variant="body2" sx={{ fontWeight: 800 }}>
                {statusText}
              </Typography>
              <ButtonLink
                to="/properties/$propertyId/tenancy/$tenancyId/deposit"
                params={{ propertyId, tenancyId: tenancy.id }}
                variant="outlined"
                sx={{ alignSelf: 'flex-start', minHeight: 44, px: 2 }}
              >
                {m.deposit_form_return_open()}
              </ButtonLink>
            </Stack>
          ) : undefined
        }
        rentChange={
          // Every tenancy's rent, not only the current one's (openspec:
          // tenancies, "Correcting a tenancy").
          <ButtonLink
            to="/properties/$propertyId/tenancy/$tenancyId/rent"
            params={{ propertyId, tenancyId: tenancy.id }}
            variant="outlined"
            sx={{ alignSelf: 'flex-start', minHeight: 44, px: 2 }}
          >
            {m.tenancy_form_change_rent()}
          </ButtonLink>
        }
      />
      {removing ? (
        <RemoveTenancyDialog
          propertyId={propertyId}
          tenancy={tenancy}
          open
          onClose={() => {
            setRemoving(false)
          }}
          onDone={async () => {
            await router.invalidate()
            await toPage()
          }}
        />
      ) : null}
    </>
  )
}