import { useEffect, useState } from "react";

const useFetchFromEndpoint = (endpoint, token) => {
  const [response, setResponse] = useState(null);
  const [status, setStatus] = useState(0);

  useEffect(() => {
    const fetchFromAPI = async () => {
      await fetch(`http://localhost:8000${endpoint}`, { headers: { 'Authorization': `Token ${token}` } })
        .then(res => {
          setStatus(res.status);
          return res.json();
        })
        .then(data => setResponse(data))
    };

    fetchFromAPI();
  }, [endpoint, token]);

  return [response, status];
};

export default useFetchFromEndpoint;
