Refactor: Complete API and cache improvements

This commit is contained in:
gpt-engineer-app[bot]
2025-10-31 12:22:06 +00:00
parent 0d16bb511c
commit 631ce9c89e
8 changed files with 219 additions and 254 deletions

View File

@@ -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", {

View File

@@ -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);
}

View File

@@ -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();
}