@@ -9,13 +9,38 @@ import { PasswordInput } from "@/components/inputs/password-input";
99import { Button } from "@/components/ui/button" ;
1010import { Form , FormField } from "@/components/ui/form" ;
1111import { FetchError } from "@/features/backend" ;
12- import { logger , parseError } from "@/features/logging" ;
1312import { getToastMessages } from "@/lib/get-toast-messages" ;
1413
1514import { changePassword } from "../api/change-password" ;
1615import { ChangePasswordSchema } from "../schemas/change-password-schema" ;
1716import 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+
1944export 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 ) }
0 commit comments