Skip to content

Commit

Permalink
Merge pull request #1493 from target/delete-user-ui
Browse files Browse the repository at this point in the history
ui/users: user delete dialog
  • Loading branch information
dctalbot authored May 5, 2021
2 parents 5a4b4f2 + 191cff3 commit 0cc2089
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 1 deletion.
3 changes: 2 additions & 1 deletion web/src/app/users/UserContactMethodList.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { makeStyles, createStyles } from '@material-ui/core/styles'
import { styles as globalStyles } from '../styles/materialStyles'
import useWidth from '../util/useWidth'
import Spinner from '../loading/components/Spinner'
import { GenericError } from '../error-pages'
import { GenericError, ObjectNotFound } from '../error-pages'
import SendTestDialog from './SendTestDialog'

const query = gql`
Expand Down Expand Up @@ -63,6 +63,7 @@ export default function UserContactMethodList(props) {
})

if (loading && !data) return <Spinner />
if (!data.user) return <ObjectNotFound type='user' />
if (error) return <GenericError error={error.message} />

const contactMethods = data.user.contactMethods
Expand Down
81 changes: 81 additions & 0 deletions web/src/app/users/UserDeleteDialog.tsx
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
22 changes: 22 additions & 0 deletions web/src/app/users/UserDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import Spinner from '../loading/components/Spinner'
import { GenericError, ObjectNotFound } from '../error-pages'
import { useConfigValue, useSessionInfo } from '../util/RequireConfig'
import AppLink from '../util/AppLink'
import PageActions from '../util/PageActions'
import UserDeleteDialog from './UserDeleteDialog'
import OtherActions from '../util/OtherActions'

const userQuery = gql`
query userInfo($id: ID!) {
Expand Down Expand Up @@ -105,6 +108,7 @@ export default function UserDetails(props) {
const [createCM, setCreateCM] = useState(false)
const [createNR, setCreateNR] = useState(false)
const [showVerifyDialogByID, setShowVerifyDialogByID] = useState(null)
const [showUserDeleteDialog, setShowUserDeleteDialog] = useState(false)

const { data, loading: isQueryLoading, error } = useQuery(
isAdmin || props.userID === currentUserID ? profileQuery : userQuery,
Expand Down Expand Up @@ -153,6 +157,24 @@ export default function UserDetails(props) {

return (
<React.Fragment>
{isAdmin && (
<PageActions>
<OtherActions
actions={[
{
label: 'Delete User',
onClick: () => setShowUserDeleteDialog(true),
},
]}
/>
</PageActions>
)}
{showUserDeleteDialog && (
<UserDeleteDialog
userID={props.userID}
onClose={() => setShowUserDeleteDialog(false)}
/>
)}
{props.readOnly ? null : (
<SpeedDial
label='Add Items'
Expand Down
16 changes: 16 additions & 0 deletions web/src/cypress/integration/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ function testUsers(screen: ScreenFormat): void {
cy.get('ul').should('contain', prof.name)
})
})

describe('Page Actions', () => {
it('should delete a user', () => {
cy.adminLogin()

cy.fixture('users').then((users) => {
cy.visit(`/users/${users[0].id}`)

cy.pageAction('Delete')
cy.dialogTitle('Are you sure?')
cy.dialogFinish('Confirm')

cy.get('[data-cy=apollo-list]').should('not.contain', users[0].name)
})
})
})
}

testScreen('Users', testUsers)

0 comments on commit 0cc2089

Please sign in to comment.