-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A4A: Implement the 'Remove member' action. (#94102)
* Move handle member action to a hook. * Add handler for 'Remove member' action. * Fix success message. * Temporarily disable password reset. * Address PR comments.
- Loading branch information
1 parent
b5b89c2
commit 8b21a6e
Showing
4 changed files
with
122 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useMutation, UseMutationOptions, UseMutationResult } from '@tanstack/react-query'; | ||
import wpcom from 'calypso/lib/wp'; | ||
import { useSelector } from 'calypso/state'; | ||
import { getActiveAgencyId } from 'calypso/state/a8c-for-agencies/agency/selectors'; | ||
|
||
interface APIError { | ||
status: number; | ||
code: string | null; | ||
message: string; | ||
} | ||
|
||
export interface Params { | ||
id: number; | ||
} | ||
|
||
interface APIResponse { | ||
success: boolean; | ||
} | ||
|
||
function removeMemberMutation( params: Params, agencyId?: number ): Promise< APIResponse > { | ||
if ( ! agencyId ) { | ||
throw new Error( 'Agency ID is required to remove a team member' ); | ||
} | ||
|
||
return wpcom.req.post( { | ||
apiNamespace: 'wpcom/v2', | ||
path: `/agency/${ agencyId }/users/${ params.id }`, | ||
method: 'DELETE', | ||
} ); | ||
} | ||
|
||
export default function useRemoveMemberMutation< TContext = unknown >( | ||
options?: UseMutationOptions< APIResponse, APIError, Params, TContext > | ||
): UseMutationResult< APIResponse, APIError, Params, TContext > { | ||
const agencyId = useSelector( getActiveAgencyId ); | ||
|
||
return useMutation< APIResponse, APIError, Params, TContext >( { | ||
...options, | ||
mutationFn: ( args ) => removeMemberMutation( args, agencyId ), | ||
} ); | ||
} |
77 changes: 77 additions & 0 deletions
77
client/a8c-for-agencies/sections/team/hooks/use-handle-member-action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { useTranslate } from 'i18n-calypso'; | ||
import { useCallback } from 'react'; | ||
import useCancelMemberInviteMutation from 'calypso/a8c-for-agencies/data/team/use-cancel-member-invite'; | ||
import useRemoveMemberMutation from 'calypso/a8c-for-agencies/data/team/use-remove-member'; | ||
import { useDispatch } from 'calypso/state'; | ||
import { errorNotice, successNotice } from 'calypso/state/notices/actions'; | ||
import { TeamMember } from '../types'; | ||
|
||
type Props = { | ||
onRefetchList?: () => void; | ||
}; | ||
|
||
export default function useHandleMemberAction( { onRefetchList }: Props ) { | ||
const translate = useTranslate(); | ||
const dispatch = useDispatch(); | ||
|
||
const { mutate: cancelMemberInvite } = useCancelMemberInviteMutation(); | ||
|
||
const { mutate: removeMember } = useRemoveMemberMutation(); | ||
|
||
return useCallback( | ||
( action: string, item: TeamMember ) => { | ||
if ( action === 'cancel-user-invite' ) { | ||
cancelMemberInvite( | ||
{ id: item.id }, | ||
{ | ||
onSuccess: () => { | ||
dispatch( | ||
successNotice( translate( 'The invitation has been successfully cancelled.' ), { | ||
id: 'cancel-user-invite-success', | ||
duration: 5000, | ||
} ) | ||
); | ||
onRefetchList?.(); | ||
}, | ||
|
||
onError: ( error ) => { | ||
dispatch( | ||
errorNotice( error.message, { | ||
id: 'cancel-user-invite-error', | ||
duration: 5000, | ||
} ) | ||
); | ||
}, | ||
} | ||
); | ||
} | ||
|
||
if ( action === 'delete-user' ) { | ||
removeMember( | ||
{ id: item.id }, | ||
{ | ||
onSuccess: () => { | ||
dispatch( | ||
successNotice( translate( 'The member has been successfully removed.' ), { | ||
id: 'remove-user-success', | ||
duration: 5000, | ||
} ) | ||
); | ||
onRefetchList?.(); | ||
}, | ||
|
||
onError: ( error ) => { | ||
dispatch( | ||
errorNotice( error.message, { | ||
id: 'remove-user-error', | ||
duration: 5000, | ||
} ) | ||
); | ||
}, | ||
} | ||
); | ||
} | ||
}, | ||
[ cancelMemberInvite, dispatch, onRefetchList, removeMember, translate ] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters