All files / src/features/transactions DeleteDialog.tsx

82.35% Statements 14/17
50% Branches 4/8
100% Functions 3/3
82.35% Lines 14/17

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                                                        21x 21x 21x 21x 21x                   1x 1x 1x 1x     1x 1x               1x       21x                                                   1x                  
// Confirming the deletion of an entry (Design canvas: "Muokkaa kirjausta ยท
// poista"): it names the entry and says that it and its receipt are deleted
// permanently (openspec: transactions, "Deleting an entry").
import Button from '@mui/material/Button'
import Dialog from '@mui/material/Dialog'
import DialogActions from '@mui/material/DialogActions'
import DialogContent from '@mui/material/DialogContent'
import DialogContentText from '@mui/material/DialogContentText'
import DialogTitle from '@mui/material/DialogTitle'
import Stack from '@mui/material/Stack'
import { useId, useState } from 'react'
import { ReasonAlert } from '#/components/ReasonAlert'
import { clientLog } from '#/lib/client-log'
import { formatDate, formatMoney } from '#/lib/format'
import { failureReason } from '#/lib/network-error'
import type { AlertReason } from '#/lib/reasons'
import { m } from '#/paraglide/messages.js'
import { deleteTransaction } from './functions'
import type { TransactionDetails } from './transaction.repository.server'
 
interface Props {
  open: boolean
  entry: TransactionDetails
  onClose: () => void
  onDeleted: () => Promise<void>
}
 
export function DeleteDialog(props: Props) {
  const { entry } = props
  const titleId = useId()
  const [busy, setBusy] = useState(false)
  const [alert, setAlert] = useState<AlertReason>()
  const name = m.transactions_delete_entry({
    type:
      entry.type === 'income'
        ? m.transactions_income_title()
        : m.transactions_expense_title(),
    amount: formatMoney(entry.amount),
    date: formatDate(entry.date),
  })
 
  async function remove() {
    setAlert(undefined)
    setBusy(true)
    try {
      const result = await deleteTransaction({
        data: { propertyId: entry.propertyId, transactionId: entry.id },
      })
      if (result.ok) {
        await props.onDeleted()
      } else E{
        setAlert(result.reason)
      }
    } catch (error) {
      clientLog.error('transaction.delete_failed', {}, error)
      setAlert(failureReason(error))
    } finally {
      setBusy(false)
    }
  }
 
  return (
    <Dialog open={props.open} onClose={props.onClose} aria-labelledby={titleId}>
      <DialogTitle
        id={titleId}
        sx={{ fontSize: 20, fontWeight: 900, lineHeight: 1.3 }}
      >
        {m.transactions_delete_title()}
      </DialogTitle>
      <DialogContent>
        <Stack sx={{ gap: 1.5 }}>
          <DialogContentText>
            {entry.receipt
              ? m.transactions_delete_text({ entry: name })
              : m.transactions_delete_text_no_receipt({ entry: name })}
          </DialogContentText>
          {alert === undefined ? null : <ReasonAlert reason={alert} />}
        </Stack>
      </DialogContent>
      <DialogActions>
        <Button variant="text" color="inherit" onClick={props.onClose}>
          {m.transactions_cancel()}
        </Button>
        <Button
          color="error"
          loading={busy}
          onClick={() => {
            void remove()
          }}
        >
          {m.transactions_delete_confirm()}
        </Button>
      </DialogActions>
    </Dialog>
  )
}