From 80fdfa60ef74f148cc662432ff1539d9b19044d8 Mon Sep 17 00:00:00 2001 From: mai-vu Date: Thu, 16 May 2024 23:19:09 -0700 Subject: [PATCH] moved api url to .env --- src/app/admin-panel/home/jobPostingService.js | 8 +++++--- src/app/admin-panel/home/page.js | 14 +++++++------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/app/admin-panel/home/jobPostingService.js b/src/app/admin-panel/home/jobPostingService.js index 9140027..6a97073 100644 --- a/src/app/admin-panel/home/jobPostingService.js +++ b/src/app/admin-panel/home/jobPostingService.js @@ -1,3 +1,5 @@ +const JOB_POSTING_API_URL = process.env.NEXT_PUBLIC_JOB_POSTING_API_URL; + const saveJobPosting = async formData => { try { console.log('Form data:', formData); @@ -9,7 +11,7 @@ const saveJobPosting = async formData => { const jobPostings = Array.isArray(jobPosting) ? jobPosting : [jobPosting]; // Send the array of job postings to the backend API - const addJobPosting = await fetch('http://localhost:3000/api/job-posting', { + const addJobPosting = await fetch(JOB_POSTING_API_URL, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -42,7 +44,7 @@ const saveJobPosting = async formData => { const getJobPostingDetails = async jobId => { try { const response = await fetch( - `http://localhost:3000/api/job-posting/by-id?job-posting-id=${jobId}` + `${JOB_POSTING_API_URL}by-id?job-posting-id=${jobId}` ); if (!response.ok) { @@ -66,7 +68,7 @@ const editJobPosting = async (jobPostingId, data) => { // Send the job posting to the backend API const response = await fetch( - `http://localhost:3000/api/job-posting/?job-posting-id=${jobPostingId}`, + `${JOB_POSTING_API_URL}?job-posting-id=${jobPostingId}`, { method: 'PATCH', headers: { diff --git a/src/app/admin-panel/home/page.js b/src/app/admin-panel/home/page.js index 0eef3fd..bde9bc7 100644 --- a/src/app/admin-panel/home/page.js +++ b/src/app/admin-panel/home/page.js @@ -14,8 +14,9 @@ export default function Home() { const [loading, setLoading] = useState(true); const [user, setUser] = useState(null); + const JOB_POSTING_API_URL = process.env.NEXT_PUBLIC_JOB_POSTING_API_URL; + const fetchUser = useCallback(async () => { - console.log('fetchUser called'); try { const response = await fetch('/api/auth/me'); if (response.ok) { @@ -23,13 +24,12 @@ export default function Home() { setUser(userData); } else { console.error('Failed to fetch user:', response.statusText); + window.location.href = '/'; } } catch (error) { console.error('Error fetching user:', error); - } - - if (!user) { - window.location.href = '/'; // Redirect to login page if user is not logged in + //redirect to login page + window.location.href = '/'; } }, []); const [showForm, setShowForm] = useState(false); @@ -39,7 +39,7 @@ export default function Home() { try { setLoading(true); const sortCriteria = JSON.stringify({ _id: -1 }); - const apiURL = `http://localhost:3000/api/job-posting/?email=${user.email}&sort=${sortCriteria}`; + const apiURL = `${JOB_POSTING_API_URL}?email=${user.email}&sort=${sortCriteria}`; const response = await fetch(apiURL, { method: 'GET', headers: { @@ -74,7 +74,7 @@ export default function Home() { const handleDelete = async jobPostingId => { try { // Make API call to delete the job posting - const apiUrl = `http://localhost:3000/api/job-posting/?job-posting-id=${jobPostingId}`; + const apiUrl = `${JOB_POSTING_API_URL}?job-posting-id=${jobPostingId}`; const response = await fetch(apiUrl, { method: 'DELETE', headers: {