FeedbackStudentSelect Component
Overview
FeedbackStudentSelect is a React component that enables students to select one of their enrolled courses and navigate to a feedback form. It: - Retrieves the current student's ID from localStorage - Fetches their enrolled courses using React Query - Displays each course in a styled card format - Navigates to the feedback submission screen on button click
Dependencies
- React Router DOM: useNavigate – for client-side route transitions useQuery – for managing server state and data fetching
- newRequest: an Axios-like instance used for API calls.
- react-icons/fa: for FontAwesome-style icons used in the UI (FaBookOpen, FaClipboardList, FaBullhorn, etc.)
- localStorage.getItem("currentUser"): used to retrieve the logged-in user’s info.
Event Handlers
- handleFeedback: Handles navigation to the feedback form for the selected course using navigate() and passes course details in the state.
const handleFeedback = (course) => {
navigate('/student/feedback/submit', {
state: {
courseId: course.id,
courseName: course.name,
credits: course.credits,
}
});
};
Data Fetching
- Retrieve Current Student Info
const {data:userData} = JSON.parse(localStorage.getItem("currentUser")); const {userId} = userData.user; - Parses user data from local storage
-
Extracts the student’s userId for backend use
-
Fetch Courses with React Query
const { isLoading, error, data: studentCourses = [] } = useQuery({ queryKey: ["courses"], queryFn: () => newRequest.get(`/student/${userId}/courses`).then((res) => { return res.data.courses || []; }), }); - Caches data with key ["courses"]
- Queries GET /student/:userId/courses
- Defaults to an empty array if no data is returned
UI Structure
Rendered layout includes: - Header Section - Title and instructions - Conditional Views - Loading Spinner - Error Display - No Courses Message - Course Cards (Grid) - Course Header: name, icon, ID - Course Stats: credits, assignments, attendance
- Feedback Button
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
{studentCourses.map((course) => (
<div key={course.id} className="course-card">
{/* Header, Stats, Button */}
</div>
))}
</div>
Loading and Error States
- Loading: Displays an animated spinner with Tailwind classes:
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-pink-500 mx-auto mb-4"></div>
<p>Loading your courses...</p>
- Error: Displays the error.message or a fallback:
<p className="text-red-700 text-lg mb-4">{error.message || "Failed to fetch courses"}</p>
- No Courses: Gracefully handles the case where the student has no enrolled courses:
<p className="text-gray-700 text-lg mb-4">You are not enrolled in any courses.</p>
Scope of Improvement
Area Suggestion ------------ --------------- LocalStorage Parsing Use a helper function to extract user ID safely and handle malformed data Error Granularity Distinguish between network errors, unauthorized access, and empty data Accessibility Add semantic roles, ARIA attributes, and alt text for better screen reader support
Usage
- Route Setup: Make sure the following route exists:
<Route path="/student/feedback/select" element={<FeedbackStudentSelect />} />
-
Auth Requirements: This component should be protected with an authentication guard (PrivateRoute or similar) since it depends on local user data.
-
Navigation Trigger: Usually launched after login or from a student dashboard:
navigate('/student/feedback/select');