forked from DeerCo/IBS
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from DakshChan/feature/IBS-49-SubmitAssignment…
…MarksTable [IBS-46] submit assignment marks table
- Loading branch information
Showing
5 changed files
with
227 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
import StaffApi from '../../../api/staff_api'; | ||
import useSWR from 'swr'; | ||
import { toast } from 'react-toastify'; | ||
import { | ||
TableContainer, | ||
Typography, | ||
Paper, | ||
Table, | ||
TableHead, | ||
TableCell, | ||
TableRow, | ||
TableBody, | ||
TextField, | ||
Button | ||
} from '@mui/material'; | ||
import { useEffect, useState } from 'react'; | ||
import DashboardCard from '../../FlexyMainComponents/base-card/DashboardCard'; | ||
import SaveIcon from '@mui/icons-material/Save'; | ||
import InstructorApi from '../../../api/instructor_api'; | ||
|
||
function extractCriteriaNames(data) { | ||
const criteria = []; | ||
|
||
for (const studentData of Object.values(data)) { | ||
for (const key of Object.keys(studentData)) { | ||
if (criteria.includes(key)) continue; | ||
criteria.push({ criteriaName: key, outOf: studentData[key].out_of }); | ||
} | ||
} | ||
|
||
return criteria; | ||
} | ||
|
||
function extractMarkData(data) { | ||
return Object.keys(data).map((studentName) => { | ||
return { name: studentName, ...data[studentName] }; | ||
}); | ||
} | ||
|
||
const TaskMarkTable = ({ courseId, taskId }) => { | ||
const [criteriaNames, setCriteriaNames] = useState([]); | ||
const [taskMarkData, setTaskMarkData] = useState([]); | ||
|
||
const markFetcher = (key) => StaffApi.getMarksForTask(courseId, taskId); | ||
const { data, isLoading, error, mutate } = useSWR('/getMarksForTask', markFetcher, { | ||
refreshInterval: 60000 | ||
}); | ||
|
||
useEffect(() => { | ||
if (isLoading || error) return; | ||
|
||
const criteriaNamesTemp = extractCriteriaNames(data); | ||
setCriteriaNames(criteriaNamesTemp); | ||
|
||
const markDataTemp = extractMarkData(data); | ||
setTaskMarkData(markDataTemp); | ||
}, [isLoading, error, data]); | ||
|
||
if (isLoading) return <Typography variant="h1">Loading...</Typography>; | ||
if (error) return <Typography variant="h1">Unknown error...Try again</Typography>; | ||
|
||
const TaskMarkEntry = ({ row }) => { | ||
const name = row.name; | ||
|
||
const handleChange = (event, criteriaId) => { | ||
const tempTaskMarkData = taskMarkData.map((studentTaskData) => { | ||
if (studentTaskData.name !== name) return studentTaskData; | ||
|
||
const newStudentTaskData = { | ||
...studentTaskData | ||
}; | ||
|
||
newStudentTaskData[criteriaId] = { | ||
mark: event.target.valueAsNumber, | ||
out_of: newStudentTaskData[criteriaId].out_of | ||
}; | ||
|
||
return newStudentTaskData; | ||
}); | ||
setTaskMarkData(tempTaskMarkData); | ||
}; | ||
|
||
const handleSave = () => { | ||
let studentTaskData = taskMarkData.filter((data) => data.name === name); | ||
if (studentTaskData.length === 0) return; | ||
|
||
studentTaskData = studentTaskData[0]; | ||
Object.keys(studentTaskData).map((key) => { | ||
if (key === 'name') return; | ||
|
||
// in this case key is a criteria id | ||
InstructorApi.submitMark( | ||
courseId, | ||
taskId, | ||
key, | ||
name, | ||
studentTaskData[key].mark | ||
).catch((err) => { | ||
toast.error(`Something went wrong. Refresh and Try again`, { | ||
theme: 'colored' | ||
}); | ||
}); | ||
}); | ||
|
||
toast.success('Marks updated...', { | ||
theme: 'colored' | ||
}); | ||
|
||
mutate(); | ||
}; | ||
|
||
return ( | ||
<> | ||
{Object.keys(row).map((col) => { | ||
if (col === 'name') | ||
return ( | ||
<TableCell align="center" key={name}> | ||
<Typography>{row.name}</Typography> | ||
</TableCell> | ||
); | ||
|
||
return ( | ||
<TableCell | ||
align="center" | ||
key={`${name}-${col}-${row[col].mark}}-${row[col].out_of}`} | ||
> | ||
<TextField | ||
id="outlined-number" | ||
type="number" | ||
size="small" | ||
max={row[col].out_of} | ||
min={0} | ||
onChange={(event) => handleChange(event, col)} | ||
defaultValue={row[col].mark} | ||
/> | ||
</TableCell> | ||
); | ||
})} | ||
<TableCell> | ||
<Button variant="contained" onClick={handleSave} startIcon={<SaveIcon />}> | ||
Save | ||
</Button> | ||
</TableCell> | ||
</> | ||
); | ||
}; | ||
|
||
return ( | ||
<DashboardCard title={`Marks for ${taskId}`}> | ||
<TableContainer component={Paper}> | ||
<Table sx={{ minWidth: 650 }}> | ||
<TableHead> | ||
<TableRow> | ||
<TableCell align="center"> | ||
<Typography>Student</Typography> | ||
</TableCell> | ||
{criteriaNames.map((crit) => ( | ||
<TableCell align="center" key={`${crit.criteriaName}`}> | ||
<Typography> | ||
{crit.criteriaName} (/{crit.outOf}) | ||
</Typography> | ||
</TableCell> | ||
))} | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
<TableRow> | ||
{taskMarkData.map((entry) => ( | ||
<TaskMarkEntry key={`${entry.name}`} row={entry} /> | ||
))} | ||
</TableRow> | ||
</TableBody> | ||
</Table> | ||
</TableContainer> | ||
</DashboardCard> | ||
); | ||
}; | ||
|
||
export default TaskMarkTable; |
23 changes: 23 additions & 0 deletions
23
frontend/src/components/Page/Instructor/InstructorTaskMarksPage.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Container, Grid } from '@mui/material'; | ||
import NavBar from '../../Module/Navigation/NavBar'; | ||
import { useParams } from 'react-router-dom'; | ||
import { useEffect } from 'react'; | ||
import TaskMarkTable from '../../Module/Mark/TaskMarkTable'; | ||
|
||
const InstructorTaskMarksPage = () => { | ||
const { course_id, task_id } = useParams(); | ||
return ( | ||
<Grid container spacing={2}> | ||
<Grid xs={12}> | ||
<NavBar item page="Task Marks" role="instructor" /> | ||
</Grid> | ||
<Grid item container spacing={2} xs={12}> | ||
<Container> | ||
<TaskMarkTable courseId={course_id} taskId={task_id} /> | ||
</Container> | ||
</Grid> | ||
</Grid> | ||
); | ||
}; | ||
|
||
export default InstructorTaskMarksPage; |