feat: use REST API endpoint to delete current user
This commit is contained in:
@@ -1,31 +1,36 @@
|
||||
import PropTypes from 'prop-types';
|
||||
import * as React from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useMutation } from '@apollo/client';
|
||||
|
||||
import * as URLS from 'config/urls';
|
||||
import ConfirmationDialog from 'components/ConfirmationDialog';
|
||||
import apolloClient from 'graphql/client';
|
||||
import { DELETE_CURRENT_USER } from 'graphql/mutations/delete-current-user.ee';
|
||||
import useAuthentication from 'hooks/useAuthentication';
|
||||
import useFormatMessage from 'hooks/useFormatMessage';
|
||||
import useCurrentUser from 'hooks/useCurrentUser';
|
||||
import useDeleteCurrentUser from 'hooks/useDeleteCurrentUser';
|
||||
|
||||
function DeleteAccountDialog(props) {
|
||||
const [deleteCurrentUser] = useMutation(DELETE_CURRENT_USER);
|
||||
const formatMessage = useFormatMessage();
|
||||
const { data } = useCurrentUser();
|
||||
const currentUser = data?.data;
|
||||
|
||||
const { mutateAsync: deleteCurrentUser } = useDeleteCurrentUser(
|
||||
currentUser.id,
|
||||
);
|
||||
|
||||
const authentication = useAuthentication();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const handleConfirm = React.useCallback(async () => {
|
||||
await deleteCurrentUser();
|
||||
|
||||
authentication.removeToken();
|
||||
|
||||
await apolloClient.clearStore();
|
||||
|
||||
navigate(URLS.LOGIN);
|
||||
}, [deleteCurrentUser, currentUser]);
|
||||
}, [deleteCurrentUser, authentication, navigate]);
|
||||
|
||||
return (
|
||||
<ConfirmationDialog
|
||||
|
14
packages/web/src/hooks/useDeleteCurrentUser.js
Normal file
14
packages/web/src/hooks/useDeleteCurrentUser.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
import api from 'helpers/api';
|
||||
|
||||
export default function useDeleteCurrentUser(userId) {
|
||||
const query = useMutation({
|
||||
mutationFn: async () => {
|
||||
const { data } = await api.delete(`/v1/users/${userId}`);
|
||||
|
||||
return data;
|
||||
},
|
||||
});
|
||||
|
||||
return query;
|
||||
}
|
Reference in New Issue
Block a user