All files / src/hooks/mutations useProfileMutations.ts

62.96% Statements 34/54
27.77% Branches 10/36
66.66% Functions 16/24
70.83% Lines 34/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180                              1x 203x   7x   6x             6x     1x                           1x 203x   5x   3x             3x     2x                           1x 203x   1x   1x             1x                                 1x 203x   5x   4x             4x     1x                           1x 203x   1x   1x             1x                                 1x 203x   2x   1x             1x     1x        
// src/hooks/mutations/useProfileMutations.ts
import { useMutation } from '@tanstack/react-query';
import * as apiClient from '../../services/apiClient';
import { notifyError } from '../../services/notificationService';
import type { Profile, Address } from '../../types';
 
/**
 * Mutation hook for updating user profile.
 *
 * @example
 * ```tsx
 * const updateProfile = useUpdateProfileMutation();
 * updateProfile.mutate({ full_name: 'New Name', avatar_url: 'https://...' });
 * ```
 */
export const useUpdateProfileMutation = () => {
  return useMutation({
    mutationFn: async (data: Partial<Profile>): Promise<Profile> => {
      const response = await apiClient.updateUserProfile(data);
 
      Iif (!response.ok) {
        const error = await response.json().catch(() => ({
          message: `Request failed with status ${response.status}`,
        }));
        throw new Error(error.message || 'Failed to update profile');
      }
 
      return response.json();
    },
    onError: (error: Error) => {
      notifyError(error.message || 'Failed to update profile');
    },
  });
};
 
/**
 * Mutation hook for updating user address.
 *
 * @example
 * ```tsx
 * const updateAddress = useUpdateAddressMutation();
 * updateAddress.mutate({ street_address: '123 Main St', city: 'Toronto' });
 * ```
 */
export const useUpdateAddressMutation = () => {
  return useMutation({
    mutationFn: async (data: Partial<Address>): Promise<Address> => {
      const response = await apiClient.updateUserAddress(data);
 
      Iif (!response.ok) {
        const error = await response.json().catch(() => ({
          message: `Request failed with status ${response.status}`,
        }));
        throw new Error(error.message || 'Failed to update address');
      }
 
      return response.json();
    },
    onError: (error: Error) => {
      notifyError(error.message || 'Failed to update address');
    },
  });
};
 
/**
 * Mutation hook for updating user password.
 *
 * @example
 * ```tsx
 * const updatePassword = useUpdatePasswordMutation();
 * updatePassword.mutate({ password: 'newPassword123' });
 * ```
 */
export const useUpdatePasswordMutation = () => {
  return useMutation({
    mutationFn: async ({ password }: { password: string }): Promise<void> => {
      const response = await apiClient.updateUserPassword(password);
 
      Iif (!response.ok) {
        const error = await response.json().catch(() => ({
          message: `Request failed with status ${response.status}`,
        }));
        throw new Error(error.message || 'Failed to update password');
      }
 
      return response.json();
    },
    onError: (error: Error) => {
      notifyError(error.message || 'Failed to update password');
    },
  });
};
 
/**
 * Mutation hook for updating user preferences.
 *
 * @example
 * ```tsx
 * const updatePreferences = useUpdatePreferencesMutation();
 * updatePreferences.mutate({ darkMode: true });
 * ```
 */
export const useUpdatePreferencesMutation = () => {
  return useMutation({
    mutationFn: async (prefs: Partial<Profile['preferences']>): Promise<Profile> => {
      const response = await apiClient.updateUserPreferences(prefs);
 
      Iif (!response.ok) {
        const error = await response.json().catch(() => ({
          message: `Request failed with status ${response.status}`,
        }));
        throw new Error(error.message || 'Failed to update preferences');
      }
 
      return response.json();
    },
    onError: (error: Error) => {
      notifyError(error.message || 'Failed to update preferences');
    },
  });
};
 
/**
 * Mutation hook for exporting user data.
 *
 * @example
 * ```tsx
 * const exportData = useExportDataMutation();
 * exportData.mutate();
 * ```
 */
export const useExportDataMutation = () => {
  return useMutation({
    mutationFn: async (): Promise<unknown> => {
      const response = await apiClient.exportUserData();
 
      Iif (!response.ok) {
        const error = await response.json().catch(() => ({
          message: `Request failed with status ${response.status}`,
        }));
        throw new Error(error.message || 'Failed to export data');
      }
 
      return response.json();
    },
    onError: (error: Error) => {
      notifyError(error.message || 'Failed to export data');
    },
  });
};
 
/**
 * Mutation hook for deleting user account.
 *
 * @example
 * ```tsx
 * const deleteAccount = useDeleteAccountMutation();
 * deleteAccount.mutate({ password: 'currentPassword' });
 * ```
 */
export const useDeleteAccountMutation = () => {
  return useMutation({
    mutationFn: async ({ password }: { password: string }): Promise<void> => {
      const response = await apiClient.deleteUserAccount(password);
 
      Iif (!response.ok) {
        const error = await response.json().catch(() => ({
          message: `Request failed with status ${response.status}`,
        }));
        throw new Error(error.message || 'Failed to delete account');
      }
 
      return response.json();
    },
    onError: (error: Error) => {
      notifyError(error.message || 'Failed to delete account');
    },
  });
};