Skip to content

Commit

Permalink
Merge pull request #73 from Vero-Ventures/hot-fix/update-admin-panel-…
Browse files Browse the repository at this point in the history
…fetch-postings

Hot fix/update admin panel fetch postings
  • Loading branch information
Soohyeun authored May 23, 2024
2 parents a78400b + 1e6dfdb commit 41e64a5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 41 deletions.
55 changes: 39 additions & 16 deletions src/app/admin-panel/home/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import jobPostingService from './jobPostingService';
import { useCallback } from 'react';
import Navbar from '@/components/ui/navbar';

const MAX_PAGES = 999;

// Define your component
export default function Home() {
// State to store the list of job postings
Expand Down Expand Up @@ -41,31 +43,52 @@ export default function Home() {
}, []);
const [showForm, setShowForm] = useState(false);

// Function to fetch job postings from the API
const fetchJobPostings = useCallback(async () => {
try {
setLoading(true);
const sortCriteria = JSON.stringify({ _id: -1 });
const apiURL = `${JOB_POSTING_API_URL}?email=${user.email}&sort=${sortCriteria}`;
const response = await fetch(apiURL, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
let page = 1;
let jobPostingsArray = [];

if (response.ok) {
const res = await response.json();
setJobPostings(res.jobPostings);
} else {
console.error('Failed to fetch job postings:', response.statusText);
while (page <= MAX_PAGES) {
const apiURL = `${JOB_POSTING_API_URL}?page_num=${page}&email=${user.email}`;
const response = await fetch(apiURL, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});

if (response.ok) {
if (response.status === 204) {
// If the API returns no content status, break the loop
break;
}

const res = await response.json();
const jobPostings = res.jobPostings;

if (jobPostings.length === 0) {
// If no job postings found, break the loop
break;
} else {
// Add the fetched job postings to the array
jobPostingsArray = [...jobPostingsArray, ...jobPostings];
page++; // Increment the page number for the next request
}
} else {
console.error('Failed to fetch job postings:', response.statusText);
break;
}
}

// Set the job postings array in state
setJobPostings(jobPostingsArray);
} catch (error) {
console.error('Error fetching job postings:', error);
} finally {
setLoading(false);
}
}, [user]);
}, [user, JOB_POSTING_API_URL]);

useEffect(() => {
fetchUser();
Expand All @@ -75,7 +98,7 @@ export default function Home() {
if (user) {
fetchJobPostings();
}
}, [user]);
}, [user, fetchJobPostings]);

// Function to handle deletion of job posting
const handleDelete = async jobPostingId => {
Expand Down
25 changes: 0 additions & 25 deletions src/components/ui/checkbox.jsx

This file was deleted.

0 comments on commit 41e64a5

Please sign in to comment.