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 | 23x 7x 23x 12x 12x 12x 12x 12x 12x 12x 40x 40x 37x 163x 23x 23x 81x 81x 81x 75x 6x | import type React from "react";
import { useMutation } from "@tanstack/react-query";
import { useTranslation } from "react-i18next";
import { z } from "zod";
import { useBooleanState } from "~app/hooks/use-boolean-state";
import { useTrpcMutationOptions } from "~app/hooks/use-trpc-mutation-options";
import { useAppForm } from "~app/utils/forms";
import { useTRPC } from "~app/utils/trpc";
import { passwordSchema } from "~app/utils/validation";
import { Button } from "~components/button";
import { options as userChangePasswordOptions } from "~mutations/user/change-password";
const formSchema = z
.object({
prevPassword: passwordSchema,
password: passwordSchema,
passwordRetype: passwordSchema,
})
.refine((obj) => obj.password === obj.passwordRetype, {
path: ["passwordRetype"],
message: "Passwords don't match",
});
type Form = z.infer<typeof formSchema>;
const ChangePasswordForm: React.FC = () => {
const { t } = useTranslation("user");
const trpc = useTRPC();
const changePasswordMutation = useMutation(
trpc.user.changePassword.mutationOptions(
useTrpcMutationOptions(userChangePasswordOptions),
),
);
const defaultValues: Partial<Form> = {};
const form = useAppForm({
defaultValues: defaultValues as Form,
validators: {
onMount: formSchema,
onChange: formSchema,
onSubmit: formSchema,
},
onSubmit: ({ value }) => {
changePasswordMutation.mutate({
prevPassword: value.prevPassword,
password: value.password,
});
},
});
return (
<form.AppForm>
<form.Form className="flex flex-col gap-4" testID="password-form">
<form.AppField name="prevPassword">
{(field) => (
<field.TextField
value={field.state.value}
onValueChange={field.setValue}
name={field.name}
onBlur={field.handleBlur}
label={t("changePassword.prevPassword.label")}
mutation={changePasswordMutation}
fieldError={
field.state.meta.isDirty ? field.state.meta.errors : undefined
}
type="password"
/>
)}
</form.AppField>
<form.AppField name="password">
{(field) => (
<field.TextField
value={field.state.value}
onValueChange={field.setValue}
name={field.name}
onBlur={field.handleBlur}
label={t("changePassword.password.label")}
mutation={changePasswordMutation}
fieldError={
field.state.meta.isDirty ? field.state.meta.errors : undefined
}
type="password"
/>
)}
</form.AppField>
<form.AppField name="passwordRetype">
{(field) => (
<field.TextField
value={field.state.value}
onValueChange={field.setValue}
name={field.name}
onBlur={field.handleBlur}
label={t("changePassword.retypePassword.label")}
mutation={changePasswordMutation}
fieldError={
field.state.meta.isDirty ? field.state.meta.errors : undefined
}
type="password"
/>
)}
</form.AppField>
<form.Subscribe selector={(state) => state.canSubmit}>
{(canSubmit) => (
<Button
color="primary"
isDisabled={!canSubmit || changePasswordMutation.isPending}
isLoading={changePasswordMutation.isPending}
type="submit"
>
{t("changePassword.submitButton")}
</Button>
)}
</form.Subscribe>
</form.Form>
</form.AppForm>
);
};
export const ChangePasswordScreen = () => {
const { t } = useTranslation("user");
const [changePasswordShown, { setTrue: showChangePassword }] =
useBooleanState();
if (!changePasswordShown) {
return (
<Button color="primary" onPress={showChangePassword}>
{t("changePassword.showFormButton")}
</Button>
);
}
return <ChangePasswordForm />;
};
|