DeleteUserModal.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { useParams } from 'common'
  2. import { toast } from 'sonner'
  3. import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
  4. import { useUserDeleteMutation } from '@/data/auth/user-delete-mutation'
  5. import { User } from '@/data/auth/users-infinite-query'
  6. interface DeleteUserModalProps {
  7. visible: boolean
  8. selectedUser?: User
  9. onClose: () => void
  10. onDeleteSuccess?: () => void
  11. }
  12. export const DeleteUserModal = ({
  13. visible,
  14. selectedUser,
  15. onClose,
  16. onDeleteSuccess,
  17. }: DeleteUserModalProps) => {
  18. const { ref: projectRef } = useParams()
  19. const { mutate: deleteUser, isPending: isDeleting } = useUserDeleteMutation({
  20. onSuccess: () => {
  21. toast.success(`Successfully deleted ${selectedUser?.email}`)
  22. onDeleteSuccess?.()
  23. },
  24. })
  25. const handleDeleteUser = async () => {
  26. if (!projectRef) return console.error('Project ref is required')
  27. if (selectedUser?.id === undefined) {
  28. return toast.error(`Failed to delete user: User ID not found`)
  29. }
  30. deleteUser({ projectRef, userId: selectedUser.id })
  31. }
  32. return (
  33. <ConfirmationModal
  34. visible={visible}
  35. variant="destructive"
  36. title="Confirm to delete user"
  37. loading={isDeleting}
  38. confirmLabel="Delete"
  39. onCancel={() => onClose()}
  40. onConfirm={() => handleDeleteUser()}
  41. alert={{
  42. title: 'Deleting a user is irreversible',
  43. description:
  44. 'This will remove the selected the user from the project and all associated data.',
  45. }}
  46. >
  47. <p className="text-sm text-foreground-light">
  48. This is permanent! Are you sure you want to delete the user{' '}
  49. {selectedUser?.email ?? selectedUser?.phone ?? 'this user'}?
  50. </p>
  51. </ConfirmationModal>
  52. )
  53. }