Files
GGMM/GrossesMitaines/grosses-mitaines-ui/src/pages/MyInvoices.js
DavidBelisle 595b6d980c FirstPart
2022-12-12 12:27:26 -05:00

83 lines
2.7 KiB
JavaScript

import { useEffect } from "react";
import { useState } from "react";
import InvoiceList from "../components/InvoiceList";
import Swal from "sweetalert2";
import withReactContent from "sweetalert2-react-content";
const MyInvoices = () => {
const [invoices, setInvoices] = useState([]);
const mySwal = withReactContent(Swal);
useEffect(() => {
fetch(`https://localhost:7292/api/Invoice`, {
method: 'GET',
mode: 'cors',
credentials: 'include'
}).then(async (response) => {
if (response.ok) {
var json = await response.json();
setInvoices(json);
}
else {
console.log("Erreur lors de la requête des invoices");
}
});
}, []);
const handleCancelInvoice = async (id) => {
mySwal.fire({
title: `Annuler la commande?`,
text: 'Êtes-vous certain de vouloir annuler cette commande (cette action est irréversible)?',
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'Oui',
cancelButtonText: 'Non',
}).then(async (result) => {
if (result.isConfirmed) {
fetch(`https://localhost:7292/api/Invoice/Cancel/${id}`, {
method: 'POST',
mode: 'cors',
credentials: 'include'
}).then(async (response) => {
if (response.ok) {
const deletedInvoice = await response.json();
var newInvoice = invoices.find(x => x.id == deletedInvoice.id);
console.log(newInvoice);
newInvoice.status = deletedInvoice.status;
setInvoices([...(invoices.filter(x => x.id !== newInvoice.id)), { ...newInvoice }].sort((a, b) => a.id - b.id));
mySwal.fire({
title: `Commande annulée...`,
text: `La commande a été annulée!`,
icon: 'success',
});
}
else {
mySwal.fire({
title: `Erreur lors de l'annulation de la commande...`,
text: `L'erreur: ${await response.json()}`,
icon: 'error',
});
}
});
}
})
};
return (
<>
<InvoiceList
invoices={invoices}
onCancel={handleCancelInvoice}
paymentInfos={true}
/>
</>
);
}
export default MyInvoices