All files / packages/app/features/reset-password reset-password-screen.tsx

100% Statements 26/26
100% Branches 10/10
100% Functions 10/10
100% Lines 26/26

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                                            13x         10x           13x   57x 57x     57x                 13x 47x 47x 47x 47x   47x     1x       47x 47x               3x       47x                           91x                                 92x                             202x   86x                                       13x 49x 49x 49x 49x                              
import React from "react";
 
import { useMutation, useSuspenseQuery } from "@tanstack/react-query";
import { useTranslation } from "react-i18next";
import { z } from "zod";
 
import { EmptyCard } from "~app/components/empty-card";
import { ErrorMessage } from "~app/components/error-message";
import { PageHeader } from "~app/components/page-header";
import { suspendedFallback } from "~app/components/suspense-wrapper";
import { NavigationContext } from "~app/contexts/navigation-context";
import { useTrpcMutationOptions } from "~app/hooks/use-trpc-mutation-options";
import { useAppForm } from "~app/utils/forms";
import { getPathHooks } from "~app/utils/navigation";
import { useTRPC } from "~app/utils/trpc";
import { passwordSchema } from "~app/utils/validation";
import { Button } from "~components/button";
import { Input } from "~components/input";
import { Skeleton } from "~components/skeleton";
import { Text } from "~components/text";
import { options as authResetPasswordOptions } from "~mutations/auth/reset-password";
 
const formSchema = z
	.object({
		password: passwordSchema,
		passwordRetype: passwordSchema,
	})
	.refine((obj) => obj.password === obj.passwordRetype, {
		path: ["passwordRetype"],
		message: "Passwords don't match",
	});
type Form = z.infer<typeof formSchema>;
 
const ResetPasswordHeader = suspendedFallback<{ token: string }>(
	({ token }) => {
		const trpc = useTRPC();
		const { data: intention } = useSuspenseQuery(
			trpc.resetPasswordIntentions.get.queryOptions({ token }),
		);
		return <Text variant="h3">{intention.email}</Text>;
	},
	<Skeleton className="h-8 w-60 rounded-sm" />,
);
 
type Props = {
	token: string;
};
 
const ResetPassword: React.FC<Props> = ({ token }) => {
	const { t } = useTranslation("reset-password");
	const trpc = useTRPC();
	const { useNavigate } = React.use(NavigationContext);
	const navigate = useNavigate();
 
	const changePasswordMutation = useMutation(
		trpc.auth.resetPassword.mutationOptions(
			useTrpcMutationOptions(authResetPasswordOptions, {
				onSuccess: () => navigate({ to: "/login", replace: true }),
			}),
		),
	);
	const defaultValues: Partial<Form> = {};
	const form = useAppForm({
		defaultValues: defaultValues as Form,
		validators: {
			onMount: formSchema,
			onChange: formSchema,
			onSubmit: formSchema,
		},
		onSubmit: ({ value }) => {
			changePasswordMutation.mutate({ password: value.password, token });
		},
	});
 
	return (
		<>
			<ResetPasswordHeader token={token} />
			<form.AppForm>
				<form.Form className="flex flex-col gap-4">
					<Input
						value={token}
						label={t("form.token.label")}
						isDisabled
						className="hidden"
						autoComplete="username"
					/>
					<form.AppField name="password">
						{(field) => (
							<field.TextField
								value={field.state.value}
								onValueChange={field.setValue}
								name={field.name}
								onBlur={field.handleBlur}
								label={t("form.password.label")}
								type="password"
								autoComplete="new-password"
								fieldError={
									field.state.meta.isDirty ? field.state.meta.errors : undefined
								}
								mutation={changePasswordMutation}
							/>
						)}
					</form.AppField>
					<form.AppField name="passwordRetype">
						{(field) => (
							<field.TextField
								value={field.state.value}
								onValueChange={field.setValue}
								name={field.name}
								onBlur={field.handleBlur}
								label={t("form.passwordRetype.label")}
								type="password"
								autoComplete="new-password"
								fieldError={
									field.state.meta.isDirty ? field.state.meta.errors : undefined
								}
								mutation={changePasswordMutation}
							/>
						)}
					</form.AppField>
					<form.Subscribe selector={(state) => state.canSubmit}>
						{(canSubmit) => (
							<Button
								className="mt-4"
								color="primary"
								isDisabled={!canSubmit || changePasswordMutation.isPending}
								isLoading={changePasswordMutation.isPending}
								type="submit"
							>
								{t("form.submit")}
							</Button>
						)}
					</form.Subscribe>
					{changePasswordMutation.status === "error" ? (
						<ErrorMessage message={changePasswordMutation.error.message} />
					) : null}
				</form.Form>
			</form.AppForm>
		</>
	);
};
 
export const ResetPasswordScreen = () => {
	const { useQueryState } = getPathHooks("/_public/reset-password");
	const [token] = useQueryState("token");
	const { t } = useTranslation("reset-password");
	return (
		<>
			<PageHeader>{t("header")}</PageHeader>
			{token ? (
				<ResetPassword token={token} />
			) : (
				<EmptyCard title={t("noToken.title")}>
					<Text variant="h3" className="text-center">
						{t("noToken.message")}
					</Text>
				</EmptyCard>
			)}
		</>
	);
};