Student Profile Component Documentation
Overview
The StudentProfile component is a React functional component that fetches and displays comprehensive details of a student, including personal information and completed courses. It handles loading and error states using React Query.
File Location
File Name: StudentProfile.js (or .jsx)
Dependencies
- React
- React Query (
@tanstack/react-query): For data fetching and state management. - newRequest: Custom HTTP client imported from
../../utils/newRequest. - CSS Module:
ProfilePage.module.cssfor scoped styling.
Data Fetching
- Retrieves the current user from
localStorage(currentUser) to extractuserIdandemail. - Uses
useQueryto fetch student details from/student/{userId}. - Uses a second
useQueryto fetch completed courses from/student/{userId}/completed-courses.
Loading and error states (isLoading, error, isLoadingCourses, errorCourses) are used to conditionally render content.
Data Mapping
Maps API response into a student object:
rollNumber,name,photo,signphoto,hostel,emailBloodgr,contactno,dob,roomNo,semesterbranch,yearOfJoining,programme,courses
Defaults:
- Placeholder images (
/student.jpg,/sign.jpg) if none provided. batch.substr(0, 4)to extract joining year.
JSX Structure
- Root Fragment with conditional rendering:
- Loading and error messages for student data and courses.
- Profile Container (
styles.profileContainer): <img>tags for profile photo and signature.- Profile Header (
styles.profileHeader):- Empty
<div/>for layout, then profile info (styles.profileInfo) listing personal details.
- Empty
- Courses Section (
styles.courseSection): - Table of completed courses with columns: Course Code, Name, Department, Credit/Audit, Semester, Credits, Grade.
Usage Example
import StudentProfile from './StudentProfile';
function App() {
return <StudentProfile />;
}
Notes & Improvements
- Console Logs: Remove
console.logstatements in production. - Query Keys: Use descriptive keys like
['student', userId]and['completedCourses', userId]. - Error Handling: Show distinct error messages for courses (
errorCourses) instead of repeatingerror. - Type Safety: Convert to TypeScript and define interfaces for API responses.
- Accessibility: Add
alttext for images and proper table semantics.