|
| 1 | +import { useState } from 'react' |
| 2 | +import { Button, Input, FormFieldLayout } from '@helpwave/hightide' |
| 3 | +import type { KcContext } from '../KcContext' |
| 4 | +import { useTranslation } from '../../i18n/useTranslation' |
| 5 | +import { useTranslatedFieldError } from '../../login/utils/translateFieldError' |
| 6 | + |
| 7 | +type AccountPasswordProps = { |
| 8 | + kcContext: Extract<KcContext, { pageId: 'password.ftl' }>, |
| 9 | +} |
| 10 | + |
| 11 | +export default function AccountPassword({ kcContext }: AccountPasswordProps) { |
| 12 | + const t = useTranslation() |
| 13 | + const translateError = useTranslatedFieldError() |
| 14 | + const { url, password, account, stateChecker, messagesPerField, message } = kcContext |
| 15 | + |
| 16 | + const [currentPassword, setCurrentPassword] = useState('') |
| 17 | + const [newPassword, setNewPassword] = useState('') |
| 18 | + const [newPasswordConfirm, setNewPasswordConfirm] = useState('') |
| 19 | + |
| 20 | + const passwordError = messagesPerField.existsError('password') |
| 21 | + ? messagesPerField.get('password') |
| 22 | + : undefined |
| 23 | + const newPasswordError = messagesPerField.existsError('password-new') |
| 24 | + ? messagesPerField.get('password-new') |
| 25 | + : undefined |
| 26 | + const passwordConfirmError = messagesPerField.existsError('password-confirm') |
| 27 | + ? messagesPerField.get('password-confirm') |
| 28 | + : undefined |
| 29 | + |
| 30 | + return ( |
| 31 | + <div className="flex flex-col gap-8"> |
| 32 | + {message && ( |
| 33 | + <div |
| 34 | + role="alert" |
| 35 | + style={{ |
| 36 | + padding: '1rem', |
| 37 | + borderRadius: '0.5rem', |
| 38 | + backgroundColor: |
| 39 | + message.type === 'error' |
| 40 | + ? 'var(--hw-color-negative-50)' |
| 41 | + : 'var(--hw-color-positive-50)', |
| 42 | + color: |
| 43 | + message.type === 'error' |
| 44 | + ? 'var(--hw-color-negative-900)' |
| 45 | + : 'var(--hw-color-positive-900)', |
| 46 | + marginBottom: '0.5rem' |
| 47 | + }} |
| 48 | + > |
| 49 | + {message.summary} |
| 50 | + </div> |
| 51 | + )} |
| 52 | + |
| 53 | + <section className="flex flex-col gap-4"> |
| 54 | + <h2 className="text-lg font-bold text-[var(--hw-color-neutral-700)]"> |
| 55 | + {t('passwordSectionTitle')} |
| 56 | + </h2> |
| 57 | + <form |
| 58 | + action={url.passwordUrl} |
| 59 | + method="post" |
| 60 | + className="flex flex-col gap-4" |
| 61 | + > |
| 62 | + <input type="hidden" name="stateChecker" value={stateChecker} /> |
| 63 | + <input |
| 64 | + type="hidden" |
| 65 | + name="username" |
| 66 | + value={account.username ?? ''} |
| 67 | + autoComplete="username" |
| 68 | + readOnly |
| 69 | + /> |
| 70 | + |
| 71 | + {password.passwordSet && ( |
| 72 | + <div> |
| 73 | + <FormFieldLayout |
| 74 | + label={t('password')} |
| 75 | + invalidDescription={translateError(passwordError)} |
| 76 | + > |
| 77 | + {({ id, ariaAttributes }) => ( |
| 78 | + <Input |
| 79 | + id={id} |
| 80 | + name="password" |
| 81 | + type="password" |
| 82 | + value={currentPassword} |
| 83 | + onChange={(e) => setCurrentPassword(e.target.value)} |
| 84 | + autoComplete="current-password" |
| 85 | + {...ariaAttributes} |
| 86 | + /> |
| 87 | + )} |
| 88 | + </FormFieldLayout> |
| 89 | + </div> |
| 90 | + )} |
| 91 | + |
| 92 | + <div> |
| 93 | + <FormFieldLayout |
| 94 | + label={t('passwordNew')} |
| 95 | + invalidDescription={translateError(newPasswordError)} |
| 96 | + required |
| 97 | + > |
| 98 | + {({ id, ariaAttributes }) => ( |
| 99 | + <Input |
| 100 | + id={id} |
| 101 | + name="password-new" |
| 102 | + type="password" |
| 103 | + value={newPassword} |
| 104 | + onChange={(e) => setNewPassword(e.target.value)} |
| 105 | + autoComplete="new-password" |
| 106 | + required |
| 107 | + {...ariaAttributes} |
| 108 | + /> |
| 109 | + )} |
| 110 | + </FormFieldLayout> |
| 111 | + </div> |
| 112 | + |
| 113 | + <div> |
| 114 | + <FormFieldLayout |
| 115 | + label={t('passwordConfirm')} |
| 116 | + invalidDescription={translateError(passwordConfirmError)} |
| 117 | + required |
| 118 | + > |
| 119 | + {({ id, ariaAttributes }) => ( |
| 120 | + <Input |
| 121 | + id={id} |
| 122 | + name="password-confirm" |
| 123 | + type="password" |
| 124 | + value={newPasswordConfirm} |
| 125 | + onChange={(e) => setNewPasswordConfirm(e.target.value)} |
| 126 | + autoComplete="new-password" |
| 127 | + required |
| 128 | + {...ariaAttributes} |
| 129 | + /> |
| 130 | + )} |
| 131 | + </FormFieldLayout> |
| 132 | + </div> |
| 133 | + |
| 134 | + <div className="flex flex-col gap-2"> |
| 135 | + <Button type="submit" name="submitAction" value="Save" color="primary"> |
| 136 | + {t('doSave')} |
| 137 | + </Button> |
| 138 | + <Button |
| 139 | + type="button" |
| 140 | + color="neutral" |
| 141 | + onClick={() => { |
| 142 | + window.location.href = url.accountUrl |
| 143 | + }} |
| 144 | + > |
| 145 | + {t('backToAccount')} |
| 146 | + </Button> |
| 147 | + </div> |
| 148 | + </form> |
| 149 | + </section> |
| 150 | + </div> |
| 151 | + ) |
| 152 | +} |
0 commit comments