Skip to content
Snippets Groups Projects
display_posts.js 1.44 KiB
Newer Older
import React, { useEffect, useState } from 'react'
import { getPosts } from '../../../api/job_postings'
import Loader from "../loader";
import Post from './post';

export default function DisplayPosts(props) {
Michael Tobis's avatar
Michael Tobis committed
    const [loading, setLoading] = useState(false);
    const [error, setError] = useState(false);
Michael Tobis's avatar
Michael Tobis committed
    const [jobPosts, setJobPosts] = useState([]);

    useEffect(() => {
        loadData();
Michael Tobis's avatar
Michael Tobis committed
    }, [props.searchForJob])

    const loadData = async () => {
Michael Tobis's avatar
Michael Tobis committed
        setLoading(true);
        if (props.searchForJob) {
            const response = await getPosts(props.query, props.filters);
Michael Tobis's avatar
Michael Tobis committed
            if (response.status && response.status === 200) {
                setJobPosts(response.data);
                console.log(response.data)
Michael Tobis's avatar
Michael Tobis committed
            } else {
                setError(true);
            }
Michael Tobis's avatar
Michael Tobis committed
            props.setSearchForJob(false);
        }
        setLoading(false);
    }

    if (loading) return <div className='posts'><Loader /></div>
    if (error) return <div className='posts'>Failed to load job posts. Please refresh the page and try again.</div>
    if (jobPosts.length === 0) return <div className='posts'>No postings shown. Search for a job posting above!</div>
        <div className='posts'>
Michael Tobis's avatar
Michael Tobis committed
            <div className='posts-data'>
                {jobPosts.map(post => {
                    return <Post key={post.job_id} post={post} />
                })}
            </div>