All files / src/features/account NameDialog.tsx

86.66% Statements 13/15
50% Branches 1/2
100% Functions 6/6
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                                                                3x 3x       1x 1x 1x     1x 1x               3x           2x 2x                         19x                                   69x   4x                    
// "Change name" on the account page (Design canvas: TiliNimi, TiliNimiVirhe;
// spec: "Changing the display name"). The rules are those of /welcome.
import Box from '@mui/material/Box'
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 DialogTitle from '@mui/material/DialogTitle'
import Stack from '@mui/material/Stack'
import Typography from '@mui/material/Typography'
import { useForm } from '@tanstack/react-form'
import { useState } from 'react'
import { ReasonAlert } from '#/components/ReasonAlert'
import { TextField } from '#/components/form/TextField'
import { setDisplayName } from '#/features/authentication/functions'
import {
  displayNameInput,
  displayNameSchema,
} from '#/features/authentication/schemas'
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 {
  name: string
  onClose: () => void
  /** Reloads the user, so the page shows the new name. */
  onSaved: () => Promise<void>
}
 
export function NameDialog({ name, onClose, onSaved }: Props) {
  const [alert, setAlert] = useState<AlertReason>()
  const form = useForm({
    defaultValues: { name },
    validators: { onChange: displayNameInput },
    onSubmit: async ({ value }) => {
      setAlert(undefined)
      try {
        await setDisplayName({
          data: { name: displayNameSchema.parse(value.name) },
        })
        await onSaved()
        onClose()
      } catch (error) {
        clientLog.error('account.name_save_failed', {}, error)
        setAlert(failureReason(error))
      }
    },
  })
 
  return (
    <Dialog open onClose={onClose} aria-labelledby="account-name-title">
      <Box
        component="form"
        noValidate
        onSubmit={(e) => {
          e.preventDefault()
          void form.handleSubmit()
        }}
      >
        <DialogTitle
          id="account-name-title"
          sx={{ fontSize: 20, fontWeight: 900, lineHeight: 1.3 }}
        >
          {m.account_change_name()}
        </DialogTitle>
        <DialogContent>
          <Stack sx={{ gap: 1.5, pt: 1 }}>
            <form.Field name="name">
              {(field) => (
                <TextField
                  field={field}
                  label={m.account_name_label()}
                  autoComplete="given-name"
                  fullWidth
                />
              )}
            </form.Field>
            <Typography variant="body2" color="text.secondary">
              {m.account_name_help()}
            </Typography>
            {alert === undefined ? null : <ReasonAlert reason={alert} />}
          </Stack>
        </DialogContent>
        <DialogActions>
          <Button variant="text" color="inherit" onClick={onClose}>
            {m.account_cancel()}
          </Button>
          <form.Subscribe selector={(s) => s.isSubmitting}>
            {(submitting) => (
              <Button type="submit" loading={submitting}>
                {m.account_save()}
              </Button>
            )}
          </form.Subscribe>
        </DialogActions>
      </Box>
    </Dialog>
  )
}