diff --git a/app/(dashboard)/projects/page.tsx b/app/(dashboard)/projects/page.tsx index 2c1d4cd..7006783 100644 --- a/app/(dashboard)/projects/page.tsx +++ b/app/(dashboard)/projects/page.tsx @@ -12,6 +12,14 @@ export default function ProjectsPage() { const [isCreateDialogOpen, setIsCreateDialogOpen] = useState(false); const { data: projects, isLoading, error } = useProjects(); + // Log state for debugging + console.log('[ProjectsPage] State:', { + isLoading, + error, + projects, + apiUrl: process.env.NEXT_PUBLIC_API_URL, + }); + if (isLoading) { return (
@@ -21,11 +29,44 @@ export default function ProjectsPage() { } if (error) { + const errorMessage = error instanceof Error ? error.message : 'Unknown error'; + const apiUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:5000/api/v1'; + + console.error('[ProjectsPage] Error loading projects:', error); + return (
-

- Failed to load projects. Please try again later. -

+ + + Failed to Load Projects + Unable to connect to the backend API + + +
+

Error Details:

+

{errorMessage}

+
+
+

API URL:

+

{apiUrl}

+
+
+

Troubleshooting Steps:

+
    +
  • Check if the backend server is running
  • +
  • Verify the API URL in .env.local
  • +
  • Check browser console (F12) for detailed errors
  • +
  • Check network tab (F12) for failed requests
  • +
+
+ +
+
); } diff --git a/lib/api/client.ts b/lib/api/client.ts index 3a265b2..662f6fb 100644 --- a/lib/api/client.ts +++ b/lib/api/client.ts @@ -1,5 +1,11 @@ const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:5000/api/v1'; +// Log API URL for debugging +if (typeof window !== 'undefined') { + console.log('[API Client] API_URL:', API_URL); + console.log('[API Client] NEXT_PUBLIC_API_URL:', process.env.NEXT_PUBLIC_API_URL); +} + export class ApiError extends Error { constructor( public status: number, @@ -14,11 +20,18 @@ export class ApiError extends Error { async function handleResponse(response: Response): Promise { if (!response.ok) { const errorData = await response.json().catch(() => ({})); - throw new ApiError( + const error = new ApiError( response.status, errorData.message || response.statusText, errorData ); + console.error('[API Client] Request failed:', { + url: response.url, + status: response.status, + statusText: response.statusText, + errorData, + }); + throw error; } if (response.status === 204) { @@ -34,6 +47,12 @@ export async function apiRequest( ): Promise { const url = `${API_URL}${endpoint}`; + console.log('[API Client] Request:', { + method: options.method || 'GET', + url, + endpoint, + }); + const headers: Record = { 'Content-Type': 'application/json', }; @@ -56,8 +75,23 @@ export async function apiRequest( headers, }; - const response = await fetch(url, config); - return handleResponse(response); + try { + const response = await fetch(url, config); + const result = await handleResponse(response); + console.log('[API Client] Response:', { + url, + status: response.status, + data: result, + }); + return result; + } catch (error) { + console.error('[API Client] Network error:', { + url, + error: error instanceof Error ? error.message : String(error), + errorObject: error, + }); + throw error; + } } export const api = { diff --git a/lib/hooks/use-projects.ts b/lib/hooks/use-projects.ts index 9ba1ed1..64f348e 100644 --- a/lib/hooks/use-projects.ts +++ b/lib/hooks/use-projects.ts @@ -5,8 +5,19 @@ import type { Project, CreateProjectDto, UpdateProjectDto } from '@/types/projec export function useProjects(page = 1, pageSize = 20) { return useQuery({ queryKey: ['projects', page, pageSize], - queryFn: () => projectsApi.getAll(page, pageSize), + queryFn: async () => { + console.log('[useProjects] Fetching projects...', { page, pageSize }); + try { + const result = await projectsApi.getAll(page, pageSize); + console.log('[useProjects] Fetch successful:', result); + return result; + } catch (error) { + console.error('[useProjects] Fetch failed:', error); + throw error; + } + }, staleTime: 5 * 60 * 1000, // 5 minutes + retry: 1, // Only retry once to fail faster }); }