All files / src/routes/_authed/transactions new.tsx

9.37% Statements 3/32
0% Branches 0/21
0% Functions 0/12
10% Lines 3/30

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                                  1x                     1x                   1x                                                                                                                                                                                                                                                        
// The new entry form (openspec: transactions, "Logging an entry", "New entry
// from the bottom bar"). With `?propertyId` (from a flat's Entries page) the
// flat is fixed; without it the user picks one of the flats they own.
import Button from '@mui/material/Button'
import Stack from '@mui/material/Stack'
import Typography from '@mui/material/Typography'
import { createFileRoute, Link, useRouter } from '@tanstack/react-router'
import { useSyncExternalStore } from 'react'
import { z } from 'zod'
import { sortByStreetAddress } from '#/features/properties/property'
import { getNewTransactionOptions } from '#/features/transactions/functions'
import { TransactionForm } from '#/features/transactions/TransactionForm'
import { m } from '#/paraglide/messages.js'
import { getLocale } from '#/paraglide/runtime.js'
import { colors, radii } from '#/theme/tokens'
 
/** The flat last used for an entry on this device: a convenience only. */
const LAST_FLAT_KEY = 'kotivalo_last_entry_property'
 
function lastFlat(): string | undefined {
  try {
    return localStorage.getItem(LAST_FLAT_KEY) ?? undefined
  } catch {
    return undefined
  }
}
 
/** The remembered flat changes only by this page's own save. */
const noSubscription = () => () => undefined
 
function rememberFlat(id: string) {
  try {
    localStorage.setItem(LAST_FLAT_KEY, id)
  } catch {
    // Storage may be blocked; the choice just isn't remembered.
  }
}
 
export const Route = createFileRoute('/_authed/transactions/new')({
  validateSearch: z.object({ propertyId: z.string().max(64).optional() }),
  loaderDeps: ({ search }) => ({ propertyId: search.propertyId }),
  loader: ({ deps }) =>
    getNewTransactionOptions({ data: { propertyId: deps.propertyId } }),
  head: () => ({ meta: [{ title: m.transactions_new_page_title() }] }),
  component: NewTransactionRoute,
})
 
function Explanation(props: { title: string; text: string; noFlats: boolean }) {
  return (
    <Stack sx={{ minHeight: '100dvh', px: 2.5, pt: 6, gap: 2 }}>
      <Stack
        component="main"
        sx={{
          bgcolor: 'background.paper',
          border: `1px solid ${colors.cardBorder}`,
          borderRadius: `${String(radii.card)}px`,
          px: 2.5,
          py: 3,
          gap: 1.25,
          textAlign: 'center',
        }}
      >
        <Typography variant="h3" component="h1" sx={{ fontSize: 17 }}>
          {props.title}
        </Typography>
        <Typography variant="body2" color="text.secondary">
          {props.text}
        </Typography>
      </Stack>
      {props.noFlats ? (
        <Button
          component={Link}
          to="/properties/new"
          sx={{ minHeight: 56, borderRadius: 999 }}
        >
          {m.property_add()}
        </Button>
      ) : (
        <Button
          component={Link}
          to="/"
          variant="outlined"
          sx={{ minHeight: 56, borderRadius: 999 }}
        >
          {m.property_back_to_list()}
        </Button>
      )}
    </Stack>
  )
}
 
function NewTransactionRoute() {
  const options = Route.useLoaderData()
  const router = useRouter()
  const owned = sortByStreetAddress(options.owned, getLocale())
  // Also an archived flat: it is left out of the choice only.
  const fixed = options.fixed ?? undefined
  // The server can't see this device's storage: hydration uses the server's
  // value, then the page renders again with the remembered flat.
  const remembered = useSyncExternalStore(
    noSubscription,
    lastFlat,
    () => undefined,
  )
  if (owned.length === 0 && !fixed) {
    if (options.ownsArchived) {
      return (
        <Explanation
          noFlats={false}
          title={m.transactions_archived_only_title()}
          text={m.transactions_archived_only_text()}
        />
      )
    }
    return options.hasFlats ? (
      <Explanation
        noFlats={false}
        title={m.transactions_member_only_title()}
        text={m.transactions_member_only_text()}
      />
    ) : (
      <Explanation
        noFlats
        title={m.transactions_no_flats_title()}
        text={m.transactions_no_flats_text()}
      />
    )
  }
  // fixed is set or owned has at least one flat here.
  const initial =
    fixed ?? owned.find((flat) => flat.id === remembered) ?? owned[0]
  return (
    <TransactionForm
      // The form keeps its own flat; a remembered flat found after mount
      // starts it again with that flat.
      key={initial.id}
      flats={fixed ? [fixed] : owned}
      initialFlatId={initial.id}
      pickFlat={!fixed}
      today={options.today}
      onClose={() => {
        if (fixed) {
          void router.navigate({
            to: '/properties/$propertyId/transactions',
            params: { propertyId: fixed.id },
          })
        } else {
          void router.navigate({ to: '/' })
        }
      }}
      onSaved={async (saved) => {
        rememberFlat(saved.propertyId)
        await router.invalidate()
        await router.navigate({
          to: '/properties/$propertyId/transactions',
          params: { propertyId: saved.propertyId },
          search: { year: Number(saved.date.slice(0, 4)) },
        })
      }}
    />
  )
}