1 | import { useState } from 'react';
|
2 | import { useMutation } from '@apollo/react-hooks';
|
3 | import useAuth from '../components/AuthContext/useAuth';
|
4 | import t from '@jetshop/intl';
|
5 | import { deleteAccount } from '@jetshop/core/data/mutations/accountMutations.gql';
|
6 | const 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 |
|
22 |
|
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 | };
|
38 | export { useDeleteAccountMutation };
|
39 |
|
\ | No newline at end of file |