diff --git a/frontend/src/components/TimeLogsPage/InstructorTimeLogsPage.jsx b/frontend/src/components/TimeLogsPage/InstructorTimeLogsPage.jsx index 004ba52..5370f6c 100644 --- a/frontend/src/components/TimeLogsPage/InstructorTimeLogsPage.jsx +++ b/frontend/src/components/TimeLogsPage/InstructorTimeLogsPage.jsx @@ -3,6 +3,7 @@ import { withRouter } from 'react-router-dom' import { TimeLogsSelectForm } from './TimeLogsSelectForm' import { TimeLogRow } from './TimeLogRow' +import LoadingSpinner from '../common/LoadingSpinner' import userService from '../../services/user' import groupManagementService from '../../services/groupManagement' @@ -13,8 +14,26 @@ const InstructorTimeLogsPage = () => { const [allStudents, setAllStudents] = useState([]) const [allLogs, setAllLogs] = useState(null) const [selectedGroup, setSelectedGroup] = useState(null) + const [allGroups, setAllGroups] = useState([]) + + const [isLoading, setIsLoading] = useState(true) useEffect(() => { + const fetchAllGroups = async () => { + try { + const allGroups = await groupManagementService.get() + setAllGroups(allGroups) + } catch (error) { + console.error( + 'Error fetching groups:', + error.message, + ' / ', + error.response.data.error + ) + notificationActions.setError(error.response.data.error) + } + } + const fetchAllStudents = async () => { try { const allStudents = await userService.get() @@ -46,45 +65,33 @@ const InstructorTimeLogsPage = () => { } const fetchData = async () => { + setIsLoading(true) + await fetchAllGroups() await fetchAllStudents() await fetchAllLogs() + setIsLoading(false) } fetchData() }, []) - useEffect(() => { - const fetchGroup = async () => { - try { - const group = await groupManagementService.getByStudent(selectedStudent) - setSelectedGroup(group) - } catch (error) { - console.error( - 'Error fetching group:', - error.message, - ' / ', - error.response.data.error - ) - notificationActions.setError(error.response.data.error) - } - } - - fetchGroup() - }, [selectedStudent]) - const isLogs = (logs) => logs && logs.length > 0 const logsByStudent = isLogs(allLogs) && allLogs.filter((log) => log.studentNumber === selectedStudent) + if (isLoading) return return (
{selectedStudent}
- {selectedGroup &&
{selectedGroup.groupName}
} + {selectedGroup &&
{selectedGroup.name}
}
{isLogs(logsByStudent) && diff --git a/frontend/src/components/TimeLogsPage/TimeLogsSelectForm.jsx b/frontend/src/components/TimeLogsPage/TimeLogsSelectForm.jsx index f76795f..3240233 100644 --- a/frontend/src/components/TimeLogsPage/TimeLogsSelectForm.jsx +++ b/frontend/src/components/TimeLogsPage/TimeLogsSelectForm.jsx @@ -3,15 +3,55 @@ import MenuItem from '@material-ui/core/MenuItem' import Typography from '@material-ui/core/Typography' import Select from '@material-ui/core/Select' -export const TimeLogsSelectForm = ({ students, selectedStudent, handleStudentChange }) => { +export const TimeLogsSelectForm = ({ + groups, selectedGroup, handleGroupChange, + students, selectedStudent, handleStudentChange }) => { + const GroupSelectWrapper = ({ label, children }) => ( +
+ {label} + {children} +
+ ) + + const GroupSelect = ({ + groups, + selectedGroup, + handleGroupChange + }) => { + return ( + + )} + + const StudentIsInGroup = ( student, group ) => { + return group.studentIds.includes(student.student_number) + } + const StudentSelectWrapper = ({ label, children }) => (
{label} {children}
) - + const StudentSelect = ({ + selectedGroup, students, selectedStudent, handleStudentChange @@ -23,8 +63,10 @@ export const TimeLogsSelectForm = ({ students, selectedStudent, handleStudentCha onChange={(e) => { handleStudentChange(e.target.value) }} + disabled={!selectedGroup} > - {students.map((student) => ( + {students.filter(student => StudentIsInGroup(student, selectedGroup)) + .map((student) => (
- - + - + + {selectedGroup && + + + + }
)