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 164 165 166 167 168 169 170 | 28x 28x 28x 28x 28x 3x 3x 1x 1x 1x 28x 14x 5x 14x 14x 14x 14x 28x | import { zodResolver } from '@hookform/resolvers/zod'
import { useState } from 'react'
import { useForm } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
import { toast } from 'sonner'
import { Button } from '@/components/ui/button'
import { PasswordInput as Input } from './PasswordInput'
import { Label } from '@/components/ui/label'
import { ApiError } from '@/lib/http-client'
import { fieldErrorKey } from '@/shared/utils/zod-error'
import { useUpdateProfile } from '../api/profile.hooks'
import { changePasswordFormSchema, type ChangePasswordFormInput } from '../api/profile.schemas'
/**
* Os três campos de senha só existem depois que alguém pede para trocá-la.
*
* Abertos por padrão, esta página mostrava **quatro** campos de senha de uma vez — três aqui e o
* "senha atual" do formulário de conta — para quem tinha entrado só para mudar o idioma. Trocar a
* senha é uma tarefa deliberada e rara; ocupar a tela com ela por padrão treina a pessoa a
* ignorar campos de senha, que é o hábito que menos se quer criar num app com dado de saúde.
*/
export function ChangePasswordForm() {
const { t } = useTranslation()
const updateProfile = useUpdateProfile()
const [isOpen, setIsOpen] = useState(false)
const {
register,
handleSubmit,
reset,
formState: { errors, isSubmitting },
} = useForm<ChangePasswordFormInput>({
resolver: zodResolver(changePasswordFormSchema),
defaultValues: { currentPassword: '', newPassword: '', confirmNewPassword: '' },
})
const onSubmit = handleSubmit(async (values) => {
try {
await updateProfile.mutateAsync({
password: values.newPassword,
currentPassword: values.currentPassword,
})
reset()
// Fecha ao salvar: a tarefa acabou, e deixar os campos abertos e vazios sugere que não.
setIsOpen(false)
toast.success(t('profile.password.savedToast'))
} catch {
// surfaced below via updateProfile.error
}
})
function close() {
if (isSubmitting) return
reset()
updateProfile.reset()
setIsOpen(false)
}
if (!isOpen) {
return (
<div>
<p className="text-sm text-ink-muted">{t('profile.password.sectionDescription')}</p>
<Button type="button" variant="outline" className="mt-4" onClick={() => setIsOpen(true)}>
{t('profile.password.startAction')}
</Button>
</div>
)
}
const currentPasswordErrorKey = fieldErrorKey(errors.currentPassword)
const newPasswordErrorKey = fieldErrorKey(errors.newPassword)
const confirmNewPasswordErrorKey = fieldErrorKey(errors.confirmNewPassword)
const submitErrorMessage =
updateProfile.error instanceof ApiError && updateProfile.error.status === 400
? t('profile.password.incorrectCurrentPassword')
: updateProfile.error
? t('profile.password.genericError')
: null
return (
<form onSubmit={onSubmit} className="space-y-6" noValidate>
<div>
<Label htmlFor="change-password-current">
{t('profile.password.currentPasswordLabel')}
</Label>
<Input
id="change-password-current"
disabled={isSubmitting}
type="password"
autoComplete="current-password"
aria-invalid={!!errors.currentPassword}
aria-describedby={currentPasswordErrorKey ? 'change-password-current-error' : undefined}
className="mt-2"
{...register('currentPassword')}
/>
{currentPasswordErrorKey && (
<p id="change-password-current-error" className="text-destructive mt-1 text-sm">
{t(currentPasswordErrorKey)}
</p>
)}
</div>
<div>
<Label htmlFor="change-password-new">{t('profile.password.newPasswordLabel')}</Label>
<Input
id="change-password-new"
disabled={isSubmitting}
type="password"
autoComplete="new-password"
aria-invalid={!!errors.newPassword}
aria-describedby={newPasswordErrorKey ? 'change-password-new-error' : undefined}
className="mt-2"
{...register('newPassword')}
/>
{newPasswordErrorKey && (
<p id="change-password-new-error" className="text-destructive mt-1 text-sm">
{t(newPasswordErrorKey)}
</p>
)}
</div>
<div>
<Label htmlFor="change-password-confirm">
{t('profile.password.confirmNewPasswordLabel')}
</Label>
<Input
id="change-password-confirm"
disabled={isSubmitting}
type="password"
autoComplete="new-password"
aria-invalid={!!errors.confirmNewPassword}
aria-describedby={
confirmNewPasswordErrorKey ? 'change-password-confirm-error' : undefined
}
className="mt-2"
{...register('confirmNewPassword')}
/>
{confirmNewPasswordErrorKey && (
<p id="change-password-confirm-error" className="text-destructive mt-1 text-sm">
{t(confirmNewPasswordErrorKey)}
</p>
)}
</div>
{submitErrorMessage && (
<p role="alert" className="text-destructive text-sm">
{submitErrorMessage}
</p>
)}
<div className="flex items-center justify-end gap-4">
<button
type="button"
onClick={close}
disabled={isSubmitting}
className="min-h-11 px-3 py-2 text-sm font-bold text-ink-muted transition-colors hover:text-ink"
>
{t('common.cancel')}
</button>
<Button type="submit" disabled={isSubmitting}>
{isSubmitting ? t('common.saving') : t('profile.password.submit')}
</Button>
</div>
</form>
)
}
|