Enhancing Dashboards: Dynamic User Statuses and Interactive Notifications in React
Introduction
Stale data and persistent notifications can quickly make a dashboard frustrating to use. For the Consejo Departamental de Sistemas frontend application, improving the real-time visibility of user statuses and streamlining notification management was a key focus. This post delves into the recent enhancements made to the application's dashboard, focusing on dynamic data display and user-friendly notification controls.
Updating the Dashboard UI for Dynamic Statuses
The core of this update involved refactoring the Dashboard.jsx component to dynamically display the statuses of "consejeros" (counselors/advisors). This means fetching current data and rendering it efficiently to show who is active and who is on leave. This approach ensures that the dashboard always reflects the most up-to-date information without requiring a page refresh.
Fetching Data with Axios and React
To power the dynamic display, the application leverages Axios for asynchronous data fetching. A common pattern in React applications is to use the useEffect hook to make API calls when the component mounts or when specific dependencies change.
import React, { useState, useEffect } from 'react';
import axios from 'axios';
function ConsejeroStatusList() {
const [consejeros, setConsejeros] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
const fetchStatuses = async () => {
try {
const response = await axios.get('/api/consejeros/status'); // Generic API endpoint
setConsejeros(response.data);
} catch (err) {
setError(err);
} finally {
setLoading(false);
}
};
fetchStatuses();
}, []); // Empty dependency array means this runs once on mount
if (loading) return <div>Loading consejeros statuses...</div>;
if (error) return <div>Error: {error.message}</div>;
return (
<div>
<h3>Current Consejeros</h3>
<ul>
{consejeros.map(consejero => (
<li key={consejero.id}>
{consejero.name}: <span className={`status-${consejero.status.toLowerCase()}`}>{consejero.status}</span>
</li>
))}
</ul>
</div>
);
}
This snippet demonstrates a typical flow: fetching data, handling loading and error states, and then rendering a list of "consejeros" with their respective statuses. Styling (e.g., status-active, status-on-leave) can then be applied based on the status for visual clarity.
Implementing Interactive Notification Dismissal
Another crucial enhancement was the addition of an intuitive way to manage notifications directly from the dashboard. An icon was integrated to allow users to close or dismiss notifications, improving the dashboard's cleanliness and user experience. This involves managing notification visibility within the component's local state.
Managing Notification State
When a user clicks the close icon, the corresponding notification needs to be removed from the UI. This is typically achieved by updating the component's state to filter out the dismissed notification.
import React, { useState } from 'react';
function DashboardNotifications() {
const [notifications, setNotifications] = useState([
{ id: '1a', message: 'Welcome to the updated dashboard!', type: 'info' },
{ id: '2b', message: 'John Doe is now on leave.', type: 'alert' }
]);
const handleDismissNotification = (idToDismiss) => {
setNotifications(prevNotifications =>
prevNotifications.filter(note => note.id !== idToDismiss)
);
};
return (
<div className="notifications-area">
{notifications.map(notification => (
<div key={notification.id} className={`notification ${notification.type}`}>
<span>{notification.message}</span>
<button
onClick={() => handleDismissNotification(notification.id)}
aria-label="Dismiss notification"
className="dismiss-button"
>
× {/* HTML entity for 'x' */}
</button>
</div>
))}
</div>
);
}
This DashboardNotifications component renders a list of active notifications. When the dismiss button is clicked, handleDismissNotification updates the notifications state, causing React to re-render the component without the dismissed item. This provides immediate visual feedback and a cleaner interface.
Results
The updates to the Consejo Departamental de Sistemas dashboard significantly improve its usability. Users now benefit from a dashboard that provides up-to-the-minute information on "consejeros" statuses and allows for quick, interactive management of notifications, reducing clutter and enhancing focus. The dashboard feels more responsive and informative, leading to a better overall experience.
Next Steps
Future enhancements could include implementing WebSocket connections for true real-time updates of "consejeros" statuses, removing the need for polling. Additionally, integrating a global notification store (e.g., with Redux or React Context) could provide a more centralized and scalable approach to managing notifications across the entire application.
Generated with Gitvlg.com