-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1493 from target/delete-user-ui
ui/users: user delete dialog
- Loading branch information
Showing
4 changed files
with
121 additions
and
1 deletion.
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
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,81 @@ | ||
import React from 'react' | ||
import { gql, useQuery, useMutation } from '@apollo/client' | ||
import { useHistory } from 'react-router' | ||
import Spinner from '../loading/components/Spinner' | ||
import FormDialog from '../dialogs/FormDialog' | ||
import { useSessionInfo } from '../util/RequireConfig' | ||
import { GenericError } from '../error-pages' | ||
|
||
const query = gql` | ||
query($id: ID!) { | ||
user(id: $id) { | ||
id | ||
name | ||
} | ||
} | ||
` | ||
const mutation = gql` | ||
mutation($input: [TargetInput!]!) { | ||
deleteAll(input: $input) | ||
} | ||
` | ||
|
||
interface RotationDeleteDialogProps { | ||
userID: string | ||
onClose: () => void | ||
} | ||
|
||
function UserDeleteDialog(props: RotationDeleteDialogProps): JSX.Element { | ||
const { userID: currentUserID, ready: isSessionReady } = useSessionInfo() | ||
const history = useHistory() | ||
|
||
const { data, loading: qLoading, error: qError } = useQuery(query, { | ||
variables: { id: props.userID }, | ||
}) | ||
|
||
const [deleteUser, { loading: mLoading, error: mError }] = useMutation( | ||
mutation, | ||
{ | ||
variables: { | ||
input: [ | ||
{ | ||
id: props.userID, | ||
type: 'user', | ||
}, | ||
], | ||
}, | ||
onCompleted: ({ deleteAll }) => { | ||
if (deleteAll) return history.push('/users') | ||
}, | ||
}, | ||
) | ||
|
||
if (!isSessionReady || (!data && qLoading)) return <Spinner /> | ||
if (qError) return <GenericError error={qError.message} /> | ||
|
||
return ( | ||
<FormDialog | ||
title='Are you sure?' | ||
confirm | ||
subTitle={`This will delete the user: ${data?.user?.name}`} | ||
loading={mLoading} | ||
errors={mError ? [mError] : []} | ||
onClose={props.onClose} | ||
onSubmit={() => deleteUser()} | ||
notices={ | ||
props.userID === currentUserID | ||
? [ | ||
{ | ||
type: 'WARNING', | ||
message: 'You are about to delete your own user account', | ||
details: | ||
'You will be logged out immediately and unable to log back in', | ||
}, | ||
] | ||
: [] | ||
} | ||
/> | ||
) | ||
} | ||
|
||
export default UserDeleteDialog |
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