Skip to content

Commit

Permalink
moved api url to .env
Browse files Browse the repository at this point in the history
  • Loading branch information
mai-vu committed May 17, 2024
1 parent ab6e6f9 commit 80fdfa6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
8 changes: 5 additions & 3 deletions src/app/admin-panel/home/jobPostingService.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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',
Expand Down Expand Up @@ -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) {
Expand All @@ -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: {
Expand Down
14 changes: 7 additions & 7 deletions src/app/admin-panel/home/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ 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) {
const userData = await response.json();
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);
Expand All @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit 80fdfa6

Please sign in to comment.