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

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>
    );
}