import React, { useState, useEffect } from "react"; import { Modal, Button, Form, Card } from "react-bootstrap"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faEdit, faTrash } from "@fortawesome/free-solid-svg-icons"; function DashboardView({onDashboardCharts}) { const [showTable, setShowTable] = useState(false); const [dashboards, setDashboards] = useState([]); const [showAddItemPopup, setShowAddItemPopup] = useState(false); const [newItem, setNewItem] = useState({ dashboardName: "", description: "", securityProfile: "", addToHome: "", }); const [selectedItemIndex, setSelectedItemIndex] = useState(null); const [columns, setColumns] = useState([ { label: "Dashboard Name", key: "dashboardName", visible: true }, { label: "Description", key: "description", visible: true }, { label: "Security Profile", key: "securityProfile", visible: true }, { label: "Add to Home", key: "addToHome", visible: true }, ]); const [showManageColumnsModal, setShowManageColumnsModal] = useState(false); const [recordsPerPage, setRecordsPerPage] = useState(10); // Initial number of records per page const [error, setError] = useState(null); // To handle error state useEffect(() => { if (showTable) { fetchData(); } else { fetchDashboardCardData(); } }, [showTable]); const fetchData = async () => { <<<<<<< HEAD const apiUrl = `${process.env.REACT_APP_API_URL}/getNotificationByUser`; ======= const apiUrl = `${process.env.REACT_APP_API_URL}getNotificationByUser`; >>>>>>> 1c0592d (commit new code) const token = localStorage.getItem("authToken"); if (!token) { console.error("Authorization token is missing."); setError("Authorization token is missing."); return; } try { const response = await fetch(apiUrl, { headers: { Authorization: `Bearer ${token}`, }, }); if (!response.ok) { throw new Error(`Error: ${response.status}`); } const data = await response.json(); setDashboards(data); } catch (error) { console.error("Fetching error:", error); setError(error.toString()); } }; const fetchDashboardCardData = async () => { <<<<<<< HEAD const apiUrl = `${process.env.REACT_APP_API_URL}/get_Dashboard_header`; ======= const apiUrl = `${process.env.REACT_APP_API_URL}get_Dashboard_header`; >>>>>>> 1c0592d (commit new code) const token = localStorage.getItem("authToken"); if (!token) { console.error("Authorization token is missing."); setError("Authorization token is missing."); return; } try { const response = await fetch(apiUrl, { headers: { Authorization: `Bearer ${token}`, }, }); if (!response.ok) { throw new Error(`Error: ${response.status}`); } const data = await response.json(); const testingDashboard = data.find( (dashboard) => dashboard.dashboard_name === "Testing Dashboard" ); setDashboards(testingDashboard ? [testingDashboard] : []); } catch (error) { console.error("Fetching error:", error); setError(error.toString()); } }; const handleAddItemClick = () => { setShowAddItemPopup(true); }; const handleInputChange = (event) => { const { name, value } = event.target; setNewItem({ ...newItem, [name]: value }); }; const handleAddItem = () => { if (selectedItemIndex !== null) { const updatedDashboards = [...dashboards]; updatedDashboards[selectedItemIndex] = newItem; setDashboards(updatedDashboards); } else { setDashboards([...dashboards, newItem]); } setNewItem({ dashboardName: "", description: "", securityProfile: "", addToHome: "", }); setSelectedItemIndex(null); setShowAddItemPopup(false); }; const handleDeleteItem = (index) => { const updatedDashboards = [...dashboards]; updatedDashboards.splice(index, 1); setDashboards(updatedDashboards); }; const handleColumnVisibilityChange = (key) => { const updatedColumns = columns.map((col) => { if (col.key === key) { return { ...col, visible: !col.visible }; } return col; }); setColumns(updatedColumns); }; const handleRecordsPerPageChange = (value) => { setRecordsPerPage(value); }; return (
| {column.label} | ))}Action |
|---|---|
| {dashboard[column.key]} | ))}