AttendanceApprovalDashboard
Overview
The AttendanceApprovalDashboard is a React component that provides an interface for administrators to review and approve pending student attendance records. The dashboard allows filtering by course and includes functionality for both individual and batch approval operations.
State Management
The component uses React's useState hook to manage several state variables:
| State Variable | Type | Description |
|---|---|---|
isApprovalPanelOpen |
Boolean | Controls the visibility of the approval panel UI |
courses |
Array | Stores the list of available courses fetched from the API |
attendanceRequests |
Array | Stores the list of attendance requests pending approval |
loading |
Object | Tracks loading states for courses and approvals data separately |
error |
String/null | Stores error messages if API requests fail |
selectedCourse |
String | Stores the course code selected for filtering |
Data Fetching
The component uses useEffect to fetch two main data sets when the component mounts:
- Course data from
/api/attendancelanding/admin - Attendance approval requests from
/api/attendancelanding/admin/approval
Core Functionality
Approval Operations
The component provides two approval methods:
- Individual Approval -
handleApprove(request): Approves a single attendance record - Bulk Approval -
handleApproveAll(): Approves all currently filtered attendance records
Both methods: - Send PATCH requests to the API - Update the local state to reflect changes immediately - Handle error cases with appropriate user feedback
Filtering
The component allows filtering attendance requests by course:
- The
selectedCoursestate controls which course is currently selected - The
filteredRequestsderived value applies this filter to the attendance requests - When no course is selected, all pending requests are shown
UI Features
- Toggle panel for showing/hiding the approval interface
- Badge showing the number of pending approvals
- Dropdown filter for course selection
- Table displaying attendance requests with student ID, course, date, status, and action button
- Color-coded status indicators for pending and approved requests
- Buttons for individual and bulk approval actions
Code Breakdown
Component Initialization and Data Fetching
const AttendanceApprovalDashboard = () => {
// State initialization
const [isApprovalPanelOpen, setIsApprovalPanelOpen] = useState(false);
const [courses, setCourses] = useState([]);
const [attendanceRequests, setAttendanceRequests] = useState([]);
const [loading, setLoading] = useState({
courses: true,
approvals: true
});
const [error, setError] = useState(null);
const [selectedCourse, setSelectedCourse] = useState("");
// Data fetching on component mount
useEffect(() => {
const fetchData = async () => {
try {
// Fetch courses and approval requests...
setLoading({ courses: false, approvals: false });
} catch (err) {
setError(err.message);
// Error handling...
}
};
fetchData();
}, []);
// Rest of component...
}
The initial useEffect hook runs once when the component mounts, fetching both courses and attendance approval requests in parallel.
Approval Handlers
Individual Approval
const handleApprove = async (request) => {
try {
const response = await fetch('http://localhost:8000/api/attendancelanding/admin/approval', {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
courseCode: request.courseId,
rollNo: request.studentId,
date: request.date
})
});
if (!response.ok) throw new Error('Approval failed');
// Update local state...
} catch (err) {
// Error handling...
}
};
When an individual approval button is clicked, this handler: 1. Sends a PATCH request to the API with the request details 2. Updates only the specific approved item in the state 3. Handles and displays any errors
Bulk Approval
const handleApproveAll = async () => {
try {
const approvalPromises = filteredRequests.map(request =>
fetch('http://localhost:8000/api/attendancelanding/admin/approval', {
method: 'PATCH',
// Request details...
})
);
const results = await Promise.all(approvalPromises);
// Check results and update state...
} catch (err) {
// Error handling...
}
};
This handler:
1. Creates an array of fetch promises for all filtered requests
2. Uses Promise.all() to process them in parallel
3. Updates the state with all successfully approved requests
4. Provides error feedback if any approvals fail
Helper Functions and Computed Values
// Filter attendance requests by selected course
const filteredRequests = selectedCourse
? attendanceRequests.filter(request => request.courseId === selectedCourse && request.pendingApproval)
: attendanceRequests.filter(request => request.pendingApproval);
// Get course name by ID
const getCourseName = (id) => {
const course = courses.find(c => c.courseCode === id);
return course ? course.courseName : "Unknown Course";
};
// Toggle the approval panel
const toggleApprovalPanel = () => {
setIsApprovalPanelOpen(!isApprovalPanelOpen);
};
These functions: - Filter attendance requests based on selected course - Lookup course names from course IDs - Toggle the UI panel visibility
UI Rendering
The component conditionally renders: 1. Loading indicator when data is being fetched 2. Error message if data fetching fails 3. Main dashboard UI with: - Toggle button for the approval panel - Course filter dropdown - Data table showing attendance requests - Approve buttons for individual and bulk actions
API Integration
| Endpoint | Method | Purpose | Request Data |
|---|---|---|---|
/api/attendancelanding/admin |
GET | Fetch course list | N/A |
/api/attendancelanding/admin/approval |
GET | Fetch approval requests | N/A |
/api/attendancelanding/admin/approval |
PATCH | Approve attendance records | { courseCode, rollNo, date } |
Debug Features
The component includes a debug useEffect hook that logs the current state of filtered requests and all attendance requests to the console whenever they change:
useEffect(() => {
console.log("Current filtered requests:", filteredRequests);
console.log("All attendance requests:", attendanceRequests);
}, [filteredRequests, attendanceRequests]);
This aids in development and troubleshooting but could be removed in a production environment.
Styling
The component uses Tailwind CSS classes for styling, including: - Responsive container layout - Card-like panel with shadow - Color-coded status indicators - Responsive table layout - Styled buttons and dropdowns
Usage Notes
- This component is intended for administrative users who have permission to approve attendance records
- The component assumes the existence of backend API endpoints for fetching and approving attendance data
- Error handling is implemented to provide feedback if API calls fail
- The UI is designed to be responsive and user-friendly
- Debug information is logged to the console for development purposes