// useUserById.ts
import { useState, useEffect } from "react";
import useAxios from "./useAxios";
import { User } from "@/types/types";

export const useUserById = (_id: string) => {
  const [user, setUser] = useState<User>();
  const { request } = useAxios();

  useEffect(() => {
    const fetchUser = async () => {
      try {
        const response = await request<{ user: User }>("GET", `/profile/${_id}`);
        setUser(response.user);
      } catch (error) {
        console.error("Failed to fetch user:", error);
      }
    };

    if (_id) fetchUser();
  }, [_id, request]);

  return user;
};
