Wanhenri / Projeto-ERP

0 stars 0 forks source link

Two URL fetch #30

Closed Wanhenri closed 4 years ago

Wanhenri commented 4 years ago

Pass two URLs through the same fetch

Wanhenri commented 4 years ago

A bad practice of writing code but it works:

//GET
    const [posts, setPosts] = React.useState([]);
    useEffect(() => {
      fetch(`${process.env.REACT_APP_API_URL}/vendor_item`, {
        method: "GET",
        headers: {
          "Content-Type": "application/json"
        }
      })
        .then((response) => response.json())
        .then(data => {
          console.log("GET")
          setPosts(data)
        })
        .catch((error) => console.log(error));

    }, []);

    //DELETE
    const [deletes, setdeletes] = React.useState([]);
    const DeleteData = async (id) => {
      await fetch(`${process.env.REACT_APP_API_URL}/vendor_item`, {
        method: "DELETE",
        headers: {
          "Content-Type": "application/json"
        },
        body: JSON.stringify({
          id
        })
      })
        .then((response) => response.json())
        .then((data) => {
          console.log("DELETE")
          console.log(data)
          setdeletes(data);
        })
        .catch((error) => console.log(error));
    };

    useEffect(() => {
      DeleteData(" ", () => {});
    }, []);

    return (    
        <Wrapper >
          {posts.vendor&&(          
          <SimpleTable 
              vendor={posts.vendor}
              id="ID" 
              name="Vendor" 
              cnpj="cnpj" 
              city="city" 
              button="Add Vendor" 
              path="/vendor"
              onSubmit={DeleteData} /> 
          )} 
        </Wrapper>    
    );
}