import { useState, useEffect } from 'react';
import classService from '../services/serviceClasses';

const useClassDetail = (classId) => {
    const [classData, setClassData] = useState([]);
    const [nameClass, setClassName] = useState('class');
    const [loading, setLoading] = useState(true);
    const [error, setError] = useState(null);
    
    
    useEffect(() => {
        const fetchData = async () => {
            setLoading(true);
            try {
                const getNameClass = await classService.getClassById(classId);
                setClassName(getNameClass.payload.NAME_CLASS);

                const data = await classService.getClassStudent(classId);
                if ([data.payload].length > 0) {
                    setClassData(data.payload);
                } else {
                    setClassData([]);
                }
            } catch (error) {
                setError(error);
            } finally {
                setLoading(false);
            }
        };

        fetchData();
    }, [classId]);

  return { classData, nameClass, loading, error };
};

export default useClassDetail;