import { Typography } from "@material-ui/core"; import { Box, Button, FormControl, MenuItem, Select, TextField, } from "@mui/material"; import React, { useEffect, useState } from "react"; import swal from "sweetalert"; import { v4 as uuid } from "uuid"; import MainAddress from "./mainAddress"; import axios from "axios"; import { isAutheticated } from "src/auth"; import { useNavigate, useParams } from "react-router-dom"; export default function EditUserAddress() { const token = isAutheticated(); const [selectUserType, setSelectUserType] = useState(""); const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [phno, setPhno] = useState(""); const [updateBtn, setUpdateBtn] = useState(false); const [edit, setEdit] = useState(false); const navigate = useNavigate(); const id = useParams()?.id; const [addressess, setAddressess] = useState([ { id: uuid(), addressLine1: "", addressLine2: "", country: "", state: "", city: "", zipcode: "", }, ]); const [view, setView] = useState("0"); const [phnoError, setPhnoError] = useState(""); const [emailError, setEmailError] = useState(""); const handlePhnoChange = (e) => { const inputValue = e.target.value; const numericValue = inputValue.replace(/[^0-9]/g, ""); setPhno(numericValue); if (numericValue.length > 10 || numericValue.length < 10) { setPhnoError( "Please enter a valid phone number with a maximum of 10 digits." ); } else { setPhno(numericValue); setPhnoError(""); } }; const handleEmailChange = (e) => { const inputValue = e.target.value; setEmail(inputValue); // Regular expression for email validation const emailRegex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i; if (!emailRegex.test(inputValue)) { setEmailError("Please enter a valid email address."); } else { setEmail(inputValue); setEmailError(""); } }; const handleNextClick = () => { if (view === "0" && selectUserType !== "") { setView("1"); } else if ( !emailError && !phnoError && name !== "" && email !== "" && phno !== "" && view === "1" ) { setView("2"); } else { swal({ title: "Please fill all the required fileds correctly", icon: "error", button: "Ok", dangerMode: true, }); } }; const handleBackClick = () => { if (view === "1") { setView("0"); } else if (view === "2") { setView("1"); } }; const handleAddMoreAddress = () => { setAddressess([ ...addressess, { id: uuid(), addressLine1: "", addressLine2: "", country: "", state: "", city: "", zipcode: "", }, ]); }; const handleDelete = (id) => { const filteredAddress = addressess.filter((item) => item.id != id); setAddressess(filteredAddress); }; const handleAddressChange = (id, field, value) => { // Find the address with the given ID const updatedAddresses = addressess.map((address) => { if (address.id === id) { return { ...address, [field]: value }; } return address; }); setAddressess(updatedAddresses); }; const getOneAddress = async () => { axios .get(`/api/user-address/getOneAddress/${id}`, { headers: { "Access-Control-Allow-Origin": "*", Authorization: `Bearer ${token}`, }, }) .then((res) => { setSelectUserType(res?.data?.address?.userType); setName(res?.data?.address?.name); setEmail(res?.data?.address?.email); setPhno(res?.data?.address?.phno); setAddressess(res?.data?.address?.addressess); }) .catch((error) => { swal({ title: error, text: " Can not fetch the Address ", icon: "error", button: "Retry", dangerMode: true, }); }); }; const handleUpdateAddress = async () => { if ( !selectUserType || !name || !email || !phno || addressess.some(addressIsEmpty) ) { swal({ title: "Please fill all the required fields correctly", icon: "error", button: "Ok", dangerMode: true, }); } else { const updatedresponse = await axios.patch( `/api/user-address/updateAddress/${id}`, { userType: selectUserType, name, email, phno, addressess, }, { headers: { Authorization: `Bearer ${token}`, }, } ); if (updatedresponse.status === 200) { swal({ title: "Congratulations!!", text: "Address updated successfully!", icon: "success", button: "OK", }); navigate("/users-address", { replace: true }); } else { swal({ title: "Please try again", text: "Cannot update something went wronge !", icon: "error", button: "OK", }); } } }; const addressIsEmpty = (address) => { return ( address.addressLine1 === "" || address.country === "" || address.state === "" || address.city === "" || address.zipcode === "" ); }; useEffect(() => { getOneAddress(); }, []); return (