mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-24 13:31:13 -05:00
feat: Implement API and cache improvements
This commit is contained in:
82
src/hooks/profile/useProfileLocationMutation.ts
Normal file
82
src/hooks/profile/useProfileLocationMutation.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
import { toast } from 'sonner';
|
||||
import { getErrorMessage } from '@/lib/errorHandler';
|
||||
import { useQueryInvalidation } from '@/lib/queryInvalidation';
|
||||
import { useAuth } from '@/hooks/useAuth';
|
||||
import type { LocationFormData } from '@/types/location';
|
||||
|
||||
/**
|
||||
* Hook for profile location mutations
|
||||
* Provides: location updates with automatic audit logging and cache invalidation
|
||||
*/
|
||||
export function useProfileLocationMutation() {
|
||||
const { user } = useAuth();
|
||||
const queryClient = useQueryClient();
|
||||
const {
|
||||
invalidateUserProfile,
|
||||
invalidateAuditLogs
|
||||
} = useQueryInvalidation();
|
||||
|
||||
const updateLocation = useMutation({
|
||||
mutationFn: async (data: LocationFormData) => {
|
||||
if (!user) throw new Error('Authentication required');
|
||||
|
||||
const previousProfile = {
|
||||
personal_location: data.personal_location,
|
||||
home_park_id: data.home_park_id,
|
||||
timezone: data.timezone,
|
||||
preferred_language: data.preferred_language,
|
||||
preferred_pronouns: data.preferred_pronouns
|
||||
};
|
||||
|
||||
const { error: profileError } = await supabase
|
||||
.from('profiles')
|
||||
.update({
|
||||
preferred_pronouns: data.preferred_pronouns || null,
|
||||
timezone: data.timezone,
|
||||
preferred_language: data.preferred_language,
|
||||
personal_location: data.personal_location || null,
|
||||
home_park_id: data.home_park_id || null,
|
||||
updated_at: new Date().toISOString()
|
||||
})
|
||||
.eq('user_id', user.id);
|
||||
|
||||
if (profileError) throw profileError;
|
||||
|
||||
// Log to audit trail
|
||||
await supabase.from('profile_audit_log').insert([{
|
||||
user_id: user.id,
|
||||
changed_by: user.id,
|
||||
action: 'location_info_updated',
|
||||
changes: JSON.parse(JSON.stringify({
|
||||
previous: { profile: previousProfile },
|
||||
updated: { profile: data },
|
||||
timestamp: new Date().toISOString()
|
||||
}))
|
||||
}]);
|
||||
|
||||
return data;
|
||||
},
|
||||
onError: (error: unknown) => {
|
||||
toast.error("Update Failed", {
|
||||
description: getErrorMessage(error),
|
||||
});
|
||||
},
|
||||
onSuccess: () => {
|
||||
if (user) {
|
||||
invalidateUserProfile(user.id);
|
||||
invalidateAuditLogs(user.id);
|
||||
}
|
||||
|
||||
toast.success("Settings Saved", {
|
||||
description: "Your location and personal information have been updated.",
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
updateLocation,
|
||||
isUpdating: updateLocation.isPending,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user