@@ -27,6 +27,7 @@ interface AddUserModalProps {
2727export function AddUserModal ( { open, onOpenChange, onCreated } : AddUserModalProps ) {
2828 const addUser = useAddUser ( )
2929 const submissionInFlightRef = useRef ( false )
30+ const [ isSubmitting , setIsSubmitting ] = useState ( false )
3031 const [ name , setName ] = useState ( '' )
3132 const [ email , setEmail ] = useState ( '' )
3233 const [ password , setPassword ] = useState ( '' )
@@ -41,11 +42,12 @@ export function AddUserModal({ open, onOpenChange, onCreated }: AddUserModalProp
4142 password . length > 0 && password . length < 8
4243 ? 'Password must be at least 8 characters'
4344 : undefined
45+ const isSubmissionPending = isSubmitting || addUser . isPending
4446 const canSubmit =
4547 normalizedName . length > 0 &&
4648 isValidEmailSyntax ( normalizedEmail ) &&
4749 password . length >= 8 &&
48- ! addUser . isPending
50+ ! isSubmissionPending
4951
5052 const reset = ( ) => {
5153 setName ( '' )
@@ -56,14 +58,15 @@ export function AddUserModal({ open, onOpenChange, onCreated }: AddUserModalProp
5658 }
5759
5860 const handleClose = ( ) => {
59- if ( submissionInFlightRef . current || addUser . isPending ) return
61+ if ( submissionInFlightRef . current || isSubmissionPending ) return
6062 reset ( )
6163 onOpenChange ( false )
6264 }
6365
6466 const handleAddUser = ( ) => {
6567 if ( ! canSubmit || submissionInFlightRef . current ) return
6668 submissionInFlightRef . current = true
69+ setIsSubmitting ( true )
6770 addUser . reset ( )
6871 addUser . mutate (
6972 {
@@ -80,6 +83,7 @@ export function AddUserModal({ open, onOpenChange, onCreated }: AddUserModalProp
8083 } ,
8184 onSettled : ( ) => {
8285 submissionInFlightRef . current = false
86+ setIsSubmitting ( false )
8387 } ,
8488 }
8589 )
@@ -93,7 +97,9 @@ export function AddUserModal({ open, onOpenChange, onCreated }: AddUserModalProp
9397 } }
9498 srTitle = 'Add user'
9599 >
96- < ChipModalHeader onClose = { handleClose } > Add user</ ChipModalHeader >
100+ < ChipModalHeader onClose = { handleClose } closeDisabled = { isSubmissionPending } >
101+ Add user
102+ </ ChipModalHeader >
97103 < ChipModalBody >
98104 < ChipModalField
99105 type = 'input'
@@ -107,7 +113,7 @@ export function AddUserModal({ open, onOpenChange, onCreated }: AddUserModalProp
107113 placeholder = 'Canary Writer'
108114 maxLength = { 100 }
109115 autoComplete = 'off'
110- disabled = { addUser . isPending }
116+ disabled = { isSubmissionPending }
111117 required
112118 />
113119 < ChipModalField
@@ -121,7 +127,7 @@ export function AddUserModal({ open, onOpenChange, onCreated }: AddUserModalProp
121127 error = { emailError }
122128 placeholder = 'writer@synthetics.example.com'
123129 autoComplete = 'off'
124- disabled = { addUser . isPending }
130+ disabled = { isSubmissionPending }
125131 required
126132 />
127133 < ChipModalField
@@ -137,7 +143,7 @@ export function AddUserModal({ open, onOpenChange, onCreated }: AddUserModalProp
137143 hint = 'Better Auth creates a credential account with this password.'
138144 placeholder = 'At least 8 characters'
139145 autoComplete = 'new-password'
140- disabled = { addUser . isPending }
146+ disabled = { isSubmissionPending }
141147 required
142148 />
143149 < ChipModalField
@@ -151,7 +157,7 @@ export function AddUserModal({ open, onOpenChange, onCreated }: AddUserModalProp
151157 options = { EMAIL_STATUS_OPTIONS }
152158 align = 'start'
153159 hint = 'Verified users can sign in when email verification is required.'
154- disabled = { addUser . isPending }
160+ disabled = { isSubmissionPending }
155161 required
156162 />
157163 < ChipModalError >
@@ -160,9 +166,9 @@ export function AddUserModal({ open, onOpenChange, onCreated }: AddUserModalProp
160166 </ ChipModalBody >
161167 < ChipModalFooter
162168 onCancel = { handleClose }
163- cancelDisabled = { addUser . isPending }
169+ cancelDisabled = { isSubmissionPending }
164170 primaryAction = { {
165- label : addUser . isPending ? 'Adding...' : 'Add user' ,
171+ label : isSubmissionPending ? 'Adding...' : 'Add user' ,
166172 onClick : handleAddUser ,
167173 disabled : ! canSubmit ,
168174 } }
0 commit comments