diff --git a/src/api/api.ts b/src/api/api.ts index e530877..08da243 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -1,6 +1,7 @@ import { API_URL } from '@/constants/constants'; import type { IActivity } from '@/types/IActivity'; import type { IClassroom } from '@/types/IClassroomCard'; +import type { IUser } from '@/types/IUser'; const getAuthHeaders = (): Record => { const token = typeof window !== 'undefined' ? localStorage.getItem('token') : null; @@ -31,6 +32,18 @@ export const api = { return res.json(); }, + getMembers: async (id: string): Promise => { + const res = await fetch(`${API_URL}/classrooms/${encodeURIComponent(id)}/members`, { + headers: { + ...getAuthHeaders() + } + }); + + if (!res.ok) throw new Error('Failed to fetch classroom'); + return res.json(); + }, + + create: async (data: { name: string; description: string; diff --git a/src/components/modals/JoinClassModal.tsx b/src/components/modals/JoinClassModal.tsx index 603d43a..f6f98b0 100644 --- a/src/components/modals/JoinClassModal.tsx +++ b/src/components/modals/JoinClassModal.tsx @@ -51,6 +51,7 @@ export default function JoinClassModal({ isOpen, onClose, onClassroomJoined }: R setIsLoading(true); try { + await api.classroom.join(code); const updatedClassrooms = await api.classroom.getAll(); onClassroomJoined?.(updatedClassrooms); diff --git a/src/components/screens/ClassScreen.tsx b/src/components/screens/ClassScreen.tsx index cf32bd3..d0151f9 100644 --- a/src/components/screens/ClassScreen.tsx +++ b/src/components/screens/ClassScreen.tsx @@ -31,12 +31,15 @@ import { useEffect, useState } from 'react'; import { FiCode, FiFileText, FiPlus, FiUsers } from 'react-icons/fi'; import ActivityCard from '../general/ActivityCard'; import CreateActivityModal from '../modals/CreateActivityModal'; +import type { IUser } from '@/types/IUser'; export default function ClassScreen({ id }: Readonly<{ id: string }>) { const [classroom, setClassroom] = useState(null); const [isLoading, setIsLoading] = useState(true); const router = useRouter(); const [classActivities, setClassActivities] = useState([]); + const [classMembers, setClassMembers] = useState([]); + const [isMembersLoading, setIsMembersLoading] = useState(false); const { isOpen, onOpen, onClose } = useDisclosure(); const toast = useToast(); @@ -72,6 +75,28 @@ export default function ClassScreen({ id }: Readonly<{ id: string }>) { setClassActivities((prev) => [...prev, activity]); }; + const handleTabChange = async (index: number) => { + // Index 1 is the "Estudiantes" tab + if (index === 1 && classMembers.length === 0) { + setIsMembersLoading(true); + try { + const members = await api.classroom.getMembers(id); + setClassMembers(members); + } catch (error) { + toast({ + title: 'Error', + description: 'No se pudo cargar la lista de estudiantes', + status: 'error', + position: 'top-right', + duration: 3000, + isClosable: true + }); + } finally { + setIsMembersLoading(false); + } + } + }; + if (isLoading) { return ( @@ -125,7 +150,7 @@ export default function ClassScreen({ id }: Readonly<{ id: string }>) { - 24 estudiantes + {classMembers.length || '...'} estudiantes @@ -148,7 +173,7 @@ export default function ClassScreen({ id }: Readonly<{ id: string }>) { - + ) { Estudiantes - - + + + ) : ( + - - - Ángel - - Profesor - - - - - {Array.from({ length: 5 }).map((_, i) => ( - - - - Estudiante {i + 1} - - Estudiante - - - - ))} - + {classMembers.map((member) => ( + + + + {member.username} + {member.id === classroom.owner ? ( + + Profesor + + ) : ( + + Estudiante + + )} + + + ))} + + )}