@@ -3,8 +3,8 @@ import AppLayout from '@/layouts/app-layout';
33import SettingsLayout from '@/layouts/settings/layout' ;
44import { type BreadcrumbItem } from '@/types' ;
55import { Transition } from '@headlessui/react' ;
6- import { Head , useForm } from '@inertiajs/react' ;
7- import { FormEventHandler , useRef } from 'react' ;
6+ import { Form , Head } from '@inertiajs/react' ;
7+ import { useRef } from 'react' ;
88
99import HeadingSmall from '@/components/heading-small' ;
1010import { Button } from '@/components/ui/button' ;
@@ -22,32 +22,6 @@ export default function Password() {
2222 const passwordInput = useRef < HTMLInputElement > ( null ) ;
2323 const currentPasswordInput = useRef < HTMLInputElement > ( null ) ;
2424
25- const { data, setData, errors, put, reset, processing, recentlySuccessful } = useForm ( {
26- current_password : '' ,
27- password : '' ,
28- password_confirmation : '' ,
29- } ) ;
30-
31- const updatePassword : FormEventHandler = ( e ) => {
32- e . preventDefault ( ) ;
33-
34- put ( route ( 'password.update' ) , {
35- preserveScroll : true ,
36- onSuccess : ( ) => reset ( ) ,
37- onError : ( errors ) => {
38- if ( errors . password ) {
39- reset ( 'password' , 'password_confirmation' ) ;
40- passwordInput . current ?. focus ( ) ;
41- }
42-
43- if ( errors . current_password ) {
44- reset ( 'current_password' ) ;
45- currentPasswordInput . current ?. focus ( ) ;
46- }
47- } ,
48- } ) ;
49- } ;
50-
5125 return (
5226 < AppLayout breadcrumbs = { breadcrumbs } >
5327 < Head title = "Password settings" />
@@ -56,71 +30,90 @@ export default function Password() {
5630 < div className = "space-y-6" >
5731 < HeadingSmall title = "Update password" description = "Ensure your account is using a long, random password to stay secure" />
5832
59- < form method = "POST" onSubmit = { updatePassword } className = "space-y-6" >
60- < div className = "grid gap-2" >
61- < Label htmlFor = "current_password" > Current password</ Label >
62-
63- < Input
64- id = "current_password"
65- ref = { currentPasswordInput }
66- value = { data . current_password }
67- onChange = { ( e ) => setData ( 'current_password' , e . target . value ) }
68- type = "password"
69- className = "mt-1 block w-full"
70- autoComplete = "current-password"
71- placeholder = "Current password"
72- />
73-
74- < InputError message = { errors . current_password } />
75- </ div >
76-
77- < div className = "grid gap-2" >
78- < Label htmlFor = "password" > New password</ Label >
79-
80- < Input
81- id = "password"
82- ref = { passwordInput }
83- value = { data . password }
84- onChange = { ( e ) => setData ( 'password' , e . target . value ) }
85- type = "password"
86- className = "mt-1 block w-full"
87- autoComplete = "new-password"
88- placeholder = "New password"
89- />
90-
91- < InputError message = { errors . password } />
92- </ div >
93-
94- < div className = "grid gap-2" >
95- < Label htmlFor = "password_confirmation" > Confirm password</ Label >
96-
97- < Input
98- id = "password_confirmation"
99- value = { data . password_confirmation }
100- onChange = { ( e ) => setData ( 'password_confirmation' , e . target . value ) }
101- type = "password"
102- className = "mt-1 block w-full"
103- autoComplete = "new-password"
104- placeholder = "Confirm password"
105- />
106-
107- < InputError message = { errors . password_confirmation } />
108- </ div >
109-
110- < div className = "flex items-center gap-4" >
111- < Button disabled = { processing } > Save password</ Button >
112-
113- < Transition
114- show = { recentlySuccessful }
115- enter = "transition ease-in-out"
116- enterFrom = "opacity-0"
117- leave = "transition ease-in-out"
118- leaveTo = "opacity-0"
119- >
120- < p className = "text-sm text-neutral-600" > Saved</ p >
121- </ Transition >
122- </ div >
123- </ form >
33+ < Form
34+ method = "put"
35+ action = { route ( 'password.update' ) }
36+ options = { {
37+ preserveScroll : true ,
38+ } }
39+ resetOnError = { [ 'password' , 'password_confirmation' , 'current_password' ] }
40+ resetOnSuccess
41+ onError = { ( errors ) => {
42+ if ( errors . password ) {
43+ passwordInput . current ?. focus ( ) ;
44+ }
45+
46+ if ( errors . current_password ) {
47+ currentPasswordInput . current ?. focus ( ) ;
48+ }
49+ } }
50+ className = "space-y-6"
51+ >
52+ { ( { errors, processing, recentlySuccessful } ) => (
53+ < >
54+ < div className = "grid gap-2" >
55+ < Label htmlFor = "current_password" > Current password</ Label >
56+
57+ < Input
58+ id = "current_password"
59+ ref = { currentPasswordInput }
60+ name = "current_password"
61+ type = "password"
62+ className = "mt-1 block w-full"
63+ autoComplete = "current-password"
64+ placeholder = "Current password"
65+ />
66+
67+ < InputError message = { errors . current_password } />
68+ </ div >
69+
70+ < div className = "grid gap-2" >
71+ < Label htmlFor = "password" > New password</ Label >
72+
73+ < Input
74+ id = "password"
75+ ref = { passwordInput }
76+ name = "password"
77+ type = "password"
78+ className = "mt-1 block w-full"
79+ autoComplete = "new-password"
80+ placeholder = "New password"
81+ />
82+
83+ < InputError message = { errors . password } />
84+ </ div >
85+
86+ < div className = "grid gap-2" >
87+ < Label htmlFor = "password_confirmation" > Confirm password</ Label >
88+
89+ < Input
90+ id = "password_confirmation"
91+ name = "password_confirmation"
92+ type = "password"
93+ className = "mt-1 block w-full"
94+ autoComplete = "new-password"
95+ placeholder = "Confirm password"
96+ />
97+
98+ < InputError message = { errors . password_confirmation } />
99+ </ div >
100+
101+ < div className = "flex items-center gap-4" >
102+ < Button disabled = { processing } > Save password</ Button >
103+
104+ < Transition
105+ show = { recentlySuccessful }
106+ enter = "transition ease-in-out"
107+ enterFrom = "opacity-0"
108+ leave = "transition ease-in-out"
109+ leaveTo = "opacity-0"
110+ >
111+ < p className = "text-sm text-neutral-600" > Saved</ p >
112+ </ Transition >
113+ </ div >
114+ </ >
115+ ) }
116+ </ Form >
124117 </ div >
125118 </ SettingsLayout >
126119 </ AppLayout >
0 commit comments