Show six jobs on home
This commit is contained in:
parent
c0d42b1cc4
commit
cdfec9e3f2
1 changed files with 31 additions and 14 deletions
|
|
@ -24,32 +24,49 @@ export default function HomePage() {
|
|||
useEffect(() => {
|
||||
async function fetchFeaturedJobs() {
|
||||
try {
|
||||
// Assuming API proxy is set up or env var is available. using relative path for now as per usual Next.js setup
|
||||
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8521'}/jobs?featured=true&limit=6`)
|
||||
if (!res.ok) throw new Error('Failed to fetch jobs')
|
||||
const data = await res.json()
|
||||
|
||||
// Backend returns: { data: JobWithCompany[], pagination: ... }
|
||||
if (data.data) {
|
||||
const mappedJobs: Job[] = data.data.map((j: any) => ({
|
||||
const apiBase = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8521"
|
||||
const mapJobs = (jobs: any[]): Job[] =>
|
||||
jobs.map((j: any) => ({
|
||||
id: String(j.id),
|
||||
title: j.title,
|
||||
company: j.companyName || t('jobs.confidential'),
|
||||
location: j.location || t('workMode.remote'),
|
||||
type: j.employmentType || 'full-time',
|
||||
salary: j.salaryMin ? `R$ ${j.salaryMin}` : t('jobs.salary.negotiable'),
|
||||
company: j.companyName || t("jobs.confidential"),
|
||||
location: j.location || t("workMode.remote"),
|
||||
type: j.employmentType || "full-time",
|
||||
salary: j.salaryMin ? `R$ ${j.salaryMin}` : t("jobs.salary.negotiable"),
|
||||
description: j.description,
|
||||
requirements: j.requirements || [],
|
||||
postedAt: j.createdAt,
|
||||
isFeatured: j.isFeatured
|
||||
}))
|
||||
setFeaturedJobs(mappedJobs)
|
||||
|
||||
const featuredRes = await fetch(`${apiBase}/jobs?featured=true&limit=6`)
|
||||
if (!featuredRes.ok) throw new Error("Failed to fetch featured jobs")
|
||||
const featuredData = await featuredRes.json()
|
||||
const featuredList = featuredData.data ? mapJobs(featuredData.data) : []
|
||||
|
||||
if (featuredList.length === 6) {
|
||||
setFeaturedJobs(featuredList)
|
||||
return
|
||||
}
|
||||
|
||||
const fallbackRes = await fetch(`${apiBase}/jobs?limit=6`)
|
||||
if (!fallbackRes.ok) throw new Error("Failed to fetch fallback jobs")
|
||||
const fallbackData = await fallbackRes.json()
|
||||
const fallbackList = fallbackData.data ? mapJobs(fallbackData.data) : []
|
||||
|
||||
const combined = [...featuredList, ...fallbackList].slice(0, 6)
|
||||
if (combined.length === 6) {
|
||||
setFeaturedJobs(combined)
|
||||
} else if (combined.length > 0) {
|
||||
setFeaturedJobs(combined)
|
||||
} else {
|
||||
setFeaturedJobs(mockJobs.slice(0, 6))
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error fetching featured jobs:", error)
|
||||
// Fallback to mock data if API fails? Or just empty.
|
||||
// For MVP let's leave empty or maybe keep mock as fallback if needed.
|
||||
// setFeaturedJobs(mockJobs.slice(0, 6))
|
||||
setFeaturedJobs(mockJobs.slice(0, 6))
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue