'use client';

import { PropsWithChildren, useEffect } from 'react';
import { useTokenStore } from '@/store/token.state';
import { useTasksStore } from '@/store/tasks.state';
import { TasksApi } from '@/api/tasks.api';

export const TasksProvider = ({ children }: PropsWithChildren) => {
    const { setTasks } = useTasksStore();
    const { token } = useTokenStore();

    useEffect(() => {
        if (!token) {
            return;
        }
        const api = TasksApi.getInstance(token);
        api.getAllTasks().then(tasks => setTasks(tasks));
    }, [token]);

    return <>{children}</>;
};
