This commit is contained in:
@@ -1,51 +1,43 @@
|
||||
// src/hooks/useUserProfileData.ts
|
||||
import { useState, useEffect } from 'react';
|
||||
import * as apiClient from '../services/apiClient';
|
||||
import { UserProfile, Achievement, UserAchievement } from '../types';
|
||||
import { logger } from '../services/logger.client';
|
||||
import { useCallback } from 'react';
|
||||
import { useQueryClient } from '@tanstack/react-query';
|
||||
import { useUserProfileDataQuery } from './queries/useUserProfileDataQuery';
|
||||
import type { UserProfile } from '../types';
|
||||
|
||||
/**
|
||||
* A custom hook to access the authenticated user's profile and achievements.
|
||||
*
|
||||
* Refactored to use TanStack Query (ADR-0005 Phase 8).
|
||||
*
|
||||
* @returns An object containing profile, achievements, loading state, error, and setProfile function.
|
||||
*/
|
||||
export const useUserProfileData = () => {
|
||||
const [profile, setProfile] = useState<UserProfile | null>(null);
|
||||
const [achievements, setAchievements] = useState<(UserAchievement & Achievement)[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const queryClient = useQueryClient();
|
||||
const { data, isLoading, error } = useUserProfileDataQuery();
|
||||
|
||||
useEffect(() => {
|
||||
const fetchData = async () => {
|
||||
setIsLoading(true);
|
||||
try {
|
||||
const [profileRes, achievementsRes] = await Promise.all([
|
||||
apiClient.getAuthenticatedUserProfile(),
|
||||
apiClient.getUserAchievements(),
|
||||
]);
|
||||
// Provide a setProfile function for backward compatibility
|
||||
// This updates the query cache directly
|
||||
const setProfile = useCallback(
|
||||
(updater: UserProfile | ((prev: UserProfile | null) => UserProfile | null)) => {
|
||||
queryClient.setQueryData(['user-profile-data'], (oldData: typeof data) => {
|
||||
if (!oldData) return oldData;
|
||||
|
||||
if (!profileRes.ok) throw new Error('Failed to fetch user profile.');
|
||||
if (!achievementsRes.ok) throw new Error('Failed to fetch user achievements.');
|
||||
const newProfile = typeof updater === 'function' ? updater(oldData.profile) : updater;
|
||||
|
||||
const profileData: UserProfile | null = await profileRes.json();
|
||||
const achievementsData: (UserAchievement & Achievement)[] | null =
|
||||
await achievementsRes.json();
|
||||
return {
|
||||
...oldData,
|
||||
profile: newProfile,
|
||||
};
|
||||
});
|
||||
},
|
||||
[queryClient],
|
||||
);
|
||||
|
||||
logger.info(
|
||||
{ profileData, achievementsCount: achievementsData?.length },
|
||||
'useUserProfileData: Fetched data',
|
||||
);
|
||||
|
||||
if (profileData) {
|
||||
setProfile(profileData);
|
||||
}
|
||||
setAchievements(achievementsData || []);
|
||||
} catch (err) {
|
||||
const errorMessage = err instanceof Error ? err.message : 'An unknown error occurred.';
|
||||
setError(errorMessage);
|
||||
logger.error({ err }, 'Error in useUserProfileData:');
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchData();
|
||||
}, []);
|
||||
|
||||
return { profile, setProfile, achievements, isLoading, error };
|
||||
};
|
||||
return {
|
||||
profile: data?.profile ?? null,
|
||||
setProfile,
|
||||
achievements: data?.achievements ?? [],
|
||||
isLoading,
|
||||
error: error?.message ?? null,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user