diff --git a/src/routes.js b/src/routes.js index 5974e37..3722d78 100644 --- a/src/routes.js +++ b/src/routes.js @@ -131,6 +131,7 @@ import CityRevenueCharts from "./views/Charts/CityRevenue"; import { element } from "prop-types"; import OrderdayChart from "./views/Charts/OrderDaywise"; import RevenueCharts from "./views/Charts/RevenueCharts"; +import AddCustomer from "./views/customerDetails/addCustomer"; const routes = [ { path: "/", exact: true, name: "Home" }, { @@ -180,6 +181,11 @@ const routes = [ name: "User Table", element: SingleUserAllDetails, }, + { + path: "/add-customer", + name: "User Table", + element: AddCustomer, + }, // { // path: "/users-address/add", // name: "User Address", diff --git a/src/views/customerDetails/addCustomer.js b/src/views/customerDetails/addCustomer.js new file mode 100644 index 0000000..2f0dc20 --- /dev/null +++ b/src/views/customerDetails/addCustomer.js @@ -0,0 +1,395 @@ +import React, { useState } from "react"; +import { + TextField, + Button, + Card, + FormControl, + Grid, + FormHelperText, + OutlinedInput, + Box, +} from "@mui/material"; +import { useNavigate } from "react-router-dom"; +import toast from "react-hot-toast"; +import axios from "axios"; // Import axios for making HTTP requests +import { isAutheticated } from "src/auth"; +const styles = { + formStyle: { + fontWeight: "700", + fontSize: "12px", + fontFamily: "inter", + marginBottom: "3px", + marginLeft: "0", + }, +}; +const AddCustomer = () => { + const navigate = useNavigate(); + const [user, setUser] = useState({ + name: "", + email: "", + password: "", // No need to initialize password here + }); + const [id, setUserId] = useState(""); + const token = isAutheticated(); + + const [loading, setLoading] = useState(false); + const [data, setData] = useState({ + first_Name: "", + last_Name: "", + phone_Number: Number, + street: "", + city: "", + state: "", + postalCode: "", + country: "", + }); + + // console.log(data); + const handleChange = (e) => { + setData((prev) => ({ ...prev, [e.target.name]: e.target.value })); + }; + + const handerInputChanges = (e) => { + setUser({ ...user, [e.target.name]: e.target.value }); + }; + function handleAddressSubmit(e) { + e.preventDefault(); + if ( + data.first_Name === "" || + data.last_Name === "" || + data.phone_Number === null || + data.street === "" || + data.city === "" || + data.state === "" || + data.postalCode === "" || + data.country === "" + ) { + swal({ + title: "Warning", + text: "Please fill All mendetory fields ", + icon: "warning", + button: "ok", + dangerMode: true, + }); + return; + } + setLoading(true); + axios + .post( + `/api/shipping/address/admin/new/${id}`, + { + ...data, + }, + + { + headers: { + "Access-Control-Allow-Origin": "*", + Authorization: `Bearer ${token}`, + }, + } + ) + .then((res) => { + setLoading(false); + // setSuccess((prev) => !prev); + navigate("/customers-details"); + toast.success(res.data.message ? res.data.message : "Address Added!"); + }) + .catch((error) => { + setLoading(false); + toast.error( + error.response.data.message + ? error.response.data.message + : "Something went wrong!" + ); + }); + } + + // Generate password function + const generatePassword = (name, email) => { + const combinedStr = (name + email).toLowerCase(); // Convert to lowercase for consistency + const specialChars = "@#*"; // Define the set of special characters + const alphaChars = combinedStr.match(/[a-zA-Z]/g); // Filter out alphabetic characters + const filteredChars = combinedStr.match(/[^\W_]/g); // Filter out non-alphanumeric characters + let passwordChars = alphaChars.concat(filteredChars); // Combine alphabetic and filtered characters + + // Insert a random special character at a random position in the password characters array + const specialChar = specialChars.charAt( + Math.floor(Math.random() * specialChars.length) + ); // Pick a random special character + const randomIndex = Math.floor(Math.random() * (passwordChars.length + 1)); // Pick a random position to insert the special character + passwordChars.splice(randomIndex, 0, specialChar); // Insert the special character at the random position + + passwordChars = passwordChars.sort(() => Math.random() - 0.5); // Shuffle the characters + const password = passwordChars.join("").slice(0, 8); // Take the first 8 characters + return password; + }; + const handleFormSubmit = async (e) => { + e.preventDefault(); + try { + if (!user.name || !user.email) { + throw new Error("Fill all fields!"); + } + + // Generate password based on name and email + const generatedPassword = generatePassword(user.name, user.email); + console.log(generatedPassword); // Use generatedPassword instead of generatePassword + setUser({ ...user, password: generatedPassword }); // Set generated password to user state + + const response = await axios.post("/api/v1/user/register", { + ...user, // Send user details + password: generatedPassword, // Send generated password to the backend + }); + if (response.status === 201) { + toast.success("User Added Successful"); + setUserId(response.data.userId); + } + } catch (error) { + console.log(error.response.data.message); + toast.error(error.response.data.message); + } + }; + + console.log(user); + console.log("this is the id ", id); + + return ( +
+ +
+ +
+ +
+ + +
+ + +
+ + + + + + + FIRST NAME* + + handleChange(e)} + + // value={accountDetails.firstname} + // onChange={handerInputChanges} + /> + + + + + + LAST NAME* + + handleChange(e)} + // value={accountDetails.firstname} + // onChange={handerInputChanges} + /> + + + + + + PHONE NUMBER* + + handleChange(e)} + // value={accountDetails.firstname} + // onChange={handerInputChanges} + /> + + + + + + STREET ADDRESS* + + handleChange(e)} + // value={accountDetails.firstname} + // onChange={handerInputChanges} + /> + + + + + + + + COUNTRY* + + handleChange(e)} + // value={accountDetails.firstname} + // onChange={handerInputChanges} + /> + + + + + + TOWN CITY* + + handleChange(e)} + // value={accountDetails.firstname} + // onChange={handerInputChanges} + /> + + + + + + + + STATE* + + handleChange(e)} + // value={accountDetails.firstname} + // onChange={handerInputChanges} + /> + + + + + + + ZIP CODE* + + handleChange(e)} + // value={accountDetails.firstname} + // onChange={handerInputChanges} + /> + + + + + + + + +
+
+
+ ); +}; + +export default AddCustomer; diff --git a/src/views/customerDetails/customerTable.js b/src/views/customerDetails/customerTable.js index 448770e..ba6f0fb 100644 --- a/src/views/customerDetails/customerTable.js +++ b/src/views/customerDetails/customerTable.js @@ -131,7 +131,7 @@ const CustomerTable = () => { All Customers - {/*
+
-
*/} +