import { useEffect, useState } from 'react';
import { getLatestReview, getReviewHistory } from '../../../api';

export default function useReview() {
    const [latest, setLatest] = useState(null);
    const [history, setHistory] = useState([]);
    const [loading, setLoading] = useState(true);

    useEffect(() => {
        let cancel = false;
        Promise.all([getLatestReview(), getReviewHistory(4)])
            .then(([l, h]) => {
                if (cancel) return;
                setLatest(l);
                setHistory(h);
            })
            .catch(() => {})
            .finally(() => !cancel && setLoading(false));
        return () => { cancel = true; };
    }, []);

    return { latest, history, loading };
}
