@@ -30,8 +30,7 @@ import {
3030import type { AuthRole , CreateInvitationInput , CurrentUserDto } from '@acme/shared' ;
3131
3232import { authClient } from '@/lib/auth-client' ;
33- import { ApiClientError } from '@/lib/api-client' ;
34- import { useCreateInvitationMutation , useUsersWorkspaceQuery } from '@/lib/queries' ;
33+ import { useUsersWorkspaceQuery } from '@/lib/queries' ;
3534
3635const getErrorMessage = ( error : unknown ) =>
3736 error instanceof Error ? error . message : 'Unable to complete the request' ;
@@ -77,43 +76,47 @@ export function UsersWorkspace({
7776 const [ notice , setNotice ] = useState < string | null > ( null ) ;
7877 const [ setupError , setSetupError ] = useState < string | null > ( null ) ;
7978 const [ isProvisioning , startProvisioning ] = useTransition ( ) ;
79+ const [ isInviting , setIsInviting ] = useState ( false ) ;
8080 const workspaceQuery = useUsersWorkspaceQuery ( ) ;
81- const createInvitationMutation = useCreateInvitationMutation ( ) ;
8281
8382 const workspace = workspaceQuery . data ;
8483 const effectiveViewer = workspace ?. viewer ?? viewer ;
8584 const members = workspace ?. members ?? [ ] ;
8685 const invitations = workspace ?. invitations ?? [ ] ;
8786 const canInviteMembers = canManageMembers ( effectiveViewer . role ) ;
88- const errorMessage = createInvitationMutation . isError
89- ? getErrorMessage ( createInvitationMutation . error )
90- : workspaceQuery . isError
91- ? getErrorMessage ( workspaceQuery . error )
92- : null ;
87+ const errorMessage = workspaceQuery . isError ? getErrorMessage ( workspaceQuery . error ) : null ;
9388
9489 const handleSubmit = async ( event : React . FormEvent < HTMLFormElement > ) => {
9590 event . preventDefault ( ) ;
9691 setNotice ( null ) ;
92+ setSetupError ( null ) ;
9793 const submittedInvite = { ...inviteForm } ;
9894
9995 try {
100- await createInvitationMutation . mutateAsync ( submittedInvite ) ;
96+ setIsInviting ( true ) ;
97+ const invitationResponse = ( await authClient . organization . inviteMember ( {
98+ email : submittedInvite . email ,
99+ role : submittedInvite . role ,
100+ organizationId : effectiveViewer . organization ?. id ,
101+ resend : true ,
102+ } ) ) as {
103+ error ?: {
104+ message ?: string ;
105+ } | null ;
106+ } ;
107+
108+ if ( invitationResponse . error ) {
109+ setSetupError ( invitationResponse . error . message ?? 'Unable to send invitation' ) ;
110+ return ;
111+ }
112+
101113 setInviteForm ( { email : '' , role : 'member' } ) ;
102114 setNotice ( `Invitation queued for ${ submittedInvite . email } ` ) ;
115+ await workspaceQuery . refetch ( ) ;
103116 } catch ( error ) {
104- if ( error instanceof ApiClientError && error . code === 'REQUEST_TIMEOUT' ) {
105- const refreshedWorkspace = await workspaceQuery . refetch ( ) ;
106- const invitationWasCreated = refreshedWorkspace . data ?. invitations . some (
107- ( invitation ) => invitation . email === submittedInvite . email ,
108- ) ;
109-
110- if ( invitationWasCreated ) {
111- setInviteForm ( { email : '' , role : 'member' } ) ;
112- setNotice (
113- `Invitation queued for ${ submittedInvite . email } . Delivery took longer than the browser wait, but the pending invite was created successfully.` ,
114- ) ;
115- }
116- }
117+ setSetupError ( getErrorMessage ( error ) ) ;
118+ } finally {
119+ setIsInviting ( false ) ;
117120 }
118121 } ;
119122
@@ -270,11 +273,9 @@ export function UsersWorkspace({
270273 < Button
271274 type = "submit"
272275 className = "w-full"
273- disabled = { createInvitationMutation . isPending }
276+ disabled = { isInviting }
274277 >
275- { createInvitationMutation . isPending
276- ? 'Sending invitation...'
277- : 'Send invitation' }
278+ { isInviting ? 'Sending invitation...' : 'Send invitation' }
278279 </ Button >
279280 </ form >
280281 { notice ? (
@@ -283,10 +284,10 @@ export function UsersWorkspace({
283284 < AlertDescription > { notice } </ AlertDescription >
284285 </ Alert >
285286 ) : null }
286- { errorMessage ? (
287+ { setupError ?? errorMessage ? (
287288 < Alert variant = "destructive" >
288289 < AlertTitle > Unable to send invitation</ AlertTitle >
289- < AlertDescription > { errorMessage } </ AlertDescription >
290+ < AlertDescription > { setupError ?? errorMessage } </ AlertDescription >
290291 </ Alert >
291292 ) : null }
292293 </ >
0 commit comments