Basic foncitonnalité de POST et DELETE pour Product
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user