import { memo, useEffect, useMemo, useState } from "react";
import UserService from "../services/UserService";

export default function useUserData<T>(token: string, initData: T): T {
    const [userData, setUserData] = useState<T>(initData)

    useEffect(()=> {
        const fetchUserData = async (token: string) => {
            const response = await UserService.getCurrentUser(token)
            setUserData(response.data)
        }

        fetchUserData(token)
    }, [token])

    const memoizedData = useMemo<T>(() => userData, [userData])
    return memoizedData
}