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(() => {
|
useEffect(() => {
|
||||||
async function fetchFeaturedJobs() {
|
async function fetchFeaturedJobs() {
|
||||||
try {
|
try {
|
||||||
// Assuming API proxy is set up or env var is available. using relative path for now as per usual Next.js setup
|
const apiBase = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8521"
|
||||||
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8521'}/jobs?featured=true&limit=6`)
|
const mapJobs = (jobs: any[]): Job[] =>
|
||||||
if (!res.ok) throw new Error('Failed to fetch jobs')
|
jobs.map((j: any) => ({
|
||||||
const data = await res.json()
|
|
||||||
|
|
||||||
// Backend returns: { data: JobWithCompany[], pagination: ... }
|
|
||||||
if (data.data) {
|
|
||||||
const mappedJobs: Job[] = data.data.map((j: any) => ({
|
|
||||||
id: String(j.id),
|
id: String(j.id),
|
||||||
title: j.title,
|
title: j.title,
|
||||||
company: j.companyName || t('jobs.confidential'),
|
company: j.companyName || t("jobs.confidential"),
|
||||||
location: j.location || t('workMode.remote'),
|
location: j.location || t("workMode.remote"),
|
||||||
type: j.employmentType || 'full-time',
|
type: j.employmentType || "full-time",
|
||||||
salary: j.salaryMin ? `R$ ${j.salaryMin}` : t('jobs.salary.negotiable'),
|
salary: j.salaryMin ? `R$ ${j.salaryMin}` : t("jobs.salary.negotiable"),
|
||||||
description: j.description,
|
description: j.description,
|
||||||
requirements: j.requirements || [],
|
requirements: j.requirements || [],
|
||||||
postedAt: j.createdAt,
|
postedAt: j.createdAt,
|
||||||
isFeatured: j.isFeatured
|
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) {
|
} catch (error) {
|
||||||
console.error("Error fetching featured jobs:", error)
|
console.error("Error fetching featured jobs:", error)
|
||||||
// Fallback to mock data if API fails? Or just empty.
|
// Fallback to mock data if API fails? Or just empty.
|
||||||
// For MVP let's leave empty or maybe keep mock as fallback if needed.
|
// 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 {
|
} finally {
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue