UNPKG

@ludiks/react

Version:

Complete React library for Ludiks gamification platform - includes SDK and ready-to-use components

43 lines (42 loc) 1.34 kB
import { useState, useEffect } from 'react'; import { getUserProfile } from '../utils/ludiks-client'; export function useUserProfile({ userId, autoRefresh = false, refreshInterval = 30000, }) { const [profile, setProfile] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const fetchProfile = async () => { if (!userId) { setError('User ID is required'); setLoading(false); return; } try { setLoading(true); setError(null); const profileData = await getUserProfile(userId); setProfile(profileData); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to fetch user profile'); setProfile(null); } finally { setLoading(false); } }; useEffect(() => { fetchProfile(); }, [userId]); useEffect(() => { if (autoRefresh && refreshInterval > 0 && userId) { const interval = setInterval(fetchProfile, refreshInterval); return () => clearInterval(interval); } }, [autoRefresh, refreshInterval, userId]); return { profile, loading, error, refetch: fetchProfile, }; }