From db9a2dbdace31f1650a929dde4ef7e2db9be138b Mon Sep 17 00:00:00 2001 From: Min Thwin Khant <60309448+MinKhant01@users.noreply.github.com> Date: Mon, 9 Dec 2024 17:56:31 -0800 Subject: [PATCH] fix create product for ManagerDashboard --- client/src/Components/ManagerDashboard.js | 307 ++++++++++++++++------ 1 file changed, 224 insertions(+), 83 deletions(-) diff --git a/client/src/Components/ManagerDashboard.js b/client/src/Components/ManagerDashboard.js index 632e1cb..e76e099 100644 --- a/client/src/Components/ManagerDashboard.js +++ b/client/src/Components/ManagerDashboard.js @@ -38,7 +38,8 @@ function ManagerDashboard() { const [newPassword, setNewPassword] = useState(""); const [confirmNewPassword, setConfirmNewPassword] = useState(""); const [showPasswordTooltip, setShowPasswordTooltip] = useState(false); - const [showConfirmPasswordTooltip, setShowConfirmPasswordTooltip] = useState(false); + const [showConfirmPasswordTooltip, setShowConfirmPasswordTooltip] = + useState(false); const [passwordFocused, setPasswordFocused] = useState(false); const [confirmPasswordFocused, setConfirmPasswordFocused] = useState(false); const [statistics, setStatistics] = useState({ @@ -158,7 +159,7 @@ function ManagerDashboard() { const regex = /^\d{0,6}(\.\d{0,2})?$/; if (regex.test(value)) { // Trim leading zeroes - value = value.replace(/^0+(?!\.|$)/, ''); + value = value.replace(/^0+(?!\.|$)/, ""); setAccountFormData({ ...accountFormData, salary: value, @@ -184,11 +185,15 @@ function ManagerDashboard() { return; } if (!validateSalary(accountFormData.salary)) { - alert("Salary must be a number with 2 to 6 digits before the decimal and up to 2 digits after the decimal."); + alert( + "Salary must be a number with 2 to 6 digits before the decimal and up to 2 digits after the decimal." + ); return; } if (!validateStartDate(accountFormData.startDate)) { - alert("Start date must be within the last 5 years and not more than 1 year ahead."); + alert( + "Start date must be within the last 5 years and not more than 1 year ahead." + ); return; } const response = await axios.post( @@ -204,7 +209,7 @@ function ManagerDashboard() { }, { withCredentials: true } ); - console.log("Registration response:", response.data); + // console.log("Registration response:", response.data); setOneTimePassword(response.data.oneTimePassword); setShowPasswordModal(true); alert("Employee added successfully!"); @@ -239,7 +244,7 @@ function ManagerDashboard() { }, { withCredentials: true } ); - console.log("Registration response:", res.data); + // console.log("Registration response:", res.data); setOneTimePassword(res.data.oneTimePassword); setShowPasswordModal(true); alert("Supplier added successfully!"); @@ -268,11 +273,15 @@ function ManagerDashboard() { return; } if (!validateSalary(accountFormData.salary)) { - alert("Salary must be a number with 2 to 6 digits before the decimal and up to 2 digits after the decimal."); + alert( + "Salary must be a number with 2 to 6 digits before the decimal and up to 2 digits after the decimal." + ); return; } if (!validateStartDate(accountFormData.startDate)) { - alert("Start date must be within the last 5 years and not more than 1 year ahead."); + alert( + "Start date must be within the last 5 years and not more than 1 year ahead." + ); return; } if (accountType === "supplier") { @@ -284,7 +293,7 @@ function ManagerDashboard() { }, { withCredentials: true } ); - console.log("Registration response:", res.data); + // console.log("Registration response:", res.data); setOneTimePassword(res.data.oneTimePassword); setShowPasswordModal(true); alert("Supplier added successfully!"); @@ -302,7 +311,7 @@ function ManagerDashboard() { }, { withCredentials: true } ); - console.log("Registration response:", response.data); + // console.log("Registration response:", response.data); setOneTimePassword(response.data.oneTimePassword); setShowPasswordModal(true); alert("Employee added successfully!"); @@ -341,18 +350,18 @@ function ManagerDashboard() { e.preventDefault(); // Log the form values first - console.log("Form Values:", { - categoryId: e.target.categoryId.value, - productName: e.target.productName.value, - productDescription: e.target.productDescription.value, - brand: e.target.brand.value, - pictureUrl: e.target.pictureUrl.value, - quantity: e.target.quantity.value, - reorderLevel: e.target.reorderLevel.value, - // reorderQuantity: e.target.reorderQuantity.value, - price: e.target.price.value, - weight: e.target.weight.value, - }); + // console.log("Form Values:", { + // categoryId: e.target.categoryId.value, + // productName: e.target.productName.value, + // productDescription: e.target.productDescription.value, + // brand: e.target.brand.value, + // pictureUrl: e.target.pictureUrl.value, + // quantity: e.target.quantity.value, + // reorderLevel: e.target.reorderLevel.value, + // // reorderQuantity: e.target.reorderQuantity.value, + // price: e.target.price.value, + // weight: e.target.weight.value, + // }); const productData = { categoryId: Number(e.target.categoryId.value), // Changed to Number() @@ -368,11 +377,11 @@ function ManagerDashboard() { }; // Log the formatted data - console.log("Formatted Product Data:", productData); + // console.log("Formatted Product Data:", productData); try { // Log the request - console.log("Sending request to:", "http://localhost:8080/products"); + // console.log("Sending request to:", "http://localhost:8080/products"); const response = await axios.post( "http://localhost:8080/products", @@ -386,7 +395,7 @@ function ManagerDashboard() { ); // Log the response - console.log("Server Response:", response); + // console.log("Server Response:", response); if (response.status === 201) { toggleCreateProductModal(); @@ -472,7 +481,7 @@ function ManagerDashboard() { const handleDeleteProduct = async (productId) => { if (window.confirm("Are you sure you want to delete this product?")) { try { - console.log("Attempting to delete product:", productId); + // console.log("Attempting to delete product:", productId); if (!productId) { throw new Error("Invalid product ID"); @@ -541,7 +550,7 @@ function ManagerDashboard() { weight: Number(editingProduct.WEIGHT), }; - console.log("Sending update with data:", formData); // Debug log + // console.log("Sending update with data:", formData); // Debug log const response = await axios.put( `http://localhost:8080/products/${editingProduct.ID}`, @@ -576,6 +585,30 @@ function ManagerDashboard() { } }; + const handlePriceChange = (e) => { + let value = e.target.value; + const regex = /^\d{0,3}(\.\d{0,2})?$/; + if (regex.test(value)) { + value = value.replace(/[+-]/g, ""); // Prevent typing positive or negative signs + setEditingProduct({ + ...editingProduct, + PRICE: value, + }); + } + }; + + const handleWeightChange = (e) => { + let value = e.target.value; + const regex = /^\d{0,3}(\.\d{0,2})?$/; + if (regex.test(value)) { + value = value.replace(/[+-]/g, ""); // Prevent typing positive or negative signs + setEditingProduct({ + ...editingProduct, + WEIGHT: value, + }); + } + }; + useEffect(() => { const fetchAllSales = async () => { try { @@ -829,12 +862,23 @@ function ManagerDashboard() { }; const isSupplierFormValid = () => { - return ( - validateEmail(accountFormData.email) && - accountFormData.supplierName - ); + return validateEmail(accountFormData.email) && accountFormData.supplierName; }; + // var out = document.querySelector(".foo"); + + // if (out) { // Check if the element with class .foo exists + // console.log(out); // Log the element to the console + // let text = out.textContent; // Get the inner HTML of the element + // console.log(text); // Log the inner HTML + + // let str = text.replace(/(.{20})/g, '
'); // Replace every 10 characters with
+ // out.innerHTML = str; // Set the modified HTML back to the element + // console.log("Found!"); + // } else { + // console.log("Element with class '.foo' not found."); + // } + return (
{/* Welcome Banner */} @@ -1348,7 +1392,7 @@ function ManagerDashboard() { {product.BRAND} - + {product.PRODUCTDESCRIPTION} @@ -1449,7 +1493,7 @@ function ManagerDashboard() { onChange={(e) => setNewStatus(e.target.value)} > - + {/* */} @@ -1523,17 +1567,29 @@ function ManagerDashboard() {
-
+ {accountType === "supplier" ? ( // Supplier Form <>
@@ -1547,7 +1603,12 @@ function ManagerDashboard() { /> @@ -1555,7 +1616,8 @@ function ManagerDashboard() {
0 }, + { + text: "Supplier name is required.", + valid: + accountFormData.supplierName.length > + 0, + }, ]} - visible={supplierNameFocused && accountFormData.supplierName.length > 0} + visible={ + supplierNameFocused && + accountFormData.supplierName.length > 0 + } />
) : ( // Employee Form <> -
+
0 }, + { + text: "Salary must be greater than zero", + valid: + parseFloat(accountFormData.salary) > + 0, + }, ]} - visible={showSalaryTooltip && !validateSalary(accountFormData.salary)} + visible={ + showSalaryTooltip && + !validateSalary(accountFormData.salary) + } />
setAccountFormData({ ...accountFormData, startDate: e.target.value })} + onChange={(e) => + setAccountFormData({ + ...accountFormData, + startDate: e.target.value, + }) + } onBlur={(e) => { const date = e.target.value; if (date && !validateStartDate(date)) { setShowStartDateTooltip(true); - alert("Start date must be within the last 5 years and not more than 1 year ahead."); + alert( + "Start date must be within the last 5 years and not more than 1 year ahead." + ); } else { setShowStartDateTooltip(false); } @@ -1733,7 +1836,12 @@ function ManagerDashboard() { /> @@ -1750,7 +1858,11 @@ function ManagerDashboard() { setShowEmailTooltip(true); } }} - disabled={accountType === "supplier" ? !isSupplierFormValid() : !isEmployeeFormValid()} + disabled={ + accountType === "supplier" + ? !isSupplierFormValid() + : !isEmployeeFormValid() + } > Add{" "} {accountType === "supplier" @@ -1994,6 +2106,8 @@ function ManagerDashboard() { className="form-control" placeholder="e.g. Apples" required + minlength="1" + maxlength="25" />
@@ -2005,6 +2119,8 @@ function ManagerDashboard() { className="form-control" placeholder="e.g. Fresh red apples" required + minlength="1" + maxlength="255" >
@@ -2015,6 +2131,8 @@ function ManagerDashboard() { className="form-control" placeholder="e.g. Fresh Foods" required + minlength="1" + maxlength="15" />
@@ -2036,6 +2154,7 @@ function ManagerDashboard() { name="quantity" className="form-control" min="0" + max="200" step="1" placeholder="e.g. 100" required @@ -2050,6 +2169,7 @@ function ManagerDashboard() { name="reorderLevel" className="form-control" min="0" + max="200" step="1" placeholder="e.g. 20" required @@ -2059,27 +2179,31 @@ function ManagerDashboard() {
+ className="form-control" + value={editingProduct.PRICE} + onChange={handlePriceChange} + />