- Introduced `useUserProfile` hook to centralize user profile fetching and management. - Updated various components to utilize the new `useUserProfile` hook instead of context. - Enhanced token validation logic in `Fetch` function to prevent unnecessary redirects. - Added `parseDueDate` function to handle due date parsing with improved logic. - Cleaned up user profile state management across multiple views and settings. - Improved loading states and error handling in user-related components. - remove usercontext and just use react query for userProfile
54 lines
1.3 KiB
JavaScript
54 lines
1.3 KiB
JavaScript
import { useQuery, useQueryClient } from '@tanstack/react-query'
|
|
import {
|
|
GetAllCircleMembers,
|
|
GetAllUsers,
|
|
GetUserProfile,
|
|
} from '../utils/Fetcher'
|
|
import { isTokenValid } from '../utils/TokenManager'
|
|
|
|
export const useAllUsers = () => {
|
|
return useQuery({
|
|
queryKey: ['allUsers'],
|
|
queryFn: GetAllUsers,
|
|
})
|
|
}
|
|
|
|
export const useCircleMembers = () => {
|
|
const queryClient = useQueryClient()
|
|
|
|
const { data, error, isLoading } = useQuery({
|
|
queryKey: ['allCircleMembers'],
|
|
queryFn: GetAllCircleMembers,
|
|
})
|
|
|
|
const handleRefetch = () => {
|
|
queryClient.invalidateQueries(['allCircleMembers'])
|
|
}
|
|
|
|
return { data, error, isLoading, handleRefetch }
|
|
}
|
|
|
|
export const useUserProfile = () => {
|
|
const queryClient = useQueryClient()
|
|
|
|
const { data, error, isLoading } = useQuery({
|
|
queryKey: ['userProfile'],
|
|
queryFn: async () => {
|
|
const resp = await GetUserProfile()
|
|
const result = await resp.json()
|
|
if (!isTokenValid()) {
|
|
return null // Token is invalid, return null to indicate no profile
|
|
}
|
|
return result.res // Return the actual user profile data
|
|
},
|
|
staleTime: 30 * 60 * 1000, // 30 minutes in milliseconds
|
|
gcTime: 30 * 60 * 1000, // 30 minutes in milliseconds
|
|
})
|
|
return {
|
|
data,
|
|
error,
|
|
isLoading,
|
|
refetch: () => queryClient.invalidateQueries(['userProfile']),
|
|
}
|
|
}
|