import { useState, useContext, useEffect } from "react"; import Cookies from "universal-cookie"; import ReviewProdList from "../components/ReviewProdList"; import TotalProductsPrice from "../components/TotalProductsPrice"; import { Row, Col, Button } from "react-bootstrap"; import { useNavigate } from "react-router-dom"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; import { CartContext } from "../components/Cart"; import StripeContainer from "../components/StripeContainer" const ReviewInvoice = () => { const navigate = useNavigate(); const mySwal = withReactContent(Swal); const cart = useContext(CartContext); const [thisInvoice, setThisInvoice] = useState({ firstName: "", lastName: "", phoneNumber: "", emailAddress: "", civicNumber: "", appartment: "", street: "", city: "", province: "", country: "", postalCode: "", prodQuant: [], }); const [total, setTotal] = useState(-1); useEffect(() => { const cookies = new Cookies(); const thisInvoice = cookies.get('invoice'); if (thisInvoice != null) { var dic = {}; if (cart.items.length > 0) { cart.items.forEach((item) => { dic[item.id] = item.quantity; } ) } const tpsRate = 5; const tvqRate = 9.975; var price = cart.getTotalCost(); var tps = 0; var tvq = 0; var totalCost; tps = price * (tpsRate / 100); tvq = price * (tvqRate / 100); totalCost = price + tps + tvq; totalCost = totalCost * 100; totalCost = totalCost.toFixed(0); setTotal(totalCost); setThisInvoice((e) => { return { ...e, firstName: thisInvoice.firstName, lastName: thisInvoice.lastName, phoneNumber: thisInvoice.phoneNumber, emailAddress: thisInvoice.emailAddress, civicNumber: thisInvoice.civicNumber, appartment: thisInvoice.appartment, street: thisInvoice.street, city: thisInvoice.city, province: thisInvoice.province, country: thisInvoice.country, postalCode: thisInvoice.postalCode, prodQuant: dic } }) } else { console.log("No invoice to review!"); } }, [cart]); const handleModify = () => { navigate("/formulaire") } const handleConfirmer = async () => { const json = JSON.stringify(thisInvoice); const response = await fetch(`https://localhost:7292/api/Invoice`, { method: 'POST', credentials: 'include', mode: 'cors', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: json }) if (response.ok) { mySwal.fire({ title: 'Commande envoyée avec succès!', timer: 2000, icon: "success" }).then( () => { cart.emptyCart(); navigate('/morceaux'); } ) } else { console.log("Erreur de creation la commande #" + thisInvoice.id); mySwal.fire({ title: 'Erreur!', timer: 2000, icon: "error" }) } } return ( <> Veuillez confirmer les informations ci-dessous! Prénom: {thisInvoice.firstName} Nom: {thisInvoice.lastName} Téléphone: {thisInvoice.phoneNumber} Courriel: {thisInvoice.emailAddress} Numéro civique: {thisInvoice.civicNumber} {thisInvoice.appartment != null ? Numéro d'appartement: {thisInvoice.appartment} : null } Rue: {thisInvoice.street} Ville: {thisInvoice.city} Province (abréviation): {thisInvoice.province} Pays: {thisInvoice.country} Code postal: {thisInvoice.postalCode} {/* Confirmer */} Modifier > ); } export default ReviewInvoice;