diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/AddressController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/AddressController.cs index 429767f..a61f777 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/AddressController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/AddressController.cs @@ -104,10 +104,13 @@ public class AddressController : Controller { } [EnableCors("_myAllowSpecificOrigins"), HttpPost] - public async Task> Post(AddressModel ad) { + public async Task> Post([FromForm] AddressModel ad) { + string id; + try { var user = await _userMan.GetUserAsync(_signInMan.Context.User); - user.Adresses.Add(ad); + id = _signInMan.Context.User.Identity.GetUserId(); + _context.Users.Where(x => x.Id == id).Include("Adresses").First().Adresses.Add(ad); _context.SaveChanges(); } catch (Exception e) { _logger.LogError(10, e.Message); diff --git a/GrossesMitaines/grosses-mitaines-ui/src/components/Address.js b/GrossesMitaines/grosses-mitaines-ui/src/components/Address.js new file mode 100644 index 0000000..c9c4c22 --- /dev/null +++ b/GrossesMitaines/grosses-mitaines-ui/src/components/Address.js @@ -0,0 +1,31 @@ +import { useState } from 'react'; +import { faTimes } from "@fortawesome/free-solid-svg-icons"; +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; + +const Address = ({ address, onDelete }) => { + return ( +
+
+

+ {address.civicNumber} {address.street} +

+

+ onDelete(address)} /> +

+
+ {address.appartment != null ? +
+ App: {address.appartment} +
+ : + null + } +
+

{address.city}, {address.province}, {address.country}, {address.postalCode}

+
+
+ ) +} + +export default Address; \ No newline at end of file diff --git a/GrossesMitaines/grosses-mitaines-ui/src/components/App.js b/GrossesMitaines/grosses-mitaines-ui/src/components/App.js index 22d56e6..21d13b0 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/components/App.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/components/App.js @@ -17,6 +17,7 @@ import React from 'react'; import Invoices from "../pages/Invoices"; import Cookies from "universal-cookie"; import ReviewInvoice from "../pages/ReviewInvoice"; +import MyAddresses from "../pages/MyAddresses"; const App = () => { const cookies = new Cookies(); @@ -53,6 +54,7 @@ const App = () => { } /> } /> } /> + } /> diff --git a/GrossesMitaines/grosses-mitaines-ui/src/pages/Formulaire.js b/GrossesMitaines/grosses-mitaines-ui/src/pages/Formulaire.js index 4baca0a..e971e37 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/pages/Formulaire.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/pages/Formulaire.js @@ -137,7 +137,7 @@ export default function App() { Adresse setFormAddress(userAddresses[e.target.value])}> {userAddresses.map((a, i) => ( - //{a.civicNumber} {a.street} + ))} diff --git a/GrossesMitaines/grosses-mitaines-ui/src/pages/MyAddresses.js b/GrossesMitaines/grosses-mitaines-ui/src/pages/MyAddresses.js new file mode 100644 index 0000000..03dc273 --- /dev/null +++ b/GrossesMitaines/grosses-mitaines-ui/src/pages/MyAddresses.js @@ -0,0 +1,238 @@ +import { useEffect } from "react"; +import { useState } from "react"; +import { Button } from "react-bootstrap"; +import { useForm } from "react-hook-form"; +import Cookies from "universal-cookie"; +import { Form } from "react-bootstrap"; +import { useNavigate } from "react-router-dom"; +import Address from "../components/Address"; +import Swal from "sweetalert2"; +import withReactContent from "sweetalert2-react-content"; +import { Alert } from "react-bootstrap"; + + +const MyAddresses = () => { + const { register, handleSubmit, setValue, formState: { errors } } = useForm(); + const cookies = new Cookies(); + const [userAddresses, setUserAddresses] = useState([]); + const [alertTitle, setAlertTitle] = useState(""); + const [alertMessage, setAlertMessage] = useState(""); + const [showAlert, setShowAlert] = useState(false); + const [isFormvisible, setFormVisibility] = useState(false); + const [isLoading, setIsLoading] = useState(true); + + const mySwal = withReactContent(Swal); + + 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); + setIsLoading(false); + } + }); + } + }, []); + + // useEffect(() => { + // if (userAddresses.length == 0) { + // if (cookies.get('addresses') != null && cookies.get('addresses').length != 0) + // setUserAddresses(cookies.get('addresses')); + // } + // }, []); + + // useEffect(() => { + // cookies.set('addresses', userAddresses, { path: '/', SameSite: 'strict', secure: true, maxAge: 2592000 }) + + + // }, [userAddresses]); + + const OnDelete = async (address) => { + mySwal.fire({ + title: `Effacer ${address.civicNumber} ${address.street}?`, + text: 'Êtes-vous certain de vouloir effacer cette adresse (cette action est irréversible)?', + icon: 'warning', + showCancelButton: true, + confirmButtonText: 'Oui', + cancelButtonText: 'Non', + }).then(async (result) => { + if (result.isConfirmed) { + fetch(`https://localhost:7292/api/Address?id=${address.id}`, { + method: 'DELETE', + mode: 'cors', + credentials: 'include' + }).then(async (response) => { + console.log(response); + if (response.ok) { + const deletedId = await response.json(); + setUserAddresses(userAddresses.filter((address) => address.id !== deletedId)); + + onShowAlert('Suppression de:', `${address.civicNumber} ${address.street} avec succès!`, 2000); + } + else { + console.log("test"); + mySwal.fire({ + title: `Erreur lors de la suppression de ${address.civicNumber} ${address.street}`, + text: `L'erreur: ${response}`, + icon: 'error', + }); + } + }); + } + }) + } + + const onSubmit = async (address) => { + console.log(address); + + let formData = new FormData(); + Object.keys(address).map((k) => { + formData.set(k, address[k]); + }); + + console.log(formData); + + const response = await fetch(`https://localhost:7292/api/Address`, { + method: 'POST', + credentials: 'include', + mode: 'cors', + headers: { + 'accept': 'application/json', + }, + body: formData + }) + + const newAddress = await response.json(); + console.log(newAddress); + + if (response.ok) { + setUserAddresses([...userAddresses, { ...newAddress }]); + + onShowAlert('Création de:', `${address.civicNumber} ${address.street} avec succès!`); + setFormVisibility(false); + } + else + console.log("Erreur de creation " + address.civicNumber + " " + address.street); + } + + const onShowAlert = (title, message, time) => { + + setAlertTitle(title); + setAlertMessage(message); + setShowAlert(true); + + window.setTimeout(() => { setShowAlert(false); }, time); + } + + return ( +
+ + {alertTitle} {alertMessage} + +
+

Adresses

+ + +
+ {isFormvisible && +
+
+

Ajouter une adresse

+
+
+
+ + +
+
+ {errors.civicNumber && errors.civicNumber.type === 'required' && Vous devez entrer une numéro civique!} +
+
+ + +
+
+ {errors.street && errors.street.type === 'required' && Vous devez entrer votre rue!} + {errors.street && errors.street.type === 'minLength' && Votre rue doit avoir au moins 3 lettres!} + {errors.street && errors.street.type === 'maxLength' && Votre rue doit avoir moins de 51 lettres!} + +
+
+ + +
+
+ + +
+
+ {errors.city && errors.city.type === 'required' && Vous devez entrer votre ville!} + {errors.city && errors.city.type === 'minLength' && Votre ville doit avoir au moins 4 lettres!} + {errors.city && errors.city.type === 'maxLength' && Votre ville doit avoir moins de 51 lettres!} +
+
+ + +
+
+ {errors.province && errors.province.type === 'required' && Vous devez entrer votre province!} + {errors.province && errors.province.type === 'maxLength' && Votre province doit avoir moins de 4 lettres!} +
+
+ + +
+
+ {errors.country && errors.country.type === 'required' && Vous devez entrer votre pays!} + {errors.country && errors.country.type === 'minLength' && Votre pays doit avoir au moins 4 lettres!} + {errors.cicountryty && errors.country.type === 'maxLength' && Votre pays doit avoir moins de 31 lettres!} +
+
+ + +
+
+ {errors.postalCode && errors.postalCode.type === 'required' && Vous devez entrer votre code postal!} +
+
+ +
+ +
+
+ } + {isLoading &&
} + {userAddresses.length > 0 ? + userAddresses.map((add) => ( + add && +
+ )) + : + ('Il n\'y a pas d\'adresses!')} +
+ ) +} + +export default MyAddresses; \ No newline at end of file