All files / src/features/tenancies TenancyCard.tsx

100% Statements 10/10
69.76% Branches 30/43
100% Functions 3/3
100% Lines 10/10

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                                            2x                                 25x     25x         25x   25x 26x     25x       25x     2x           25x                                                                                                                                                                                                                
// The tenancy card on a flat's details page (Design canvas: Asunnon tiedot
// · vuokra vuokrasuhteella): the current tenant with the contract type,
// start and due day, and the rent with its next change when given; it opens
// the Tenancy page (openspec: properties, "The flat details page").
import Box from '@mui/material/Box'
import Link from '@mui/material/Link'
import Stack from '@mui/material/Stack'
import Typography from '@mui/material/Typography'
import { createLink } from '@tanstack/react-router'
import {
  monthOf,
  periodOn,
  type Period,
} from '#/features/recurring-amounts/periods'
import { formatDate, formatMoney } from '#/lib/format'
import { m } from '#/paraglide/messages.js'
import { colors, radii } from '#/theme/tokens'
import { feeInMonth, type FeeOfTenancy } from './fees'
import { contractShortLabel } from './labels'
import { initials } from './TenancyPage'
import type { Tenancy } from './tenancy.repository.server'
 
const CardLink = createLink(Link)
 
export function TenancyCard(props: {
  propertyId: string
  /** The current tenancy, or null when the flat is vacant. */
  tenancy: Tenancy | null
  /** Adds the due day, as the flat details page shows it. */
  showDueDay?: boolean
  /** The tenancy's rent periods, oldest first, to show the rent. */
  rents?: readonly Period<number>[]
  /** Today, for the rent in effect. */
  today?: string
  /** The tenancy's occupants periods, to show today's occupants. */
  occupants?: readonly Period<number>[]
  /** The tenancy's fees, to show their sum this month. */
  fees?: readonly FeeOfTenancy[]
}) {
  const { tenancy, rents, today } = props
  // The rent today, or the first rent of a tenancy that starts later.
  const day =
    tenancy && today !== undefined
      ? today < tenancy.validFrom
        ? tenancy.validFrom
        : today
      : undefined
  const rent = rents && day !== undefined ? periodOn(rents, day) : undefined
  const next =
    rents && day !== undefined
      ? rents.find((p) => p.validFrom > day)
      : undefined
  const persons =
    props.occupants && day !== undefined
      ? periodOn(props.occupants, day)?.value
      : undefined
  const feesThisMonth =
    tenancy && props.fees && day !== undefined
      ? props.fees.reduce(
          (sum, fee) =>
            sum +
            (feeInMonth(fee, props.occupants ?? [], tenancy, monthOf(day)) ??
              0),
          0,
        )
      : 0
  return (
    <CardLink
      to="/properties/$propertyId/tenancy"
      params={{ propertyId: props.propertyId }}
      underline="none"
      color="inherit"
      sx={{
        display: 'flex',
        alignItems: 'center',
        gap: 1.5,
        bgcolor: 'background.paper',
        border: `2px solid ${colors.yellow}`,
        borderRadius: `${String(radii.card)}px`,
        px: 2,
        py: 1.75,
      }}
    >
      <Box
        aria-hidden="true"
        sx={{
          width: 44,
          height: 44,
          borderRadius: '50%',
          bgcolor: colors.lavenderTint,
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center',
          fontSize: 15,
          fontWeight: 900,
          flexShrink: 0,
        }}
      >
        {tenancy ? initials(tenancy.tenantName) : ''}
      </Box>
      <Stack sx={{ flex: 1, minWidth: 0 }}>
        <Typography
          variant="body2"
          sx={{ fontWeight: 800, color: 'text.secondary' }}
        >
          {m.tenancy_title()}
        </Typography>
        <Typography sx={{ fontSize: 16, fontWeight: 900 }}>
          {tenancy ? tenancy.tenantName : m.tenancy_card_vacant()}
        </Typography>
        {tenancy ? (
          <Typography variant="body2" color="text.secondary">
            {props.showDueDay
              ? m.tenancy_card_details_due({
                  contract: contractShortLabel[tenancy.contractType](),
                  date: formatDate(tenancy.validFrom),
                  day: String(tenancy.dueDay),
                })
              : m.tenancy_card_details({
                  contract: contractShortLabel[tenancy.contractType](),
                  date: formatDate(tenancy.validFrom),
                })}
            {persons === undefined
              ? ''
              : ` · ${m.occupants_count({ persons })}`}
          </Typography>
        ) : null}
      </Stack>
      {rent ? (
        <Stack sx={{ alignItems: 'flex-end', flexShrink: 0 }}>
          <Typography sx={{ fontSize: 16, fontWeight: 900 }}>
            {m.amounts_per_month({ amount: formatMoney(rent.value) })}
          </Typography>
          {next ? (
            <Typography
              variant="body2"
              sx={{ fontSize: 11, fontWeight: 800, color: 'text.secondary' }}
            >
              {m.amounts_upcoming({
                date: formatDate(next.validFrom),
                amount: formatMoney(next.value),
              })}
            </Typography>
          ) : null}
          {feesThisMonth > 0 ? (
            <Typography
              variant="body2"
              sx={{ fontSize: 12, fontWeight: 800, color: 'text.secondary' }}
            >
              {m.tenancy_card_fees({ amount: formatMoney(feesThisMonth) })}
            </Typography>
          ) : null}
        </Stack>
      ) : null}
      <svg
        width="20"
        height="20"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        strokeWidth="2.4"
        strokeLinecap="round"
        strokeLinejoin="round"
        aria-hidden="true"
      >
        <path d="M9 5 L16 12 L9 19" />
      </svg>
    </CardLink>
  )
}