From e10a1839638a88add29a9a11a60b0c2a5780eff0 Mon Sep 17 00:00:00 2001 From: DavidBelisle <79233327+DavidBelisle@users.noreply.github.com> Date: Mon, 12 Dec 2022 13:10:46 -0500 Subject: [PATCH] Billing Address --- .../src/components/PaymentForm.js | 162 +++++++++++++++++- 1 file changed, 154 insertions(+), 8 deletions(-) diff --git a/GrossesMitaines/grosses-mitaines-ui/src/components/PaymentForm.js b/GrossesMitaines/grosses-mitaines-ui/src/components/PaymentForm.js index e8e045a..8e862cf 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/components/PaymentForm.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/components/PaymentForm.js @@ -1,11 +1,12 @@ -import React, { useState, useContext } from "react" +import React, { useState, useContext, useEffect } from "react" import { CardElement, useElements, useStripe } from "@stripe/react-stripe-js" -import { Button } from "react-bootstrap" +import { Button, Form } from "react-bootstrap" import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; import { CartContext } from "../components/Cart"; import { useNavigate } from "react-router-dom"; - +import { useForm } from "react-hook-form"; +import Cookies from "universal-cookie"; const CARD_OPTIONS = { iconStyle: "solid", @@ -31,18 +32,79 @@ const CARD_OPTIONS = { const PaymentForm = ({ cost, invoice }) => { const navigate = useNavigate(); + const { register, handleSubmit, setValue, formState: { errors } } = useForm(); + const cookies = new Cookies(); const mySwal = withReactContent(Swal); const [cardName, setCardName] = useState(""); - const [cardPhone,setCardPhone] = useState(""); - const [cardEmail,setCardEmail] = useState(""); + const [cardPhone, setCardPhone] = useState(""); + const [cardEmail, setCardEmail] = useState(""); const [isLoading, setIsLoading] = useState(false); + const [userAddresses, setUserAddresses] = useState([]); + const [currentAdress, setCurrentAdress] = useState({ + civicNumber: "", + appartment: "", + street: "", + city: "", + province: "", + country: "", + postalCode: "" + }) + + const stripe = useStripe(); const elements = useElements(); const cart = useContext(CartContext); - const handleSubmit = async (e) => { + + + useEffect(() => { + const userInfo = cookies.get("GMGM"); + if (userInfo != null && userInfo.LoggedIn == true) { + fetch("https://localhost:7292/api/Address", { + method: 'GET', + credentials: 'include', + mode: 'cors' + },).then(async (response) => { + if (response.ok) { + const json = await response.json(); + setUserAddresses(json); + if (json.length >= 1) { + setFormAddress(json[0]); + } + } + }); + } + }, []); + + const setFormAddress = (address) => { + setCurrentAdress((e) => { + return { + ...e, + civicNumber: address.civicNumber, + appartment: address.appartment, + street: address.street, + city: address.city, + province: address.province, + country: address.country, + postalCode: address.postalCode + } + }); + + setValue("civicNumber", address.civicNumber); + setValue("appartment", address.appartment); + setValue("street", address.street); + setValue("city", address.city); + setValue("province", address.province); + setValue("country", address.country); + setValue("postalCode", address.postalCode); + } + + + + + const onSubmit = async (e) => { e.preventDefault(); setIsLoading(true); const { error } = await stripe.createPaymentMethod({ @@ -97,7 +159,7 @@ const PaymentForm = ({ cost, invoice }) => { title: 'Erreur!', timer: 2000, icon: "error" - }).then(()=>{ + }).then(() => { setIsLoading(false); }); } @@ -115,7 +177,91 @@ const PaymentForm = ({ cost, invoice }) => { return ( <> { -