import React, { useState, useEffect, useRef } from "react"; import axios from "axios"; import { Link, useNavigate, useParams } from "react-router-dom"; import QRCode from "react-qr-code"; import { isAutheticated } from "src/auth"; import { useDispatch, useSelector } from "react-redux"; import { addItemsToCart } from "src/redux/Actions/cartAction"; import toast from "react-hot-toast"; import { cibBlackberry } from "@coreui/icons"; import Button from "@material-ui/core/Button"; function AddOrder() { const { status, id } = useParams(); const { cartItems, subTotal, shippingCharge, tax, shipingInfo, total } = useSelector((state) => state.cart); const AllStates = useSelector((state) => state); const getValue = useRef(); const getFranchiseeID = useRef(); const dispatch = useDispatch(); const navigate = useNavigate(); const printOrderRef = useRef(); const token = isAutheticated(); const [productData, setProductData] = useState([]); const [allFranchisee, setAllFranchisee] = useState([]); const [allTax, setAllTax] = useState([]); const [productDetails, setProductDetails] = useState(); const [loading, setLoading] = useState(true); const [orderStatus, setOrderStatus] = useState(""); const [data, setData] = useState({ product_Name: "", address: "", quantity: "", contact_Number: "", total_Price: "", }); useEffect(() => { const getAllTax = async () => { const res = await axios.get(`/api/tax/view_tax`, { headers: { "Access-Control-Allow-Origin": "*", Authorization: `Bearer ${token}`, }, }); if (res.data) { // console.log(res.data) setAllTax(res.data); } }; getAllTax(); }, [token]); useEffect(() => { function getProductDetails() { setLoading(true); axios .get(`/api/product/getAll/admin`, { headers: { "Access-Control-Allow-Origin": "*", Authorization: `Bearer ${token}`, }, }) .then((res) => { setLoading(false); // console.log(res.data.product) setProductData(res.data.product); }) .catch((err) => { setLoading(false); // getBack() }); } function getFranchiseeDetails() { setLoading(true); axios .get(`/api/franchisee`, { headers: { "Access-Control-Allow-Origin": "*", Authorization: `Bearer ${token}`, }, }) .then((res) => { setLoading(false); console.log(res.data.data); setAllFranchisee(res.data.data); }) .catch((err) => { setLoading(false); // getBack() }); } getProductDetails(); getFranchiseeDetails(); }, []); const handleChange = (e) => { if (e.target.type === "text") { setData((prev) => ({ ...prev, [e.target.id]: e.target.value })); } else { setOrderStatus(e.target.value); } }; const handleQuantityChange = (e) => { setData((prev) => ({ ...prev, quantity: e.target.value, total_Price: productDetails?.base_Price * e.target.value, })); }; // ------------------------Frenchisee handle------------------------------// const handleGetSingleFrenchisee = async () => { console.log(getFranchiseeID.current.value); axios .get(`/api/franchisee/arrayspopulate/${getFranchiseeID.current.value}`, { headers: { "Access-Control-Allow-Origin": "*", Authorization: `Bearer ${token}`, }, }) .then((res) => { setLoading(false); console.log(res.data.data); let options = { Franchisee: res?.data?.data?._id, name: res?.data?.data?.name, contact_Number: res?.data?.data?.contact_Number, contact_Person_Name: res?.data?.data?.contact_Person_Name, address: res?.data?.data?.address_line_1 + " " + res?.data?.data?.address_line_2, city: res?.data?.data?.city?.city_name, price_Lable: res?.data?.data?.price_Lable, state: res?.data?.data?.city?.state?.state_name, banner: res?.data?.data?.banner?.url, Franchisee_Url: res?.data?.data?.url, }; dispatch({ type: "addShippingInfo", payload: options }); // localStorage.setItem("shippingInfo", JSON.stringify(AllStates.shipingInfo)); toast.success("Franchisee Added"); }) .catch((err) => { setLoading(false); }); }; const FranchiseeRemove = (id) => { dispatch({ type: "deleteFromshippingInfo", payload: { Franchisee: id }, }); toast.success("Franchisee Removed"); }; // ------------------------Frenchisee handle End------------------------------// // ------------------------product handle------------------------------// const handleGetSingleProduct = async (e) => { axios .get(`/api/product/getOne/${getValue.current.value}`, { headers: { "Access-Control-Allow-Origin": "*", Authorization: `Bearer ${token}`, }, }) .then((res) => { setLoading(false); const productAllkey = Object.keys(res?.data?.product); const productAllValue = Object.values(res?.data?.product); const findIndex1 = productAllkey.indexOf(shipingInfo?.price_Lable); const findIndex2 = productAllkey.indexOf( `${shipingInfo?.price_Lable}_With_Tax` ); let options = { name: res?.data?.product?.name, price: productAllValue[findIndex1], product: res?.data?.product?._id, quantity: 1, image: res?.data?.product?.image?.url, taxId: res?.data?.product?.taxId, price_With_Tax: productAllValue[findIndex2], }; dispatch({ type: "addToCart", payload: options }); dispatch({ type: "calculatePrice" }); toast.success("Product Added"); }) .catch((err) => { setLoading(false); }); }; const handleRemove = (id) => { dispatch({ type: "deleteFromCart", payload: { product: id }, }); dispatch({ type: "calculatePrice" }); toast.success("Item Removed"); }; //increase qty const increaseQuantity = (id) => { dispatch({ type: "addToCart", payload: { product: id }, }); dispatch({ type: "calculatePrice" }); // localStorage.setItem("cartItems", JSON.stringify(AllStates.cart)); }; const decreaseQuantity = (id) => { dispatch({ type: "decrement", payload: { product: id }, }); dispatch({ type: "calculatePrice" }); }; // ------------------------product handle End------------------------------// function handleSubmit() { if (shipingInfo === null) { swal({ title: "Warning", text: "Please select Franchisee ", icon: "error", button: "Close", dangerMode: true, }); return; } else if (cartItems.length < 1) { swal({ title: "Warning", text: "Please select atleast one product", icon: "error", button: "Close", dangerMode: true, }); return; } else if (tax === "" || shippingCharge === "" || total === "") { swal({ title: "Warning", text: "Fill all mandatory fields", icon: "error", button: "Close", dangerMode: true, }); return; } setLoading(true); setLoading(true); axios .post( `/api/order/create`, { orderItems: cartItems, shippingInfo: shipingInfo, shipping_charge: shippingCharge, tax_amount: tax, total_amount: total, }, { headers: { "Access-Control-Allow-Origin": "*", Authorization: `Bearer ${token}`, }, } ) .then((res) => { // console.log(res) swal({ title: "Created", text: res.data.message ? res.data.message : "Order created!", icon: "success", button: "ok", }); setLoading(false); navigate("/orders/new"); }) .catch((error) => { setLoading(false); swal({ title: "Warning", text: error.response.data.message ? error.response.data.message : "Something went wrong!", icon: "error", button: "Retry", dangerMode: true, }); }); } function getBack() { navigate(`/orders/${status}`, { replace: true }); } return ( <> {" "}
Add Order
{shipingInfo !== null && (
{shipingInfo?.name}
{shipingInfo?.name}
Address. : {shipingInfo?.address}

Contact No. : {shipingInfo?.contact_Number}

Contact Person Name :{" "} {shipingInfo?.contact_Person_Name}

Price Lable : {shipingInfo?.price_Lable}


)}
{" "}
{shipingInfo !== null && (
{cartItems && cartItems.map((productDetails, i) => (
{productDetails?.name}
{productDetails?.name}
{productDetails?.quantity}

Price With Tax: ₹ {productDetails?.price_With_Tax}

Price: ₹ {productDetails?.price}


))} {subTotal && (
Total Order Value: ₹{subTotal}
)}
)}
Order Placed On : {productData?.status_timeline?.new ? new Date( productData?.status_timeline?.new ).toLocaleString("en-IN", { month: "short", day: "numeric", year: "numeric", hour: "2-digit", minute: "numeric", hour12: true, }) : new Date(productData?.placed_on).toLocaleString( "en-IN", { month: "short", day: "numeric", year: "numeric", hour: "2-digit", minute: "numeric", hour12: true, } )}
Processing Started : {productData?.status_timeline?.processing ? new Date( productData?.status_timeline?.processing ).toLocaleString("en-IN", { month: "short", day: "numeric", year: "numeric", hour: "2-digit", minute: "numeric", hour12: true, }) : "-"}
Dispatched On : {productData?.status_timeline?.dispatched ? new Date( productData?.status_timeline?.dispatched ).toLocaleString("en-IN", { month: "short", day: "numeric", year: "numeric", hour: "2-digit", minute: "numeric", hour12: true, }) : "-"}
Delivered On : {productData?.status_timeline?.delivered ? new Date( productData?.status_timeline?.delivered ).toLocaleString("en-IN", { month: "short", day: "numeric", year: "numeric", hour: "2-digit", minute: "numeric", hour12: true, }) : "-"}
Cancelled On : {productData?.status_timeline?.cancelled ? new Date( productData?.status_timeline?.cancelled ).toLocaleString("en-IN", { month: "short", day: "numeric", year: "numeric", hour: "2-digit", minute: "numeric", hour12: true, }) : "-"}
Returned On : {productData?.status_timeline?.returned ? new Date( productData?.status_timeline?.returned ).toLocaleString("en-IN", { month: "short", day: "numeric", year: "numeric", hour: "2-digit", minute: "numeric", hour12: true, }) : "-"}
{/* */}
); } export default AddOrder;