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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | 1x 1x 1x | import Button from '@mui/material/Button'
import Link from '@mui/material/Link'
import Stack from '@mui/material/Stack'
import {
createFileRoute,
createLink,
notFound,
useRouter,
} from '@tanstack/react-router'
import { useState } from 'react'
import { NotFoundView } from '#/components/NotFoundView'
import { UndoDialog } from '#/features/recurring-amounts/UndoDialog'
import {
getTenancies,
undoTenancyOccupants,
undoTenancyRent,
} from '#/features/tenancies/functions'
import { OccupantsDialog } from '#/features/tenancies/OccupantsDialog'
import { CancelEndingDialog } from '#/features/tenancies/CancelEndingDialog'
import { tenancyStatus } from '#/features/tenancies/status'
import type { Tenancy } from '#/features/tenancies/tenancy.repository.server'
import { TenancyPage } from '#/features/tenancies/TenancyPage'
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/',
)({
loader: async ({ params }) => {
const view = await getTenancies({ data: { propertyId: params.propertyId } })
if (!view.ok) {
// eslint-disable-next-line @typescript-eslint/only-throw-error -- TanStack Router's not-found signal
throw notFound()
}
return view
},
head: () => ({ meta: [{ title: m.tenancy_page_title() }] }),
component: TenancyRoute,
notFoundComponent: () => (
<NotFoundView
title={m.reason_property_not_found_title()}
text={m.reason_property_not_found()}
backLabel={m.property_back_to_list()}
/>
),
})
const ButtonLink = createLink(Button)
const TextLink = createLink(Link)
function TenancyRoute() {
const view = Route.useLoaderData()
const { propertyId } = Route.useParams()
const router = useRouter()
const [cancelling, setCancelling] = useState<Tenancy>()
const [undoingRent, setUndoingRent] = useState<Tenancy>()
const [changingOccupants, setChangingOccupants] = useState<Tenancy>()
const [undoingOccupants, setUndoingOccupants] = useState<Tenancy>()
const undoPeriods = undoingOccupants
? (view.occupants[undoingOccupants.id] ?? [])
: []
const latestOccupants = undoPeriods.at(-1)
const previousOccupants = undoPeriods.at(-2)
const undoRents = undoingRent ? (view.rents[undoingRent.id] ?? []) : []
const latestRent = undoRents.at(-1)
const previousRent = undoRents.at(-2)
return (
<>
<TenancyPage
view={view}
address={view.property.streetAddress}
onBack={() => {
void router.navigate({
to: '/properties/$propertyId',
params: { propertyId },
})
}}
addAction={
<ButtonLink
to="/properties/$propertyId/tenancy/new"
params={{ propertyId }}
sx={{ minHeight: 48, px: 2.5 }}
>
{m.tenancy_add()}
</ButtonLink>
}
otherRow={(tenancy, row) => (
<TextLink
to="/properties/$propertyId/tenancy/$tenancyId/edit"
params={{ propertyId, tenancyId: tenancy.id }}
underline="none"
color="inherit"
sx={{ display: 'block' }}
>
{row}
</TextLink>
)}
earlierAction={
<TextLink
to="/properties/$propertyId/tenancy/new"
params={{ propertyId }}
sx={{
alignSelf: 'center',
minHeight: 44,
display: 'inline-flex',
alignItems: 'center',
fontWeight: 800,
}}
>
{m.tenancy_add_earlier()}
</TextLink>
}
rentChange={(tenancy) => (
<ButtonLink
to="/properties/$propertyId/tenancy/$tenancyId/rent"
params={{ propertyId, tenancyId: tenancy.id }}
variant="outlined"
sx={{ minHeight: 44, px: 2, flexShrink: 0 }}
>
{m.amounts_change()}
</ButtonLink>
)}
onUndoRent={setUndoingRent}
feeAdd={(tenancy) => (
<ButtonLink
to="/properties/$propertyId/tenancy/$tenancyId/fees/new"
params={{ propertyId, tenancyId: tenancy.id }}
variant="outlined"
sx={{ minHeight: 44, px: 2, flexShrink: 0 }}
>
{m.tenancy_fee_add()}
</ButtonLink>
)}
feeChange={(tenancy, fee) => (
<ButtonLink
to="/properties/$propertyId/tenancy/$tenancyId/fees/$feeId"
params={{ propertyId, tenancyId: tenancy.id, feeId: fee.id }}
variant="outlined"
aria-label={m.tenancy_fee_change_label({ name: chargeName(fee) })}
sx={{ minHeight: 44, px: 2, flexShrink: 0 }}
>
{m.amounts_change()}
</ButtonLink>
)}
depositChange={(tenancy) => (
<ButtonLink
to="/properties/$propertyId/tenancy/$tenancyId/edit"
params={{ propertyId, tenancyId: tenancy.id }}
variant="outlined"
sx={{ minHeight: 44, px: 2, flexShrink: 0 }}
>
{tenancy.deposit != null && tenancy.deposit > 0
? m.amounts_change()
: m.deposit_card_add()}
</ButtonLink>
)}
returnAction={(tenancy) => (
<ButtonLink
to="/properties/$propertyId/tenancy/$tenancyId/deposit"
params={{ propertyId, tenancyId: tenancy.id }}
variant="outlined"
sx={{ alignSelf: 'flex-start', minHeight: 44, px: 2 }}
>
{m.deposit_pending_action()}
</ButtonLink>
)}
leaseLink={(lease, row) => (
<TextLink
to="/properties/$propertyId/documents/$documentId"
params={{ propertyId, documentId: lease.id }}
underline="none"
color="inherit"
sx={{ display: 'block' }}
>
{row}
</TextLink>
)}
leaseAdd={(tenancy) => (
<ButtonLink
to="/properties/$propertyId/documents/new"
params={{ propertyId }}
search={{ kind: 'lease', tenancyId: tenancy.id }}
variant="outlined"
sx={{ alignSelf: 'flex-start', minHeight: 44, px: 2 }}
>
{m.lease_card_add()}
</ButtonLink>
)}
onChangeOccupants={setChangingOccupants}
onUndoOccupants={setUndoingOccupants}
currentActions={(tenancy) => {
const status = tenancyStatus(tenancy, view.today)
return (
<Stack sx={{ gap: 1 }}>
<ButtonLink
to="/properties/$propertyId/tenancy/$tenancyId/edit"
params={{ propertyId, tenancyId: tenancy.id }}
variant="outlined"
aria-label={m.tenancy_edit_label()}
sx={{ minHeight: 48 }}
>
{m.tenancy_edit()}
</ButtonLink>
{/* A tenancy that hasn't started has no notice date that is
both on or after its start and not in the future. */}
{tenancy.noticeGivenBy === null &&
status.kind !== 'ended' &&
status.kind !== 'starts' ? (
<ButtonLink
to="/properties/$propertyId/tenancy/$tenancyId/end"
params={{ propertyId, tenancyId: tenancy.id }}
variant="outlined"
sx={{ minHeight: 48 }}
>
{m.tenancy_end_title()}
</ButtonLink>
) : null}
{/* A tenancy with a last day (a fixed term, or an ending
recorded) can be followed by the next one. */}
{tenancy.validTo !== null ? (
<ButtonLink
to="/properties/$propertyId/tenancy/new"
params={{ propertyId }}
sx={{ minHeight: 48 }}
>
{m.tenancy_add_next()}
</ButtonLink>
) : null}
{status.kind === 'notice_given' ? (
<>
<Button
variant="text"
color="inherit"
onClick={() => {
setCancelling(tenancy)
}}
sx={{ textDecoration: 'underline' }}
>
{m.tenancy_cancel()}
</Button>
</>
) : null}
</Stack>
)
}}
/>
{changingOccupants ? (
<OccupantsDialog
open
propertyId={propertyId}
tenancyId={changingOccupants.id}
onClose={() => {
setChangingOccupants(undefined)
}}
onDone={async () => {
setChangingOccupants(undefined)
await router.invalidate()
}}
/>
) : null}
{undoingOccupants && latestOccupants && previousOccupants ? (
<UndoDialog
open
onClose={() => {
setUndoingOccupants(undefined)
}}
title={m.tenancy_occupants_undo_title({
date: formatDate(latestOccupants.validFrom),
})}
text={m.tenancy_occupants_undo_text({
persons: String(previousOccupants.value),
date: formatDate(previousOccupants.validFrom),
})}
undo={() =>
undoTenancyOccupants({
data: { propertyId, tenancyId: undoingOccupants.id },
})
}
failureEvent="tenancy.occupants_undo_failed"
onDone={async () => {
setUndoingOccupants(undefined)
await router.invalidate()
}}
/>
) : null}
{undoingRent && latestRent && previousRent ? (
<UndoDialog
open
onClose={() => {
setUndoingRent(undefined)
}}
title={m.amounts_undo_title({
date: formatDate(latestRent.validFrom),
})}
text={m.amounts_undo_text({
amount: formatMoney(previousRent.value),
date: formatDate(previousRent.validFrom),
})}
undo={() =>
undoTenancyRent({
data: { propertyId, tenancyId: undoingRent.id },
})
}
failureEvent="tenancy.rent_undo_failed"
onDone={async () => {
setUndoingRent(undefined)
await router.invalidate()
}}
/>
) : null}
{cancelling ? (
<CancelEndingDialog
propertyId={propertyId}
tenancy={cancelling}
open
onClose={() => {
setCancelling(undefined)
}}
onDone={async () => {
setCancelling(undefined)
await router.invalidate()
}}
/>
) : null}
</>
)
}
|