Hello-World-Software-Studios / calculator

Carpenter's tool for wall layout
0 stars 1 forks source link

Maybe move bodyJSON inside the post? URL too #75

Closed ecumene closed 3 years ago

ecumene commented 3 years ago

export default function usePostAPI() {
  const [isLoading, setIsLoading] = useState(false);
  const [error, setError] = useState(null);

  const callAPI = useCallback(async (url, bodyJSON) => {
    setIsLoading(true);
    try {
      const res = await fetch(url, {
        method: bodyJSON ? "POST" : "DELETE",
        headers: {
          "Content-Type": "application/json",
          Authorization: `Bearer ${localStorage.Token}`,
        },
        body: JSON.stringify(bodyJSON),
      });
      const json = await res.json();
      setIsLoading(false);
      return json;
    } catch (err) {
      setError(err.message);
    }
    return null;
  }, []);

  return [{isLoading, error}, callAPI];
}
JPM709 commented 3 years ago

So I moved url, bodyJSON into callAPI. Tested both post and delete, they seem to be working so far. Now need to remove data/setData state

JPM709 commented 3 years ago

This now works without state for data. Marking as closed.