From 34474cc275fa7730f72dd8054da0e9e5a24ad4aa Mon Sep 17 00:00:00 2001 From: soufiane Date: Tue, 18 Nov 2025 15:07:49 +0100 Subject: [PATCH] fix: use API_BASE_URL constant instead of direct env access MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace all instances of process.env.NEXT_PUBLIC_API_URL with API_BASE_URL constant for consistency and to ensure the API URL is properly defined at runtime. Files updated: - app/admin/marketing-data/page.tsx - app/admin/tirages/page.tsx - app/employe/dashboard/page.tsx - app/employe/gains-client/page.tsx - app/employe/historique/page.tsx - components/admin/TicketManagement.tsx This fixes the "undefined" API URL issue that was preventing API calls from working correctly. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- app/admin/marketing-data/page.tsx | 7 ++++--- app/admin/tirages/page.tsx | 15 ++++++++------- app/employe/dashboard/page.tsx | 6 +++--- app/employe/gains-client/page.tsx | 5 +++-- app/employe/historique/page.tsx | 3 ++- components/admin/TicketManagement.tsx | 3 ++- 6 files changed, 22 insertions(+), 17 deletions(-) diff --git a/app/admin/marketing-data/page.tsx b/app/admin/marketing-data/page.tsx index 276cb16..2bf2bfd 100644 --- a/app/admin/marketing-data/page.tsx +++ b/app/admin/marketing-data/page.tsx @@ -4,6 +4,7 @@ import { useState, useEffect } from 'react'; import { Card } from '@/components/ui/Card'; import Button from '@/components/Button'; import toast from 'react-hot-toast'; +import { API_BASE_URL } from '@/utils/constants'; import { Mail, Download, @@ -66,11 +67,11 @@ export default function MarketingPage() { const token = localStorage.getItem('auth_token') || localStorage.getItem('token'); console.log('Loading marketing stats...'); - console.log('API URL:', process.env.NEXT_PUBLIC_API_URL); + console.log('API URL:', API_BASE_URL); console.log('Token present:', !!token); const response = await fetch( - `${process.env.NEXT_PUBLIC_API_URL}/admin/marketing/stats`, + `${API_BASE_URL}/admin/marketing/stats`, { headers: { 'Content-Type': 'application/json', @@ -124,7 +125,7 @@ export default function MarketingPage() { } const response = await fetch( - `${process.env.NEXT_PUBLIC_API_URL}/admin/marketing/export`, + `${API_BASE_URL}/admin/marketing/export`, { method: 'POST', headers: { diff --git a/app/admin/tirages/page.tsx b/app/admin/tirages/page.tsx index 59ba96b..ffc847d 100644 --- a/app/admin/tirages/page.tsx +++ b/app/admin/tirages/page.tsx @@ -4,6 +4,7 @@ import { useState, useEffect } from 'react'; import { Card } from '@/components/ui/Card'; import Button from '@/components/Button'; import toast from 'react-hot-toast'; +import { API_BASE_URL } from '@/utils/constants'; import { Trophy, Users, @@ -87,7 +88,7 @@ export default function TiragesPage() { const checkExistingDraw = async () => { try { const token = localStorage.getItem('auth_token') || localStorage.getItem('token'); - const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/draw/check-existing`, { + const response = await fetch(`${API_BASE_URL}/draw/check-existing`, { headers: { Authorization: `Bearer ${token}`, }, @@ -108,7 +109,7 @@ export default function TiragesPage() { try { const token = localStorage.getItem('auth_token') || localStorage.getItem('token'); const response = await fetch( - `${process.env.NEXT_PUBLIC_API_URL}/draw/eligible-participants?minTickets=${minTickets}&verified=${verifiedOnly}`, + `${API_BASE_URL}/draw/eligible-participants?minTickets=${minTickets}&verified=${verifiedOnly}`, { headers: { Authorization: `Bearer ${token}`, @@ -144,7 +145,7 @@ export default function TiragesPage() { setLoading(true); try { const token = localStorage.getItem('auth_token') || localStorage.getItem('token'); - const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/draw/conduct`, { + const response = await fetch(`${API_BASE_URL}/draw/conduct`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -184,7 +185,7 @@ export default function TiragesPage() { try { const token = localStorage.getItem('auth_token') || localStorage.getItem('token'); const response = await fetch( - `${process.env.NEXT_PUBLIC_API_URL}/draw/${existingDraw.id}/report`, + `${API_BASE_URL}/draw/${existingDraw.id}/report`, { headers: { Authorization: `Bearer ${token}`, @@ -267,7 +268,7 @@ ${report.draw.notifiedAt ? `📧 Gagnant notifié le: ${new Date(report.draw.not try { const token = localStorage.getItem('auth_token') || localStorage.getItem('token'); const response = await fetch( - `${process.env.NEXT_PUBLIC_API_URL}/draw/${existingDraw.id}/notify`, + `${API_BASE_URL}/draw/${existingDraw.id}/notify`, { method: 'PUT', headers: { @@ -293,7 +294,7 @@ ${report.draw.notifiedAt ? `📧 Gagnant notifié le: ${new Date(report.draw.not try { const token = localStorage.getItem('auth_token') || localStorage.getItem('token'); const response = await fetch( - `${process.env.NEXT_PUBLIC_API_URL}/draw/${existingDraw.id}/claim`, + `${API_BASE_URL}/draw/${existingDraw.id}/claim`, { method: 'PUT', headers: { @@ -326,7 +327,7 @@ ${report.draw.notifiedAt ? `📧 Gagnant notifié le: ${new Date(report.draw.not try { const token = localStorage.getItem('auth_token') || localStorage.getItem('token'); const response = await fetch( - `${process.env.NEXT_PUBLIC_API_URL}/draw/${existingDraw.id}`, + `${API_BASE_URL}/draw/${existingDraw.id}`, { method: 'DELETE', headers: { diff --git a/app/employe/dashboard/page.tsx b/app/employe/dashboard/page.tsx index db9526c..8ee0b76 100644 --- a/app/employe/dashboard/page.tsx +++ b/app/employe/dashboard/page.tsx @@ -5,7 +5,7 @@ import { useRouter } from 'next/navigation'; import { useAuth } from '@/hooks'; import { Card } from '@/components/ui'; import { Loading } from '@/components/ui/Loading'; -import { ROUTES } from '@/utils/constants'; +import { ROUTES, API_BASE_URL } from '@/utils/constants'; import toast from 'react-hot-toast'; import Link from 'next/link'; @@ -56,7 +56,7 @@ export default function EmployeDashboardPage() { setLoadingTickets(true); const token = localStorage.getItem('auth_token') || localStorage.getItem('token'); const response = await fetch( - `${process.env.NEXT_PUBLIC_API_URL}/employee/pending-tickets?limit=10`, + `${API_BASE_URL}/employee/pending-tickets?limit=10`, { headers: { Authorization: `Bearer ${token}`, @@ -83,7 +83,7 @@ export default function EmployeDashboardPage() { try { const token = localStorage.getItem('auth_token') || localStorage.getItem('token'); const response = await fetch( - `${process.env.NEXT_PUBLIC_API_URL}/employee/stats`, + `${API_BASE_URL}/employee/stats`, { headers: { Authorization: `Bearer ${token}`, diff --git a/app/employe/gains-client/page.tsx b/app/employe/gains-client/page.tsx index 7df84c2..cfdb5a9 100644 --- a/app/employe/gains-client/page.tsx +++ b/app/employe/gains-client/page.tsx @@ -3,6 +3,7 @@ import { useState } from 'react'; import { Card } from '@/components/ui/Card'; import Button from '@/components/Button'; +import { API_BASE_URL } from '@/utils/constants'; import toast from 'react-hot-toast'; import { Search, @@ -66,7 +67,7 @@ export default function GainsClientPage() { const queryParam = searchType === 'email' ? `email=${encodeURIComponent(searchValue)}` : `phone=${encodeURIComponent(searchValue)}`; const response = await fetch( - `${process.env.NEXT_PUBLIC_API_URL}/employee/client-prizes?${queryParam}`, + `${API_BASE_URL}/employee/client-prizes?${queryParam}`, { headers: { Authorization: `Bearer ${token}`, @@ -100,7 +101,7 @@ export default function GainsClientPage() { try { const token = localStorage.getItem('auth_token') || localStorage.getItem('token'); const response = await fetch( - `${process.env.NEXT_PUBLIC_API_URL}/employee/validate-ticket`, + `${API_BASE_URL}/employee/validate-ticket`, { method: 'POST', headers: { diff --git a/app/employe/historique/page.tsx b/app/employe/historique/page.tsx index 801ba6a..9ccce6e 100644 --- a/app/employe/historique/page.tsx +++ b/app/employe/historique/page.tsx @@ -4,6 +4,7 @@ import { useState, useEffect } from 'react'; import { useAuth } from '@/hooks'; import { Card } from '@/components/ui'; import { Loading } from '@/components/ui/Loading'; +import { API_BASE_URL } from '@/utils/constants'; import toast from 'react-hot-toast'; import { CheckCircle, @@ -48,7 +49,7 @@ export default function EmployeeHistoryPage() { // Charger tous les tickets validés par cet employé const response = await fetch( - `${process.env.NEXT_PUBLIC_API_URL}/employee/history`, + `${API_BASE_URL}/employee/history`, { headers: { Authorization: `Bearer ${token}`, diff --git a/components/admin/TicketManagement.tsx b/components/admin/TicketManagement.tsx index 39b7a10..e0d7eeb 100644 --- a/components/admin/TicketManagement.tsx +++ b/components/admin/TicketManagement.tsx @@ -3,6 +3,7 @@ import { useState, useEffect, useCallback } from 'react'; import { adminService } from '@/services/admin.service'; import { Ticket, PaginatedResponse } from '@/types'; +import { API_BASE_URL } from '@/utils/constants'; export default function TicketManagement() { const [tickets, setTickets] = useState([]); @@ -402,7 +403,7 @@ export default function TicketManagement() { Filtre statut: {filterStatus || 'Aucun'}
- URL API: {process.env.NEXT_PUBLIC_API_URL || 'http://localhost:4000/api'} + URL API: {API_BASE_URL}
Tickets reçus: