|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 4 | +import { useMutation } from "@tanstack/react-query"; |
| 5 | +import { useForm } from "react-hook-form"; |
| 6 | +import { toast } from "sonner"; |
| 7 | + |
| 8 | +import { PasswordInput } from "@/components/inputs/password-input"; |
| 9 | +import { Button } from "@/components/ui/button"; |
| 10 | +import { Form, FormField } from "@/components/ui/form"; |
| 11 | +import { FetchError } from "@/features/backend"; |
| 12 | +import { getToastMessages } from "@/lib/get-toast-messages"; |
| 13 | + |
| 14 | +import { changePassword } from "../api/change-password"; |
| 15 | +import { ChangePasswordSchema } from "../schemas/change-password-schema"; |
| 16 | +import type { ChangePasswordFormValues } from "../schemas/change-password-schema"; |
| 17 | + |
| 18 | +/** |
| 19 | + * If a fetch request fails due to user input (incorrect old password), displays an appropriate toast message. |
| 20 | + * @param {unknown} error - Error thrown by the fetch request. |
| 21 | + * @returns {boolean} Whether the error was handled (whether a toast message was displayed). |
| 22 | + */ |
| 23 | +function handleServerValidationErrors(error: unknown): boolean { |
| 24 | + if (!(error instanceof FetchError)) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + |
| 28 | + const validationIssues = error.errorReport?.error.validationIssues; |
| 29 | + if (!Array.isArray(validationIssues)) { |
| 30 | + return false; |
| 31 | + } |
| 32 | + for (const issue of validationIssues) { |
| 33 | + const fieldName = issue.field ?? issue.rule; |
| 34 | + const refersToOldPassword = |
| 35 | + fieldName === "oldPassword" || |
| 36 | + Object.keys(issue).includes("oldPassword") || |
| 37 | + Object.values(issue).includes("oldPassword"); |
| 38 | + if (refersToOldPassword) { |
| 39 | + toast.error(getToastMessages.changePassword.invalidOldPassword); |
| 40 | + return true; |
| 41 | + } |
| 42 | + } |
| 43 | + return false; |
| 44 | +} |
| 45 | + |
| 46 | +export function ChangePasswordForm() { |
| 47 | + const form = useForm<ChangePasswordFormValues>({ |
| 48 | + resolver: zodResolver(ChangePasswordSchema), |
| 49 | + defaultValues: { |
| 50 | + oldPassword: "", |
| 51 | + newPassword: "", |
| 52 | + newPasswordConfirm: "", |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + const { mutateAsync, isPending } = useMutation({ |
| 57 | + mutationFn: changePassword, |
| 58 | + }); |
| 59 | + |
| 60 | + return ( |
| 61 | + <Form {...form}> |
| 62 | + <form |
| 63 | + noValidate |
| 64 | + onSubmit={form.handleSubmit(async (data) => { |
| 65 | + const messages = getToastMessages.changePassword; |
| 66 | + const loadingToast = toast.loading(messages.loading); |
| 67 | + try { |
| 68 | + await mutateAsync(data); |
| 69 | + toast.success(messages.success); |
| 70 | + form.reset(); |
| 71 | + } catch (error: unknown) { |
| 72 | + const handled = handleServerValidationErrors(error); |
| 73 | + if (!handled) { |
| 74 | + toast.error(messages.error); |
| 75 | + } |
| 76 | + } finally { |
| 77 | + toast.dismiss(loadingToast); |
| 78 | + } |
| 79 | + })} |
| 80 | + className="bg-background w-full max-w-md space-y-4 rounded-xl px-6 py-8" |
| 81 | + > |
| 82 | + <FormField |
| 83 | + control={form.control} |
| 84 | + name="oldPassword" |
| 85 | + render={({ field }) => ( |
| 86 | + <PasswordInput |
| 87 | + label="Aktualne hasło" |
| 88 | + placeholder="Aktualne hasło" |
| 89 | + {...field} |
| 90 | + /> |
| 91 | + )} |
| 92 | + /> |
| 93 | + |
| 94 | + <FormField |
| 95 | + control={form.control} |
| 96 | + name="newPassword" |
| 97 | + render={({ field }) => ( |
| 98 | + <PasswordInput |
| 99 | + label="Nowe hasło" |
| 100 | + placeholder="Nowe hasło" |
| 101 | + {...field} |
| 102 | + /> |
| 103 | + )} |
| 104 | + /> |
| 105 | + |
| 106 | + <FormField |
| 107 | + control={form.control} |
| 108 | + name="newPasswordConfirm" |
| 109 | + render={({ field }) => ( |
| 110 | + <PasswordInput |
| 111 | + label="Potwierdź nowe hasło" |
| 112 | + placeholder="Potwierdź nowe hasło" |
| 113 | + {...field} |
| 114 | + /> |
| 115 | + )} |
| 116 | + /> |
| 117 | + |
| 118 | + <div className="flex justify-end"> |
| 119 | + <Button type="submit" loading={isPending}> |
| 120 | + Zmień hasło |
| 121 | + </Button> |
| 122 | + </div> |
| 123 | + </form> |
| 124 | + </Form> |
| 125 | + ); |
| 126 | +} |
0 commit comments