Attempt at useGetTruck custom hook

PHOTO EMBED

Wed Apr 07 2021 22:41:39 GMT+0000 (UTC)

Saved by @stephanieraymos

import React, { useState, useEffect } from "react";
import { getByIdURL } from "../../Pages/urls";
import { useParams } from "react-router-dom";

export const useGetTruck = () => {
  const { id } = useParams();

  const [loadId, setLoadId] = useState("");
  const [loading, setLoading] = useState(false);
  const [source, setSource] = useState("");
  const [retailPrice, setRetailPrice] = useState("");
  const [price, setPrice] = useState("");
  const [status, setStatus] = useState(1);
  const [contents, setContents] = useState("");
  const [category, setCategory] = useState("");
  const [units, setUnits] = useState("");
  const [palletCount, setPalletCount] = useState("");
  const [fob, setFob] = useState("");
  const [manifestIds, setManifestIds] = useState([]);
  const [data, setData] = useState([{}]);
  fetch(`${getByIdURL}${id}`, {
    method: "GET",
    headers: {
      "Content-Type": "application/json",
    },
  })
    .then((response) => {
      return response.json();
    })
    .then((data) => {
      if (data) {
        const {
          loadId,
          source,
          price,
          retailPrice,
          category,
          fob,
          units,
          palletCount,
          contents,
          manifestIds,
          status,
        } = data[0];

        setLoadId(loadId);
        setSource(source);
        setPrice(price);
        setRetailPrice(retailPrice);
        setContents(contents.join(", "));
        setManifestIds(manifestIds);
        setCategory(category);
        setUnits(units);
        setPalletCount(palletCount);
        setFob(fob);
        setStatus(status);
        setData([
          loadId,
          source,
          price,
          retailPrice,
          category,
          fob,
          units,
          palletCount,
          contents,
          manifestIds,
          status,
        ]);
      }
      setLoading(false);
    })
    .catch((error) => {
      console.log(error);
      setLoading(false);
    });
  return data;
};
content_copyCOPY