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 | 13x 13x 13x 13x 8x 13x 13x 13x 13x 13x 2x 13x 3x 3x 3x 3x 3x 3x 1x 2x 2x 1x 1x 115x 13x 13x 3x 3x 3x 2x 13x 3x 3x 16x 24x 39x 2x 115x 17x | // Ending a tenancy (Design canvas: Päätä vuokrasuhde, Päätä määräaikainen
// vuokrasuhde): who gave notice or on what ground, the date, and the last
// day, which follows the earliest legal date until edited, with the hint
// for each case (openspec: tenancies, "Ending a tenancy", "Ending a
// fixed-term tenancy early"; CONCEPT.md §4.2).
import Box from '@mui/material/Box'
import Button from '@mui/material/Button'
import IconButton from '@mui/material/IconButton'
import Stack from '@mui/material/Stack'
import Typography from '@mui/material/Typography'
import { useForm, useSelector } from '@tanstack/react-form'
import { useRef, useState } from 'react'
import { ReasonAlert } from '#/components/ReasonAlert'
import { TextField } from '#/components/form/TextField'
import { typedDate } from '#/features/ownership/schemas'
import { clientLog } from '#/lib/client-log'
import { parseFiDate } from '#/lib/dates'
import { formatDate, formatMoney } from '#/lib/format'
import { failureReason } from '#/lib/network-error'
import type { AlertReason, FieldReason } from '#/lib/reasons'
import { m } from '#/paraglide/messages.js'
import type { NoticeGiver } from '#/server/db/schema'
import { colors, radii } from '#/theme/tokens'
import { endTenancy } from './functions'
import {
EndingHint,
EndingWayPicker,
hintFor,
LAST_DAY_REASONS,
NOTICE_REASONS,
noticeDateLabel,
suggestedLastDay,
waysFor,
} from './ending'
import type { Tenancy } from './tenancy.repository.server'
interface Props {
propertyId: string
address: string
tenancy: Tenancy
today: string
onDone: () => Promise<void>
onClose: () => void
}
function CloseIcon() {
return (
<svg
width="22"
height="22"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2.6"
strokeLinecap="round"
aria-hidden="true"
>
<path d="M6 6 L18 18 M18 6 L6 18" />
</svg>
)
}
export function EndTenancyForm(props: Props) {
const { tenancy, today } = props
const fixed = tenancy.contractType === 'fixed_term'
const [by, setBy] = useState<NoticeGiver>(
() => waysFor(tenancy.contractType)[0],
)
const [alert, setAlert] = useState<AlertReason>()
const [noticeReason, setNoticeReason] = useState<FieldReason>()
const [lastDayReason, setLastDayReason] = useState<FieldReason>()
const lastDayEditedRef = useRef(false)
const suggested = (way: NoticeGiver, notice: string) =>
suggestedLastDay(way, tenancy, parseFiDate(notice))
const form = useForm({
defaultValues: {
noticeDate: formatDate(today),
lastDay: suggestedLastDay(by, tenancy, today),
},
onSubmit: async ({ value }) => {
setAlert(undefined)
setNoticeReason(undefined)
setLastDayReason(undefined)
try {
const result = await endTenancy({
data: {
propertyId: props.propertyId,
tenancyId: tenancy.id,
noticeGivenBy: by,
noticeDate: value.noticeDate,
lastDay: value.lastDay,
},
})
if (result.ok) {
await props.onDone()
} else Iif (NOTICE_REASONS.includes(result.reason as FieldReason)) {
setNoticeReason(result.reason as FieldReason)
} else if (LAST_DAY_REASONS.includes(result.reason as FieldReason)) {
setLastDayReason(result.reason as FieldReason)
} else {
setAlert(result.reason as AlertReason)
}
} catch (error) {
clientLog.error('tenancy.end_failed', {}, error)
setAlert(failureReason(error))
}
},
})
const noticeText = useSelector(form.store, (s) => s.values.noticeDate)
const notice = parseFiDate(noticeText)
const choose = (way: NoticeGiver) => {
setBy(way)
setLastDayReason(undefined)
if (!lastDayEditedRef.current && !fixed) {
form.setFieldValue('lastDay', suggested(way, noticeText))
}
}
return (
<Stack
component="form"
noValidate
aria-labelledby="end-form-title"
onSubmit={(e) => {
e.preventDefault()
void form.handleSubmit()
}}
sx={{ minHeight: '100dvh' }}
>
<Box
component="header"
sx={{
display: 'grid',
gridTemplateColumns: '44px 1fr 44px',
alignItems: 'center',
px: 1.5,
pt: 2,
pb: 0.5,
}}
>
<IconButton aria-label={m.amounts_form_close()} onClick={props.onClose}>
<CloseIcon />
</IconButton>
<Stack sx={{ alignItems: 'center', minWidth: 0 }}>
<Typography
id="end-form-title"
variant="h3"
component="h1"
sx={{ fontSize: 18, textAlign: 'center' }}
>
{m.tenancy_end_title()}
</Typography>
<Typography variant="body2" color="text.secondary" noWrap>
{props.address}
</Typography>
</Stack>
</Box>
<Stack
component="main"
sx={{ flex: 1, gap: 2, px: 2.5, pt: 1.25, pb: 2.5 }}
>
<Typography sx={{ fontSize: 16, fontWeight: 900 }}>
{tenancy.tenantName}
</Typography>
{fixed && tenancy.termEnd !== null ? (
<Box
sx={{
bgcolor: colors.yellowTint,
borderRadius: `${String(radii.input)}px`,
px: 1.75,
py: 1.5,
fontSize: 13,
fontWeight: 700,
lineHeight: 1.45,
}}
>
{m.tenancy_end_fixed_info({ date: formatDate(tenancy.termEnd) })}
</Box>
) : null}
<EndingWayPicker
id="end-way-label"
contractType={tenancy.contractType}
value={by}
onChange={choose}
/>
<Box
sx={{
display: 'grid',
gridTemplateColumns: 'repeat(2, minmax(0, 1fr))',
gap: 1.25,
}}
>
<form.Field
name="noticeDate"
validators={{ onChange: typedDate }}
listeners={{
onChange: ({ value }) => {
setNoticeReason(undefined)
if (!lastDayEditedRef.current && !fixed) {
form.setFieldValue('lastDay', suggested(by, value))
}
},
}}
>
{(field) => (
<TextField
field={field}
reason={noticeReason}
label={noticeDateLabel(by)}
placeholder={m.date_placeholder()}
/>
)}
</form.Field>
<form.Field
name="lastDay"
validators={{ onChange: typedDate }}
listeners={{
onChange: () => {
setLastDayReason(undefined)
},
}}
>
{(field) => (
<TextField
field={field}
reason={lastDayReason}
label={m.tenancy_last_day()}
placeholder={m.date_placeholder()}
onFocus={() => {
lastDayEditedRef.current = true
}}
/>
)}
</form.Field>
</Box>
<EndingHint text={hintFor(by, tenancy, notice)} />
<Typography variant="body2" color="text.secondary">
{m.tenancy_end_info()}
</Typography>
{tenancy.deposit != null && tenancy.deposit > 0 ? (
<Typography variant="body2" sx={{ fontWeight: 800 }}>
{m.tenancy_end_deposit({ amount: formatMoney(tenancy.deposit) })}
</Typography>
) : null}
{alert === undefined ? null : <ReasonAlert reason={alert} />}
</Stack>
<Box
component="footer"
sx={{
px: 2.5,
pt: 1.5,
pb: 3.5,
borderTop: `1px solid ${colors.cardBorder}`,
}}
>
<form.Subscribe selector={(s) => s.isSubmitting}>
{(submitting) => (
<Button
type="submit"
fullWidth
loading={submitting}
loadingPosition="start"
sx={{ minHeight: 56 }}
>
{m.tenancy_end_save()}
</Button>
)}
</form.Subscribe>
</Box>
</Stack>
)
}
|