UNPKG

1.46 kBJavaScriptView Raw
1import { useState } from 'react';
2import { useMutation } from '@apollo/react-hooks';
3import useAuth from '../components/AuthContext/useAuth';
4import t from '@jetshop/intl';
5import { deleteAccount } from '@jetshop/core/data/mutations/accountMutations.gql';
6const useDeleteAccountMutation = function useDeleteAccountMutation({ confirmationMessage }) {
7 const [mutate] = useMutation(deleteAccount);
8 const { logOut } = useAuth();
9 const [success, setSuccess] = useState(false);
10 const [error, setError] = useState();
11 function handleMutationError() {
12 setError(t('Something went wrong'));
13 }
14 function confirmDeletion() {
15 const confirmation = window.confirm(confirmationMessage || 'Are you sure you wish to delete your account?');
16 if (!confirmation) {
17 return;
18 }
19 mutate()
20 .then(r => {
21 // response is a boolean. If it is false, `something` went wrong… but we
22 // don't have an error from the API to display
23 if (r.data.deleteCustomer) {
24 setSuccess(true);
25 logOut();
26 }
27 else {
28 handleMutationError();
29 }
30 })
31 .catch(err => {
32 console.warn(err);
33 handleMutationError();
34 });
35 }
36 return { confirmDeletion, success, error };
37};
38export { useDeleteAccountMutation };
39//# sourceMappingURL=useDeleteAccountMutation.js.map
\No newline at end of file