All files / src/features/tenancies TenancyRentForm.tsx

89.18% Statements 33/37
67.74% Branches 21/31
100% Functions 12/12
88.88% Lines 32/36

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                                                                                              2x                 43x                                 43x     43x 43x 43x 43x 43x   43x     2x 2x 2x 2x 2x               2x 1x 1x   1x 1x                     43x 170x     170x   43x       43x           2x 2x                                                                                                                             9x         46x                                                                       30x         45x                                           170x   46x                                      
// Changing a tenancy's rent (Design canvas: Muuta vuokraa ยท vuokrasuhteelle;
// openspec: tenancies, "Changing a tenancy's rent"): the current rent, the
// new one with the difference per month and its effect on the year, and the
// date, the first of a month or the tenancy's start day.
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 { useState } from 'react'
import { ReasonAlert } from '#/components/ReasonAlert'
import { TextField } from '#/components/form/TextField'
import { typedDate } from '#/features/ownership/schemas'
import { effectText } from '#/features/recurring-amounts/AmountForm'
import {
  periodOn,
  yearEffect,
  type Period,
} from '#/features/recurring-amounts/periods'
import { typedAmount } from '#/features/recurring-amounts/schemas'
import { clientLog } from '#/lib/client-log'
import { parseFiDate } from '#/lib/dates'
import { parseDecimal } from '#/lib/decimal'
import {
  formatMoney,
  formatMoneyChange,
  formatPercentChange,
} from '#/lib/format'
import { failureReason } from '#/lib/network-error'
import type { AlertReason, FieldReason } from '#/lib/reasons'
import { m } from '#/paraglide/messages.js'
import { colors, radii } from '#/theme/tokens'
import { setTenancyRent } from './functions'
import type { Tenancy } from './tenancy.repository.server'
 
interface Props {
  propertyId: string
  address: string
  tenancy: Tenancy
  /** The tenancy's rent periods, oldest first. */
  rents: readonly Period<number>[]
  today: string
  onDone: () => Promise<void>
  onClose: () => void
}
 
const DATE_REASONS: readonly string[] = [
  'date_invalid',
  'amount_date_not_month_start',
  'amount_date_too_early',
  'rent_date_before_tenancy',
  'rent_date_after_tenancy',
]
 
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 TenancyRentForm(props: Props) {
  const { tenancy, rents } = props
  // The rent the change is compared with: today's, or the first of a
  // tenancy that starts later.
  const day = props.today < tenancy.validFrom ? tenancy.validFrom : props.today
  const current = periodOn(rents, day) ?? rents.at(-1)
  const [alert, setAlert] = useState<AlertReason>()
  const [amountReason, setAmountReason] = useState<FieldReason>()
  const [dateReason, setDateReason] = useState<FieldReason>()
 
  const form = useForm({
    defaultValues: { amount: '', from: '' },
    onSubmit: async ({ value }) => {
      setAlert(undefined)
      setAmountReason(undefined)
      setDateReason(undefined)
      try {
        const result = await setTenancyRent({
          data: {
            propertyId: props.propertyId,
            tenancyId: tenancy.id,
            amount: value.amount,
            from: value.from,
          },
        })
        if (result.ok) {
          await props.onDone()
        } else Iif (result.reason === 'amount_invalid') {
          setAmountReason(result.reason)
        } else if (DATE_REASONS.includes(result.reason)) {
          setDateReason(result.reason as FieldReason)
        } else E{
          setAlert(result.reason as AlertReason)
        }
      } catch (error) {
        clientLog.error('tenancy.rent_save_failed', {}, error)
        setAlert(failureReason(error))
      }
    },
  })
 
  const newCents = parseDecimal(
    useSelector(form.store, (s) => s.values.amount),
    2,
  )
  const from = parseFiDate(useSelector(form.store, (s) => s.values.from))
  const effect =
    current !== undefined && newCents !== undefined && from !== undefined
      ? yearEffect(current.value, newCents, `${from.slice(0, 7)}-01`)
      : undefined
 
  return (
    <Stack
      component="form"
      noValidate
      aria-labelledby="rent-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="rent-form-title"
            variant="h3"
            component="h1"
            sx={{ fontSize: 18, textAlign: 'center' }}
          >
            {current ? m.amounts_form_change_rent() : m.amounts_form_set_rent()}
          </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 }}
      >
        {current ? (
          <Box
            sx={{
              bgcolor: 'background.paper',
              border: `1px solid ${colors.cardBorder}`,
              borderRadius: `${String(radii.input)}px`,
              px: 1.75,
              py: 1.5,
            }}
          >
            <Typography
              variant="body2"
              sx={{ fontWeight: 800 }}
              color="text.secondary"
            >
              {m.amounts_form_current()}
            </Typography>
            <Typography sx={{ fontSize: 22, fontWeight: 900 }}>
              {m.amounts_per_month({ amount: formatMoney(current.value) })}
            </Typography>
          </Box>
        ) : null}
        <form.Field
          name="amount"
          validators={{ onChange: typedAmount }}
          listeners={{
            onChange: () => {
              setAmountReason(undefined)
            },
          }}
        >
          {(field) => (
            <TextField
              field={field}
              reason={amountReason}
              label={current ? m.amounts_form_new() : m.amounts_form_amount()}
              slotProps={{ htmlInput: { inputMode: 'decimal' } }}
            />
          )}
        </form.Field>
        {effect ? (
          <Box
            role="status"
            sx={{
              bgcolor: colors.yellowTint,
              borderRadius: `${String(radii.input)}px`,
              px: 1.75,
              py: 1.5,
            }}
          >
            <Typography sx={{ fontWeight: 900 }}>
              {m.amounts_per_month({
                amount: formatMoneyChange(effect.difference),
              })}
              {effect.percent === undefined
                ? ''
                : ` (${formatPercentChange(effect.percent)})`}
            </Typography>
            <Typography variant="body2" sx={{ fontWeight: 700 }}>
              {effectText('rental_income', effect)}
            </Typography>
          </Box>
        ) : null}
        <form.Field
          name="from"
          validators={{ onChange: typedDate }}
          listeners={{
            onChange: () => {
              setDateReason(undefined)
            },
          }}
        >
          {(field) => (
            <TextField
              field={field}
              reason={dateReason}
              label={m.amounts_form_from()}
              placeholder={m.date_placeholder()}
            />
          )}
        </form.Field>
        <Typography variant="body2" color="text.secondary" sx={{ mt: -1 }}>
          {m.amounts_form_hint_rent()} {m.tenancy_rent_form_hint()}
        </Typography>
        {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 }}
            >
              {submitting
                ? m.amounts_form_saving()
                : current
                  ? m.amounts_form_save_change()
                  : m.amounts_form_save_first()}
            </Button>
          )}
        </form.Subscribe>
      </Box>
    </Stack>
  )
}