diff --git a/src/Dashboards/UserD.js b/src/Dashboards/UserD.js index 20ab328..6cb7f64 100644 --- a/src/Dashboards/UserD.js +++ b/src/Dashboards/UserD.js @@ -15,16 +15,22 @@ const UserD = () => { const fetchData = async () => { try { const token = localStorage.getItem("token"); - const userResponse = await axios.get("https://smartserver-production.up.railway.app/users", { - headers: { - Authorization: `Bearer ${token}`, - }, - }); - const productResponse = await axios.get("https://smartserver-production.up.railway.app/products", { - headers: { - Authorization: `Bearer ${token}`, - }, - }); + const userResponse = await axios.get( + "https://smartserver-production.up.railway.app/users", + { + headers: { + Authorization: `Bearer ${token}`, + }, + } + ); + const productResponse = await axios.get( + "https://smartserver-production.up.railway.app/products", + { + headers: { + Authorization: `Bearer ${token}`, + }, + } + ); setCurrentUser(userResponse.data); setProducts(productResponse.data); } catch (error) { @@ -54,6 +60,28 @@ const UserD = () => { } }; + const handleDelete = async (productId) => { + try { + const token = localStorage.getItem("token"); + const response = await axios.delete( + `https://smartserver-production.up.railway.app/products/${productId}`, + { + headers: { + Authorization: `Bearer ${token}`, + }, + } + ); + if (response.status === 200) { + setProducts(products.filter((product) => product.id !== productId)); + toast.success("Product deleted successfully."); + } else { + toast.error("Failed to delete product."); + } + } catch (error) { + toast.error("Error deleting product. Try again."); + } + }; + if (!isLoggedIn) { return null; } @@ -65,7 +93,9 @@ const UserD = () => {

Happy Saving!!

{currentUser && ( -
Welcome, {currentUser.name}!
+
+ Welcome, {currentUser.name}! +
)}
@@ -184,9 +214,28 @@ const UserD = () => {