Merge branch 'react-version' of https://github.com/MarcEricMartel/420-5DW-HY-TP into react-version

This commit is contained in:
David Belisle
2022-10-18 11:55:39 -04:00
6 changed files with 66 additions and 113 deletions

View File

@@ -1,14 +1,9 @@
import { useEffect, useState } from "react";
import axios from "axios";
import Button from '../components/Button'
import SimpleItemList from "../components/SimpleItemList";
import Ajouter from "../components/Ajouter";
const API_URL = 'https://localhost:7292/'
const INVENTAIRE_URL = API_URL + 'api/Inventory'
const PRODUIT_URL = API_URL + 'api/Product'
const Inventaire = () => {
const [morceaux, setMorceaux] = useState([]);
@@ -16,23 +11,40 @@ const Inventaire = () => {
useEffect(() => {
async function fetchData() {
const res = await axios.get(INVENTAIRE_URL);
setMorceaux(res.data);
const response = await fetch(`https://localhost:7292/api/Inventory?all=true`);
const json = await response.json();
if (json.length > 0)
setMorceaux([...json]);
}
fetchData();
document.title = 'Inventaire';
}, []);
const handleAddItem = async (morceau) => {
const res = await axios.post(PRODUIT_URL, morceau)
setMorceaux([...morceaux, { ...morceau, id: res.data.id }]);
console.log(morceau);
const response = await fetch(`https://localhost:7292/api/Product`, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(morceau)
})
const newMorceau = response.json();
if (response.ok)
setMorceaux([...morceaux, { ...newMorceau }]);
else
console.log("Erreur de creation " + morceau);
};
const handleDeleteItem = async (id) => {
const response = await fetch(`https://localhost:7292/Product?id=${id}`, { method: 'DELETE', mode: 'cors' });
// const res = await axios.delete(`${INVENTAIRE_URL}/${id}`)
const response = await fetch(`https://localhost:7292/api/Product?id=${id}`, { method: 'DELETE', mode: 'cors' });
if (response.ok)
setMorceaux(morceaux.filter((morceau) => morceau.id !== id))
else