All files / src/features/rent-payments DeletePaymentDialog.tsx

80% Statements 12/15
50% Branches 2/4
100% Functions 3/3
80% Lines 12/15

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                                                    2x 2x 2x     1x 1x 1x 1x     1x 1x               1x       2x                                                     1x                  
// Deleting a rent payment, with a confirmation (openspec: rent-payments,
// "Deleting a rent payment"). Payments are not edited: a wrong one is
// deleted and recorded again.
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 { 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 { deleteRentPayment } from './functions'
import type { RentPayment } from './rent-payment.repository.server'
 
export function DeletePaymentDialog(props: {
  propertyId: string
  payment: RentPayment
  onClose: () => void
  onDone: () => Promise<void>
}) {
  const { payment } = props
  const [alert, setAlert] = useState<AlertReason>()
  const [busy, setBusy] = useState(false)
 
  async function remove() {
    setAlert(undefined)
    setBusy(true)
    try {
      const result = await deleteRentPayment({
        data: { propertyId: props.propertyId, paymentId: payment.id },
      })
      if (result.ok) {
        await props.onDone()
      } else E{
        setAlert(result.reason)
      }
    } catch (error) {
      clientLog.error('rent_payment.delete_failed', {}, error)
      setAlert(failureReason(error))
    } finally {
      setBusy(false)
    }
  }
 
  return (
    <Dialog open onClose={props.onClose} aria-labelledby="delete-payment-title">
      <DialogTitle
        id="delete-payment-title"
        sx={{ fontSize: 20, fontWeight: 900, lineHeight: 1.3 }}
      >
        {m.rent_payment_delete_title()}
      </DialogTitle>
      <DialogContent>
        <Stack sx={{ gap: 1.5 }}>
          <DialogContentText>
            {m.rent_payment_delete_text({
              amount: formatMoney(payment.amount),
              date: formatDate(payment.paidOn),
            })}
          </DialogContentText>
          {alert === undefined ? null : <ReasonAlert reason={alert} />}
        </Stack>
      </DialogContent>
      <DialogActions>
        <Button variant="text" color="inherit" onClick={props.onClose}>
          {m.rent_payment_cancel()}
        </Button>
        <Button
          color="error"
          disabled={busy}
          onClick={() => {
            void remove()
          }}
        >
          {m.rent_payment_delete()}
        </Button>
      </DialogActions>
    </Dialog>
  )
}