import {useState} from 'react';
import { EventDetailContext } from "./eventDetail.js"
import getEventById from "../../utils/events/getEventById.js";
import deleteTaskFunction from "../../utils/tasks/deleteTask.js";

export function EventDetailProvider({ children }) {

    const [tasks, setTasks] = useState([]);
    const [eventInfo, setEventInfo] = useState([]);


    const updateEventInfo = async (id) => {
        const e = await getEventById(id);
        setEventInfo(e.data);
        setTasks(e.data.tasks)
    }

    const deleteTask = async (id, eventId) => {
        await deleteTaskFunction(id);
        await updateEventInfo(eventId);
    }

    return (
        <EventDetailContext.Provider value={{ tasks, eventInfo, updateEventInfo, deleteTask }}>
            {children}
        </EventDetailContext.Provider>
    );
}