update
This commit is contained in:
95
gnx-react/lib/hooks/useCareer.ts
Normal file
95
gnx-react/lib/hooks/useCareer.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { careerService, JobPosition } from '../api/careerService';
|
||||
|
||||
/**
|
||||
* Custom hook to fetch all job positions
|
||||
*/
|
||||
export const useJobs = () => {
|
||||
const [jobs, setJobs] = useState<JobPosition[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchJobs = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const data = await careerService.getAllJobs();
|
||||
setJobs(data);
|
||||
setError(null);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'An error occurred');
|
||||
console.error('Error fetching jobs:', err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchJobs();
|
||||
}, []);
|
||||
|
||||
return { jobs, loading, error };
|
||||
};
|
||||
|
||||
/**
|
||||
* Custom hook to fetch a single job by slug
|
||||
*/
|
||||
export const useJob = (slug: string) => {
|
||||
const [job, setJob] = useState<JobPosition | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!slug) {
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const fetchJob = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const data = await careerService.getJobBySlug(slug);
|
||||
setJob(data);
|
||||
setError(null);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'An error occurred');
|
||||
console.error('Error fetching job:', err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchJob();
|
||||
}, [slug]);
|
||||
|
||||
return { job, loading, error };
|
||||
};
|
||||
|
||||
/**
|
||||
* Custom hook to fetch featured jobs
|
||||
*/
|
||||
export const useFeaturedJobs = () => {
|
||||
const [jobs, setJobs] = useState<JobPosition[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchFeaturedJobs = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const data = await careerService.getFeaturedJobs();
|
||||
setJobs(data);
|
||||
setError(null);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'An error occurred');
|
||||
console.error('Error fetching featured jobs:', err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchFeaturedJobs();
|
||||
}, []);
|
||||
|
||||
return { jobs, loading, error };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user