From 8676af87da33bb3f48b30f06a36e3adcab2a7ccc Mon Sep 17 00:00:00 2001 From: Ansh101112 Date: Sun, 23 Jun 2024 18:07:06 +0530 Subject: [PATCH] dashboard update added --- src/Dashboards/UserD.js | 78 +++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 14 deletions(-) diff --git a/src/Dashboards/UserD.js b/src/Dashboards/UserD.js index 20ab328..1a0bf08 100644 --- a/src/Dashboards/UserD.js +++ b/src/Dashboards/UserD.js @@ -2,6 +2,7 @@ import React, { useEffect, useState } from "react"; import Navbar from "../components/Navbar"; import { toast, ToastContainer } from "react-toastify"; import { useNavigate } from "react-router-dom"; +import { format, parseISO } from "date-fns"; import axios from "axios"; import { Link } from "react-router-dom"; @@ -15,16 +16,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 +61,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 +94,9 @@ const UserD = () => {

Happy Saving!!

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