mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 16:11:12 -05:00
Refactor: Complete API and cache improvements
This commit is contained in:
@@ -62,7 +62,30 @@ export function usePrivacyMutations() {
|
||||
|
||||
return { privacySettings };
|
||||
},
|
||||
onError: (error: unknown) => {
|
||||
onMutate: async (newData) => {
|
||||
// Cancel outgoing queries
|
||||
await queryClient.cancelQueries({ queryKey: ['profile', user?.id] });
|
||||
|
||||
// Snapshot current value
|
||||
const previousProfile = queryClient.getQueryData(['profile', user?.id]);
|
||||
|
||||
// Optimistically update cache
|
||||
if (previousProfile) {
|
||||
queryClient.setQueryData(['profile', user?.id], (old: any) => ({
|
||||
...old,
|
||||
privacy_level: newData.privacy_level,
|
||||
show_pronouns: newData.show_pronouns,
|
||||
}));
|
||||
}
|
||||
|
||||
return { previousProfile };
|
||||
},
|
||||
onError: (error: unknown, _variables, context) => {
|
||||
// Rollback on error
|
||||
if (context?.previousProfile && user) {
|
||||
queryClient.setQueryData(['profile', user.id], context.previousProfile);
|
||||
}
|
||||
|
||||
toast.error("Update Failed", {
|
||||
description: getErrorMessage(error),
|
||||
});
|
||||
@@ -72,11 +95,7 @@ export function usePrivacyMutations() {
|
||||
if (user) {
|
||||
invalidateUserProfile(user.id);
|
||||
invalidateAuditLogs(user.id);
|
||||
|
||||
// If privacy level changed, invalidate user search
|
||||
if (variables.privacy_level) {
|
||||
invalidateUserSearch();
|
||||
}
|
||||
invalidateUserSearch(); // Privacy affects search visibility
|
||||
}
|
||||
|
||||
toast.success("Privacy Updated", {
|
||||
|
||||
@@ -14,7 +14,8 @@ export function useProfileLocationMutation() {
|
||||
const { user } = useAuth();
|
||||
const queryClient = useQueryClient();
|
||||
const {
|
||||
invalidateUserProfile,
|
||||
invalidateUserProfile,
|
||||
invalidateProfileStats,
|
||||
invalidateAuditLogs
|
||||
} = useQueryInvalidation();
|
||||
|
||||
@@ -58,7 +59,33 @@ export function useProfileLocationMutation() {
|
||||
|
||||
return data;
|
||||
},
|
||||
onError: (error: unknown) => {
|
||||
onMutate: async (newData) => {
|
||||
// Cancel outgoing queries
|
||||
await queryClient.cancelQueries({ queryKey: ['profile', user?.id] });
|
||||
|
||||
// Snapshot current value
|
||||
const previousProfile = queryClient.getQueryData(['profile', user?.id]);
|
||||
|
||||
// Optimistically update cache
|
||||
if (previousProfile) {
|
||||
queryClient.setQueryData(['profile', user?.id], (old: any) => ({
|
||||
...old,
|
||||
personal_location: newData.personal_location,
|
||||
home_park_id: newData.home_park_id,
|
||||
timezone: newData.timezone,
|
||||
preferred_language: newData.preferred_language,
|
||||
preferred_pronouns: newData.preferred_pronouns,
|
||||
}));
|
||||
}
|
||||
|
||||
return { previousProfile };
|
||||
},
|
||||
onError: (error: unknown, _variables, context) => {
|
||||
// Rollback on error
|
||||
if (context?.previousProfile && user) {
|
||||
queryClient.setQueryData(['profile', user.id], context.previousProfile);
|
||||
}
|
||||
|
||||
toast.error("Update Failed", {
|
||||
description: getErrorMessage(error),
|
||||
});
|
||||
@@ -66,6 +93,7 @@ export function useProfileLocationMutation() {
|
||||
onSuccess: () => {
|
||||
if (user) {
|
||||
invalidateUserProfile(user.id);
|
||||
invalidateProfileStats(user.id); // Location affects stats display
|
||||
invalidateAuditLogs(user.id);
|
||||
}
|
||||
|
||||
|
||||
@@ -64,8 +64,8 @@ export function useProfileUpdateMutation() {
|
||||
invalidateProfileStats(userId);
|
||||
invalidateProfileActivity(userId);
|
||||
|
||||
// If display name changed, invalidate user search results
|
||||
if (updates.display_name) {
|
||||
// If display name or username changed, invalidate user search results
|
||||
if (updates.display_name || updates.username) {
|
||||
invalidateUserSearch();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user