Skip to content

Commit 604af96

Browse files
committed
refactor: simplify password change error logic
1 parent 5bb7fec commit 604af96

4 files changed

Lines changed: 42 additions & 71 deletions

File tree

src/components/inputs/password-input.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export function PasswordInput({
2828
placeholder?: string;
2929
} & ControllerRenderProps) {
3030
const [showPassword, setShowPassword] = useState(false);
31-
const labelLowerCase = label.toLowerCase();
3231
return (
3332
<FormItem>
3433
<FormLabel>{label}</FormLabel>
@@ -42,11 +41,11 @@ export function PasswordInput({
4241
</FormControl>
4342
<InputGroupButton
4443
asChild
45-
tooltip={`${showPassword ? "Ukryj" : "Pokaż"} ${labelLowerCase}`}
44+
tooltip={`${showPassword ? "Ukryj" : "Pokaż"} hasło`}
4645
>
4746
<Toggle
4847
size="unset"
49-
aria-label={`Pokaż ${labelLowerCase}`}
48+
aria-label={`Pokaż ${label.toLowerCase()}`}
5049
pressed={showPassword}
5150
onPressedChange={setShowPassword}
5251
>

src/components/presentation/navbar.tsx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import { UserRound } from "lucide-react";
3+
import { KeyRound, UserRound } from "lucide-react";
44
import { usePathname } from "next/navigation";
55

66
import { Link } from "@/components/core/link";
@@ -39,16 +39,9 @@ function UserProfileMenu({ user }: { user: User | null }) {
3939
<DropdownMenuGroup>
4040
<DropdownMenuLabel>Moje konto</DropdownMenuLabel>
4141
{user == null ? (
42-
<>
43-
<DropdownMenuItem asChild className="cursor-pointer">
44-
<Link href="/login">Zaloguj się</Link>
45-
</DropdownMenuItem>
46-
<DropdownMenuItem asChild>
47-
<Link href="/change-password" className="block w-full">
48-
Zmiana hasła
49-
</Link>
50-
</DropdownMenuItem>
51-
</>
42+
<DropdownMenuItem asChild className="cursor-pointer">
43+
<Link href="/login">Zaloguj się</Link>
44+
</DropdownMenuItem>
5245
) : (
5346
<DropdownMenuItem className="font-normal">
5447
{user.fullName ?? user.email}
@@ -68,6 +61,12 @@ function UserProfileMenu({ user }: { user: User | null }) {
6861
{user != null && (
6962
<>
7063
<DropdownMenuSeparator />
64+
<DropdownMenuItem asChild>
65+
<Link href="/change-password" className="block w-full">
66+
<KeyRound />
67+
Zmiana hasła
68+
</Link>
69+
</DropdownMenuItem>
7170
<LogoutButton />
7271
</>
7372
)}

src/features/password-change/components/change-password-form.tsx

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,38 @@ import { PasswordInput } from "@/components/inputs/password-input";
99
import { Button } from "@/components/ui/button";
1010
import { Form, FormField } from "@/components/ui/form";
1111
import { FetchError } from "@/features/backend";
12-
import { logger, parseError } from "@/features/logging";
1312
import { getToastMessages } from "@/lib/get-toast-messages";
1413

1514
import { changePassword } from "../api/change-password";
1615
import { ChangePasswordSchema } from "../schemas/change-password-schema";
1716
import type { ChangePasswordFormValues } from "../schemas/change-password-schema";
1817

18+
/**
19+
* Jeżeli fetch nie przejdzie przez błąd użytkownika, wyświetli odpowidni toast
20+
* @param {unknown} error - błąd zwrócony przez fetch
21+
* @returns {boolean} - czy błąd został obsłużony (czy wyświetlono toast)
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 =
34+
(issue as Record<string, unknown>).field ??
35+
(issue as Record<string, unknown>).rule;
36+
if (fieldName === "oldPassword") {
37+
toast.error(getToastMessages.changePassword.invalidOldPassword);
38+
return true;
39+
}
40+
}
41+
return false;
42+
}
43+
1944
export function ChangePasswordForm() {
2045
const form = useForm<ChangePasswordFormValues>({
2146
resolver: zodResolver(ChangePasswordSchema),
@@ -30,53 +55,6 @@ export function ChangePasswordForm() {
3055
mutationFn: changePassword,
3156
});
3257

33-
function handleServerValidationErrors(error: unknown) {
34-
if (!(error instanceof FetchError)) {
35-
return false;
36-
}
37-
const validationIssues = error.errorReport?.error.validationIssues;
38-
if (!Array.isArray(validationIssues)) {
39-
return false;
40-
}
41-
for (const issue of validationIssues) {
42-
const fieldName =
43-
(issue as Record<string, unknown>).field ??
44-
(issue as Record<string, unknown>).rule;
45-
const message = (issue as Record<string, unknown>).message;
46-
if (fieldName === "oldPassword") {
47-
const serverMessage = typeof message === "string" ? message : undefined;
48-
let toastMessage: string | undefined;
49-
try {
50-
toastMessage = (
51-
getToastMessages.changePassword as { invalidOldPassword?: string }
52-
).invalidOldPassword;
53-
} catch (error_) {
54-
logger.error(
55-
parseError(error_),
56-
"ChangePasswordForm: failed to get invalidOldPassword message",
57-
);
58-
}
59-
60-
const fieldMessage =
61-
typeof toastMessage === "string" && toastMessage.length > 0
62-
? toastMessage
63-
: serverMessage;
64-
65-
form.setError("oldPassword", {
66-
type: "server",
67-
message: fieldMessage,
68-
});
69-
70-
if (typeof toastMessage === "string" && toastMessage.length > 0) {
71-
toast.error(toastMessage);
72-
}
73-
74-
return true;
75-
}
76-
}
77-
return false;
78-
}
79-
8058
return (
8159
<Form {...form}>
8260
<form
@@ -91,11 +69,7 @@ export function ChangePasswordForm() {
9169
} catch (error: unknown) {
9270
const handled = handleServerValidationErrors(error);
9371
if (!handled) {
94-
const errorMessage =
95-
typeof messages.error === "function"
96-
? messages.error(error)
97-
: (messages.error as string);
98-
toast.error(errorMessage);
72+
toast.error(messages.error);
9973
}
10074
} finally {
10175
toast.dismiss(loadingToast);
@@ -132,8 +106,8 @@ export function ChangePasswordForm() {
132106
name="newPasswordConfirm"
133107
render={({ field }) => (
134108
<PasswordInput
135-
label="Powtórzone hasło"
136-
placeholder="Powtórzone hasło"
109+
label="Potwierdź nowe hasło"
110+
placeholder="Potwierdź nowe hasło"
137111
{...field}
138112
/>
139113
)}

src/lib/get-toast-messages.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ export const getToastMessages = {
8888
changePassword: {
8989
loading: "Trwa zmiana hasła...",
9090
success: "Hasło zmienione poprawnie",
91-
error: (error: unknown) =>
92-
getErrorMessage(error, "Nie udało się zmienić hasła"),
91+
error: "Nie udało się zmienić hasła",
9392
invalidOldPassword: "Podane aktualne hasło jest niepoprawne",
9493
},
9594
};

0 commit comments

Comments
 (0)