Skip to content

Commit 08b782d

Browse files
committed
move variants inside components
1 parent d1498e7 commit 08b782d

File tree

4 files changed

+24
-30
lines changed

4 files changed

+24
-30
lines changed

frontend/src/components/PasswordCreationDoubleInput.tsx

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,36 @@ const usePasswordComplexity = (password: string): PasswordComplexity => {
5252
return result;
5353
};
5454

55+
type PasswordVariant = "register" | "change";
56+
5557
export default function PasswordCreationDoubleInput({
5658
siteConfig,
5759
forceShowNewPasswordInvalid,
58-
newPasswordFieldName,
59-
newPasswordLabel,
60-
newPasswordAgainFieldName,
61-
newPasswordAgainLabel,
60+
variant="change",
6261
}: {
6362
siteConfig: FragmentType<typeof CONFIG_FRAGMENT>;
6463
forceShowNewPasswordInvalid: boolean;
65-
newPasswordFieldName: string;
66-
newPasswordLabel: string;
67-
newPasswordAgainFieldName: string;
68-
newPasswordAgainLabel: string;
64+
variant?: PasswordVariant;
6965
}): React.ReactElement {
7066
const { t } = useTranslation();
7167
const { minimumPasswordComplexity } = useFragment(
7268
CONFIG_FRAGMENT,
7369
siteConfig,
7470
);
71+
const variantFields = {
72+
register: {
73+
passwordFieldName: "password",
74+
passwordLabel: t("common.password"),
75+
passwordConfirmFieldName: "password_confirm",
76+
passwordConfirmLabel: t("common.password_confirm"),
77+
},
78+
change: {
79+
passwordFieldName: "new_password",
80+
passwordLabel: t("common.password"),
81+
passwordConfirmFieldName: "new_password_again",
82+
passwordConfirmLabel: t("common.password_confirm"),
83+
}
84+
}[variant];
7585

7686
const newPasswordRef = useRef<HTMLInputElement>(null);
7787
const newPasswordAgainRef = useRef<HTMLInputElement>(null);
@@ -89,8 +99,8 @@ export default function PasswordCreationDoubleInput({
8999

90100
return (
91101
<>
92-
<Form.Field name={newPasswordFieldName}>
93-
<Form.Label>{newPasswordLabel}</Form.Label>
102+
<Form.Field name={variantFields.passwordFieldName}>
103+
<Form.Label>{variantFields.passwordLabel}</Form.Label>
94104

95105
<Form.PasswordControl
96106
required
@@ -134,13 +144,13 @@ export default function PasswordCreationDoubleInput({
134144
)}
135145
</Form.Field>
136146

137-
<Form.Field name={newPasswordAgainFieldName}>
147+
<Form.Field name={variantFields.passwordConfirmFieldName}>
138148
{/*
139149
TODO This field has validation defects,
140150
some caused by Radix-UI upstream bugs.
141151
https://github.com/matrix-org/matrix-authentication-service/issues/2855
142152
*/}
143-
<Form.Label>{newPasswordAgainLabel}</Form.Label>
153+
<Form.Label>{variantFields.passwordConfirmLabel}</Form.Label>
144154

145155
<Form.PasswordControl
146156
required
@@ -153,7 +163,7 @@ export default function PasswordCreationDoubleInput({
153163
</Form.ErrorMessage>
154164

155165
<Form.ErrorMessage
156-
match={(v, form) => v !== form.get(newPasswordFieldName)}
166+
match={(v, form) => v !== form.get(variantFields.passwordFieldName)}
157167
>
158168
{t("frontend.password_change.passwords_no_match")}
159169
</Form.ErrorMessage>

frontend/src/external/register/PasswordDoubleInput.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ function PasswordDoubleInput({
3737
const {
3838
data: { siteConfig },
3939
} = useSuspenseQuery(query);
40-
const { t } = useTranslation();
4140

4241
return (
4342
// Form.Root is needed because Form.Field requires to be included into a Form
@@ -47,10 +46,7 @@ function PasswordDoubleInput({
4746
<PasswordCreationDoubleInput
4847
siteConfig={siteConfig}
4948
forceShowNewPasswordInvalid={forceShowNewPasswordInvalid}
50-
newPasswordFieldName="password"
51-
newPasswordLabel={t("common.password")}
52-
newPasswordAgainFieldName="password_confirm"
53-
newPasswordAgainLabel={t("common.password_confirm")}
49+
variant="register"
5450
/>
5551
</div>
5652
</Form.Root>

frontend/src/routes/password.change.index.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,6 @@ function ChangePassword(): React.ReactNode {
189189
mutation.data.status === "INVALID_NEW_PASSWORD") ||
190190
false
191191
}
192-
newPasswordFieldName="new_password"
193-
newPasswordLabel={t("frontend.password_change.new_password_label")}
194-
newPasswordAgainFieldName="new_password_again"
195-
newPasswordAgainLabel={t(
196-
"frontend.password_change.new_password_again_label",
197-
)}
198192
/>
199193

200194
<Form.Submit kind="primary" disabled={mutation.isPending}>

frontend/src/routes/password.recovery.index.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,6 @@ const EmailRecovery: React.FC<{
291291
forceShowNewPasswordInvalid={
292292
mutation.data?.status === "INVALID_NEW_PASSWORD" || false
293293
}
294-
newPasswordFieldName="new_password"
295-
newPasswordLabel={t("frontend.password_change.new_password_label")}
296-
newPasswordAgainFieldName="new_password_again"
297-
newPasswordAgainLabel={t(
298-
"frontend.password_change.new_password_again_label",
299-
)}
300294
/>
301295

302296
<Form.Submit kind="primary" disabled={mutation.isPending}>

0 commit comments

Comments
 (0)