All files / src/features/year-summary HolderChoice.tsx

85.71% Statements 12/14
100% Branches 8/8
71.42% Functions 5/7
85.71% Lines 12/14

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                                                    3x     16x 16x   16x         16x           23x                     16x       5x                                                                                                                                           2x 2x 2x       55x                                                                            
// "Kenen luvut": a button under the year that opens a bottom sheet with the
// people whose figures can be shown (openspec: year-summary, "Choosing whose
// figures"; Design canvas, section #122).
import Box from '@mui/material/Box'
import ButtonBase from '@mui/material/ButtonBase'
import Button from '@mui/material/Button'
import Drawer from '@mui/material/Drawer'
import FormControlLabel from '@mui/material/FormControlLabel'
import Radio from '@mui/material/Radio'
import RadioGroup from '@mui/material/RadioGroup'
import Stack from '@mui/material/Stack'
import Typography from '@mui/material/Typography'
import { useState } from 'react'
import { m } from '#/paraglide/messages.js'
import { colors, radii } from '#/theme/tokens'
import { personParam } from './holder'
import type { YearSummaryView } from './view'
 
interface Props {
  holder: YearSummaryView['holder']
  people: YearSummaryView['people']
  counts: YearSummaryView['counts']
  /** The chosen `person` parameter; undefined for the own share. */
  onChoose: (person: string | undefined) => void
}
 
const OWN = 'own'
 
export function HolderChoice(props: Props) {
  const [open, setOpen] = useState(false)
  const chosen = personParam(props.holder) ?? OWN
  const current =
    props.holder.kind === 'own'
      ? m.year_summary_holder_own()
      : props.holder.kind === 'whole'
        ? m.year_summary_holder_whole()
        : props.holder.name
  const options = [
    {
      value: OWN,
      label: m.year_summary_holder_own(),
      flats: props.counts.own,
    },
    ...props.people.map((person) => ({
      value: person.userId,
      label: person.name,
      flats: person.flats,
    })),
    {
      value: 'whole',
      label: m.year_summary_holder_whole(),
      flats: props.counts.whole,
    },
  ]
  return (
    <>
      <ButtonBase
        onClick={() => {
          setOpen(true)
        }}
        aria-haspopup="dialog"
        sx={{
          display: 'flex',
          justifyContent: 'space-between',
          alignItems: 'center',
          gap: 1,
          width: '100%',
          minHeight: 48,
          bgcolor: 'background.paper',
          border: `1px solid ${colors.cardBorder}`,
          borderRadius: `${String(radii.tile)}px`,
          px: 2,
          py: 1,
          fontFamily: 'inherit',
          textAlign: 'left',
        }}
      >
        <Stack>
          <Typography
            component="span"
            sx={{ fontSize: 12, fontWeight: 800, color: 'text.secondary' }}
          >
            {m.year_summary_holder_label()}
          </Typography>
          <Typography component="span" sx={{ fontSize: 15, fontWeight: 900 }}>
            {current}
          </Typography>
        </Stack>
        <Box sx={{ display: 'flex', color: 'text.secondary' }}>
          <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="M6 9 L12 15 L18 9" />
          </svg>
        </Box>
      </ButtonBase>
      <Drawer
        anchor="bottom"
        open={open}
        onClose={() => {
          setOpen(false)
        }}
        slotProps={{
          paper: {
            'aria-labelledby': 'holder-choice-title',
            role: 'dialog',
          },
        }}
      >
        <Stack sx={{ px: 2.5, pt: 2.5, pb: 3, gap: 1.5 }}>
          <Typography
            id="holder-choice-title"
            component="h2"
            sx={{ fontSize: 19, fontWeight: 900 }}
          >
            {m.year_summary_holder_sheet_title()}
          </Typography>
          <RadioGroup
            value={chosen}
            onChange={(event) => {
              const value = event.target.value
              setOpen(false)
              props.onChoose(value === OWN ? undefined : value)
            }}
          >
            {options.map((option) => (
              <FormControlLabel
                key={option.value}
                value={option.value}
                control={<Radio />}
                label={
                  <Stack>
                    <Typography component="span" sx={{ fontWeight: 900 }}>
                      {option.label}
                    </Typography>
                    <Typography
                      component="span"
                      sx={{
                        fontSize: 12,
                        fontWeight: 800,
                        color: 'text.secondary',
                      }}
                    >
                      {m.year_summary_holder_flats({ count: option.flats })}
                    </Typography>
                  </Stack>
                }
                sx={{ minHeight: 56, m: 0 }}
              />
            ))}
          </RadioGroup>
          <Button
            variant="outlined"
            onClick={() => {
              setOpen(false)
            }}
          >
            {m.year_summary_holder_sheet_close()}
          </Button>
        </Stack>
      </Drawer>
    </>
  )
}