Files
flyer-crawler.projectium.com/src/hooks/mutations/useProfileMutations.ts
Torben Sorensen dcd9452b8c
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 13m46s
Adopt TanStack Query
2026-01-10 10:45:10 -08:00

180 lines
5.0 KiB
TypeScript

// 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);
if (!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);
if (!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);
if (!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);
if (!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();
if (!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);
if (!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');
},
});
};