All files / src/features/recurring-amounts UndoDialog.tsx

86.66% Statements 13/15
100% Branches 4/4
100% Functions 4/4
86.66% Lines 13/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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102                                                              116x 116x     4x 4x 4x 4x 4x 3x   1x           4x       116x                                                   4x                       141x                      
// "Undo the latest change" and its confirmation, for an amount or the
// treatment (Design canvas: VastikePeru). The form gives the texts and the
// server call; the dialog shows a refusal or failure and reloads once done.
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 { failureReason } from '#/lib/network-error'
import type { AlertReason } from '#/lib/reasons'
import { m } from '#/paraglide/messages.js'
 
interface Props {
  open: boolean
  onClose: () => void
  title: string
  text: string
  /** The server call. */
  undo: () => Promise<{ ok: true } | { ok: false; reason: AlertReason }>
  /** The client log event when the call fails. */
  failureEvent: string
  /** After undoing: reload and go back to the page. */
  onDone: () => Promise<void>
}
 
export function UndoDialog(props: Props) {
  const [alert, setAlert] = useState<AlertReason>()
  const [busy, setBusy] = useState(false)
 
  async function undo() {
    setAlert(undefined)
    setBusy(true)
    try {
      const result = await props.undo()
      if (result.ok) {
        await props.onDone()
      } else {
        setAlert(result.reason)
      }
    } catch (error) {
      clientLog.error(props.failureEvent, {}, error)
      setAlert(failureReason(error))
    } finally {
      setBusy(false)
    }
  }
 
  return (
    <Dialog
      open={props.open}
      onClose={props.onClose}
      aria-labelledby="undo-title"
    >
      <DialogTitle
        id="undo-title"
        sx={{ fontSize: 20, fontWeight: 900, lineHeight: 1.3 }}
      >
        {props.title}
      </DialogTitle>
      <DialogContent>
        <Stack sx={{ gap: 1.5 }}>
          <DialogContentText>{props.text}</DialogContentText>
          {alert === undefined ? null : <ReasonAlert reason={alert} />}
        </Stack>
      </DialogContent>
      <DialogActions>
        <Button variant="text" color="inherit" onClick={props.onClose}>
          {m.amounts_dialog_cancel()}
        </Button>
        <Button
          color="error"
          loading={busy}
          onClick={() => {
            void undo()
          }}
        >
          {m.amounts_undo_confirm()}
        </Button>
      </DialogActions>
    </Dialog>
  )
}
 
/** "Undo the latest change", under a history. */
export function UndoButton(props: { onClick: () => void }) {
  return (
    <Button
      variant="text"
      color="inherit"
      onClick={props.onClick}
      sx={{ alignSelf: 'flex-start', textDecoration: 'underline' }}
    >
      {m.amounts_undo()}
    </Button>
  )
}