import { useEffect, useState } from "react";
import { BoardSchema, BoardService } from "../../../client";
import { useProjectsContext } from "../../../contexts/ProjectsContext.tsx";


const useBoards = () => {
    const { selectedProject } = useProjectsContext();
    const [boards, setBoards] = useState<BoardSchema[]>([]);

    const refetchBoards = () => {
        if (!selectedProject) return;

        BoardService.getBoards({
            projectId: selectedProject.id,
        })
            .then(data => {
                setBoards(data.boards);
            })
            .catch(e => console.log(e));
    };

    useEffect(() => {
        refetchBoards();
    }, [selectedProject]);

    return {
        boards,
        refetchBoards,
    };
};

export default useBoards;
